Reputation: 11944
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 i.e setting the property over calling flush explicitly.
Upvotes: 2
Views: 4792
Reputation: 81862
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.
Upvotes: 2