3

As hibernate documentation says when doing batch inserts/updates the session should be flushed and cleared when number of objects equal to the jdbc batch size (hibernate.jdbc.batch_size). My problem is why that number should be equal to the hibernate.jdbc.batch_size. Is there a performance tip?
Edit: For an example think that I have set the hibernate.jdbc.batch_size to 30 in my hibernate.cfg file. Then As the doc says, the session should be flushed when the object count equals to 30. Why shouldn't I flush when the object count is 20 or 40?

prageeth
  • 7,159
  • 7
  • 44
  • 72
  • Its a memory related thing, when you insert or update a entity in database, hibernate keeps it into persistent state and in its session, when you flush the session it removes all the entities from session and they are all detached now. Therefore more free memory hence better performance. – Darshan Lila Aug 21 '14 at 05:06
  • @DarshanLila Thanks for the comment. I agree with you. But actually my problem is why we should flush the session when the object count equals to configured batch_size? Why should we stick to that configured number? I edited my question to be more clear. – prageeth Aug 21 '14 at 05:21
  • http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/batch.html this should clear your confusion. – Darshan Lila Aug 21 '14 at 05:23
  • Thanks. But it seems that this is the same link which I have posted. – prageeth Aug 21 '14 at 05:26
  • And by the way when you set `hibernate.jdbc.batch_size` to `30` it means hibernate will only do inserts and updates upto 30 entities. – Darshan Lila Aug 21 '14 at 05:28

1 Answers1

6

A JDBC batch consists of doing the following:

  • add insert statements to a batch, kept in memory
  • when you have reached a given amount of insert statements recorded in the batch (or when there are no insert statements to execute anymore), send this batch command to the database, in a single network roundtrip, in order for the database to actually execute these insert statements.

Flushing the session consists in telling Hibernate: insert everything kept in memory to the database.

So, if your batch size is set to 30, and you flush every 5 entity, Hibernate will execute lots of small batches of 5 insert statements, instead of executing 6 times less batches of 30 statements. Since you have determined that 30 was the optimal batch size, flushing every 5 entity doesn't use this optimal size.

If, on the contrary, you flush every 35 entity, then Hibernate will execute a batch of 30 inserts, then a batch of 5, then a batch of 30, then a batch of 5, etc. And once again, you're not using the optimal batch size.

If you flush every 30 entity, then hibernate will only execute batches of the optimal size, except for the last one if the total number of entities is not a multiple of 30.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255