Many people have doubt about whether Java's handles of batch data is their proper place, thereby extending, then it is considered that ORM may not be particularly suitable for batch processing. In fact, I think if we apply, you can eliminate the concerns of ORM batch processing performance issues. The following is a description as an example, if we really have to use Hibernate in Java to batch processing on data. Insert 100,000 data to the database, using hibernate like this:
Session session = sessionFactory.openSession ();
Transaction tx = session.begintransaction ();
For (int i = 0; i <100000; i ) {
Customer Customer = New Customer (.....);
Session.save (Customer);
TX.comMit ();
session.close ();
In probably running to No. 50,000, there will be memory overflows. This is Hibernate to cache the recently inserted Customer in memory, and we don't forget Hiberante without limiting the cache size of First-Level Cache:
# 持 对动 example is managed at this time, and Hibernate is synchronized with the database any already occurred.
Variable managed object.
# In a single continuous content, the persistence layer characteristics are the same as Java features (this helps you remove it.
Some confused data)
# Session implementation of asynchronous WRITE-BEHIND, which allows the batch of Hibernate to explicitly write operation.
Here, I give Hibernate how to implement batch insertion:
First, we set a reasonable JDBC batch size, hibernate.jdbc.batch_size 20. The session is then performed at a certain interval () and CLEAR ().
Session session = sessionFactory.openSession ();
Transaction tx = session.begintransaction ();
For (int i = 0; i <100000; i ) {
Customer Customer = New Customer (.....);
Session.save (CUSTOMER);
IF (i% 20 == 0) {
// Flush inserts data and releases memory:
Session.flush (); session.clear ();
}
TX.comMit ();
session.close ();
So how do you delete and update data? Well, in hibernate2.1.6 or more, scroll () This method will be the best way:
Session session = sessionFactory.openSession ();
Transaction tx = session.begintransaction ();
Scrollableresults Customers = session.getnamedQuery ("getcustomers")
. Scroll (scrollmode.forward_only);
INT count = 0;
While (Customers.Next ()) {
Customer Customer = (Customer) Customers.get (0); Customer.Updatestuff (...);
IF ( count% 20 == 0) {
// Flush update data and release memory:
Session.flush (); session.clear ();}}
TX.comMit (); session.close ();
This approach is not difficult, nor is it not elegant. Please note that if Customer enabled Second-Level Caching, we will still have some memory management issues. The reason is that each insertion and update of the user, Hibernate has to announce Second-Level Cac after the end of the transaction. Therefore, we will disable users to use caches in batches.