Hibernate Batch (batch inserts, updates and deletes)

Source: Internet
Author: User
Tags bulk insert

Summary: Hibernate in bulk processing does not give the force of the main reason is that there is a cache in the session, and hibernate mechanism is through the session of the first level cache to synchronize the database, so when the batch processing, A large amount of data stored in the cache consumes a lot of memory resources, causing various crashes.

In fact, usually work with a lot of batch processing data is still very few, rarely encountered tens of thousands of data batch processing, but if encountered with hibernate to deal with or very tangled, and then go to Hibernate official website to see if there is any treatment method to learn under.

Just when working with Hibernate is not very skilled, encountered batch processing when the direct cut to JDBC to do processing, compare the style of mixing. But it feels like the code style is not a good feeling.

 BULK INSERT:

It can be difficult to do this with hibernate if you have 10w data that needs to be inserted into the database.

The way to compare too young too naive is to cycle:

1 Session session = Sessionfactory.opensession (), 2 Transaction tx = Session.begintransaction (); 3 for (int i=0; i<10000 0; i++) {4     Customer customer = new Customer (...); 5     Session.save (Customer), 6}7 tx.commit (); 8 session.close ();

This will probably jump out of the Outofmemoryexcepiton at 5w, because Hibernate's first-level cache causes the data to be cached in the session, and when the transaction commits or flush, the data is synchronized to the database. Data volume is too big when the session will hold you understand.

It can be a little clever, before using the following method, remember to turn on JDBC batch processing, set the hibernate.jdbc.batch_size property value from 10 to 50 in the Hibernate configuration file:

1 Session session = Sessionfactory.opensession (); 2 Transaction tx = Session.begintransaction (); 3     4 for (int i=0; i<100000; i++) {5     Customer customer = new Customer (...); 6     Session.save (Customer); 7     if (i% 20 = = 0) {//20, and the Hibernate.jdbc.batch_size attribute values in the configuration file are consistent 8          9         session.flush ();  Forces the cache in session to be flushed to the database in         session.clear ();  Clear the cache in session, Ps:evict () kill one instance of the session in the cache     }12}13    tx.commit ();

 Batch update:

You can also use the Fulsh () and clear () methods to periodically clear the cache in the session, as in bulk inserts. In addition, you can use the scroll () method to handle when you want to query large amounts of data from the database and want to update the data, it seems a bit of a cursor feeling AH:

  

1 Session session = Sessionfactory.opensession (); 2 Transaction tx = Session.begintransaction (); 3     4 Scrollableresults customers = Session.getnamedquery ("GetCustomers") 5     . Setcachemode (Cachemode.ignore)  //Set the cache mode to: This session will not be associated with the cache, do not use the cache 6     . Scroll (scrollmode.forward_only);  7 int count=0; 8 while (Customers.next ()) {9     Customer customer = (customer) customers.get (0);     customer.updatestuff (...);     if (++count% = 0) {         //sync data and free memory:         Session.flush ();         session.clear ();     }16}17< C13/>18 tx.commit (); Session.close ();
The constant fields of the Org.hibernate.CacheMode are:
The get session reads data from the cache, but does not add data to the cache unless the cached data is updated to invalid data
IGNORE session does not interoperate with any caches unless the data in the cache is updated to invalid data
The put session does not read data from the cache, but it adds data read from the database to the cache
Refresh session does not read data from the cache, but it adds data read from the database to the cache, unlike put, which ignores the hibernate.cache.use_minimal_puts attribute in the configuration file in order to force a flush of the cache

The constant fields of the Org.hibernate.ScrollMode are:

Forward_only: Requests a result set that resembles a cursor and forwards only this result set

Scroll_insensitive: Requests a result set of a cursor and is not sensitive to changes in the underlying data

Scroll_sensitive: Requests a result set of a cursor and is sensitive to underlying data changes

There is no way to use this property, not exactly what it means. I'll take a look at it later.


Batch processing via Statelesssession
1 statelesssession session = Sessionfactory.openstatelesssession (); 2 Transaction tx = Session.begintransaction (); 3     4 Scrollableresults customers = Session.getnamedquery ("GetCustomers") 5     . Scroll (scrollmode.forward_only); 6 while (Customers.next ()) {7     Customer customer = (customer) customers.get (0); 8     customer.updatestuff (...);//Do Some update operations 9     session.update (Customer),}11    tx.commit (); Session.close ();
Notes: An instance of a customer returned by a statelesssession query will immediately become free and will not be associated with any persistence layer in the context and there will be no easing of any associations. Because the statelesssession itself does not include a first-level cache, there is no need to consider a cache overflow problem.

  Notes: Statelesssession does not have a primary cache, does not interact with the level two cache and other caches, and does not implicitly generate transaction and no dirty data checks.

Statelesssession is an abstract interface that is relatively low-level and very close to the underlying JDBC. It defines the insert (), update (), and Delete () methods that are directly acting on the data in the database. As with the SQL operations database directly using JDBC, the operations defined by Save (), Saveorupdate (), and delete () in the session interface are quite different.

The final approach is to perform bulk operations of the database through HQL. This will continue to be mentioned in a later article.

Here is the official text, please forgive my piracy:

Http://docs.jboss.org/hibernate/orm/4.2/devguide/en-US/html/ch04.html

Hibernate Batch (batch inserts, updates and deletes)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.