Batch Processing of Hibernate-Batch insertion

Source: Internet
Author: User

Hibernate operates databases in an object-oriented way. When a program operates persistence objects in an object-oriented way, it is automatically converted to database operations. For example, if we call the delete () method of the Session to delete the Persistent Object, Hibernate will delete the corresponding data record. When we execute the setter method of the Persistent object, hibernate will automatically convert to the underlying update statement to modify the corresponding records of the database.

The problem is: if we need to update 100000 records at the same time, is it necessary to load 100000 records one by one, and then call the setter method in sequence-this is not only cumbersome, but also has poor data access performance. In the face of this batch processing scenario, Hibernate provides a batch processing solution. The following describes how to deal with this situation from batch insert, batch update, and batch Delete.

Batch insert

If you need to insert 100000 records into the database, Hibernate may adopt the following practices:

 
 
  1. Session session = sessionFactory. openSession ();
  2. Transaction tx = session. beginTransaction ();
  3. // Insert 100000 records for 100000 cycles
  4. For (int I = 0; I <100000; I ++)
  5. {
  6. User u = new User (.....);
  7. Session. save (u );
  8. }
  9. Tx. commit ();
  10. Session. close ();

However, as this program runs, it always fails at some time and throws an OutOfMemoryException memory overflow exception ). This is because the Hibernate Session holds a required level-1 cache, and all User instances will be cached in the Session-level cache area.

To solve this problem, we have a very simple idea: regularly refresh Session cached data into the database, rather than always caching at the Session level. You can consider designing a accumulators. Each time you save a User instance, the number of accumulators increases by 1. Determines whether to fl Session cache data into the database based on the value of the accumulators.

The following is a code snippet for adding 100000 User instances.

Program list: codes \ 06 \ 6.3 \ batchInsert \ src \ lee \ UserManager. java

 
 
  1. Private void addUsers () throws Exception
  2. {
  3. // Open the Session
  4. Session session = HibernateUtil. currentSession ();
  5. // Start the transaction
  6. Transaction tx = session. beginTransaction ();
  7. // Insert 100000 records for 100000 cycles
  8. For (int I = 0; I <100000; I ++)
  9. {
  10. // Create a User instance
  11. User u1 = new User ();
  12. U1.setName ("xxxxx" + I );
  13. U1.setAge (I );
  14. U1.setNationality ("china ");
  15. // Cache the User instance at the Session level
  16. Session. save (u1 );
  17. // When the accumulators are multiples of 20, the Session data is flushed into the database,
  18. // And clear the Session cache.
  19. If (I % 20 = 0)
  20. {
  21. Session. flush ();
  22. Session. clear ();
  23. }
  24. }
  25. // Submit the transaction
  26. Tx. commit ();
  27. // Close the transaction
  28. HibernateUtil. closeSession ();
  29. }

In the above Code, when I % 20 = 0, manually write the data cached in the Session and clear the data cached in the Session. In addition to processing Session-level cache, you should also disable the second-level cache of SessionFactory through the following Configuration:

 
 
  1. hibernate.cache.use_second_level_cache false 

Note: In addition to manually clearing the Session-level cache, it is best to disable the SessionFactory-level second-level cache. Otherwise, even if the Session-level cache is manually flushed, an exception may occur because there is a second-level cache in SessionFactory. For more information about Level 2 caching, see the following content.

This article from the "crazy Java Li Gang" blog, please be sure to keep this source http://javaligang.blog.51cto.com/5026500/910675

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.