Why number of objects being flushed should be equa

2019-05-29 02:58发布

问题:

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?

回答1:

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.