简体   繁体   中英

Setting Hibernate property hibernate.jdbc.batch_size over explicitly calling session.flush()

Going through the batch processing document of hibernate https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/batch.html . I tried an example setting the batch size property( hibernate.jdbc.batch_size=20 ) in hibernate configuration file and then doing flush and clear like:

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 ) { //20, same as the JDBC batch size
        //flush a batch of inserts and release memory:
        session.flush();
        session.clear();
    }
}

tx.commit();
session.close();

Now the thing is if I set the value of i to 50, then it flushes the records as a batch of 50. So, if I can control the batch size here in the loop , why do I need to set hibernate.jdbc.batch_size 20 property.

Can someone please explain the difference between the two approaches ie setting the property over calling flush explicitly.

My understanding is that your loop and hibernate.jdbc.batch_size operate on different levels.

Your loop operates on entities (of your choice). But a single Customer might need multiple inserts (for example, when a Customer has multiple Adresses). All these inserts get executed as simple single inserts with your loop approach. The flush and clear just prevent the session to grow without limit.

hibernate.jdbc.batch_size on the other hand will bundle (insert)statements that are identical and only differ in in the parameter values into a single statement, with a list of parameter sets. The execution of such a batched statement should be processed much more efficient by the database then the equivalent single statements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM