"Java" Itoo project, Hibernate batch Save optimization

Source: Internet
Author: User

in the Itoo the. Basically every system has an import function, and a lot of data is filled into the Excel template. Then use the Import feature to import the database, which can greatly improve productivity.

Then the import involves the problem of bulk saving the database.

well, usually, in the case of a Session object to a limited number of persisted objects in the cache until the Session The object handles the transaction completion and also closes the Session object so that it can be released in time Session memory consumed by the cache. In the bulk save 1 million data, assume that the need to save 1 million data to the memory workers, when running the transaction commits, will clean up the cache, Hibernate runs the 1 -million UPDATE statement.

There are two drawbacks to this way of updating:

( 1 ) consumes a lot of memory. You must load 1 of thousands of bars into memory and update them all.

( 2 ) Run the Update an excessive number, each Update statement can only update one piece of data, you must pass 1 million Bar Update Statement Talent Update 1 million-piece data. Frequent access to databases can greatly reduce the performance of your application.

for the above situation. We can do The batch operation through the Session. the Sava method of the session will put the processing object in its own cache staff, assuming that through a session object to handle a large number of persisted objects, the object that has been processed and not being interviewed should be emptied from the cache in a timely manner.

The detailed practice is to immediately call the Flush() method to clean up the cache after processing an object or a small batch object, and then call the clear() method to empty the cache.


assuming that through Session For Bulk Operations, the following constraints apply:


1. There is a need to set the number of JDBC single batch processing in hibernate configuration file . The reasonable value is usually ten~50, such as hibernate.jdbc.batch_size=30; this is set. It is necessary to ensure that the number of batches of SQL statements sent as a database is consistent with this batch_size attribute.


2. Assuming that the operand is using the "identity" identifier generator, Hibernate cannot be in the JDBC layer for BULK insert operations.


3. when doing bulk operations. We recommend that you turn off hibernate 's second-level cache. the Session is cached as hibernate 's first-level cache, which is typically a transaction-scoped cache. Each transaction has a separate first-level cache.

sessionfactory the external cache is Hibernate second-level meat dishes, which is the cache in the application scope, and all transactions share the same second-level cache. In either case,hibernate 's first-level cache is always available, by default. Hibernate 's second-level cache is closed. However, you can also manually turn off level two caching in Hibernate's config file:

Hibernate.cache.use_second_level_cache=false;

in the Itoo The code in bulk saves such as the following:

<span style= "FONT-FAMILY:FANGSONG_GB2312;FONT-SIZE:18PX;" >/** * Bulk Save * * @param list *            List type * @return Returns a Boolean value */public <T> boolean saveentitys (list<t> list {Boolean flag = False;int BatchSize = 30;int i = 0;getdatabasename (list.get (0)); try {for (Object entity:list) {Getenti Tymanager (). Persist (entity), i++;if (i% batchsize = 0) {Getentitymanager (). Flush (); Getentitymanager (). Clear ();}} Flag = true;} catch (Exception e) {}return flag;} </span>

in the above program, each run Session.flush () method, the database staff will be inserted in bulk - record. The next session.clear () method empties the first saved object from the cache.

Only if the design has a batch import situation, will involve the content of the bulk storage, this blog is to let your bulk storage to achieve the best results.

"Java" Itoo project, Hibernate batch Save optimization

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.