开发者

Spring hibernate jdbc batch size

I have a few scenarios which I think is a little unclear from the hibernate documenation.

Data class:

class HibernateDao {
      // ...

      @Transactional
      public void store(List<Object> batch) {
          for(Object o : batch) {
             hibernate.store(o);
          }
  开发者_JAVA百科    }
}

Scenario 1

hibernate.jdbc.batch_size = 1

Q1: What happens when invoking store(..) with a collection of 10 items? Will there be 10 x 1 transactions or only one?


Scenario 2

hibernate.jdbc.batch_size = 10

Q2: What happens when invoking store(..) with a collection of 1 items? Will it immediately be written to the backing store regardless of the batch_size property?


From the hibernate documentation:

Hibernate disables insert batching at the JDBC level transparently if you use an identity identifier generator

Q3: What is classified as an identify identifier generator, using the annotations @Id and @GeneratedValue(strategy = GenerationType.AUTO)?


Q1: What happens when invoking store(..) with a collection of 10 items? Will there be 10 x 1 transactions or only one?

This is specific to Spring transactions, but as far as Hibernate is concerned, if you are using one session and one "transaction", it'll wait till "flush" or "commit" to actually perform the operations. So, one transaction.

Q2: What happens when invoking store(..) with a collection of 1 items? Will it immediately be written to the backing store regardless of the batch_size property?

Not immediately. The same from the previous response applies here: unless you explicitly ask for a "flush", Hibernate will do the action during the commit phase.

Q3: What is classified as an identify identifier generator, using the annotations @Id and @GeneratedValue(strategy = GenerationType.AUTO)?

Anything which Hibernate cannot predict as the ID. For instance, identity (like sequences, but for T-SQL databases), auto-increment, sequences, ... The reason is simple: Hibernate does not knows what would be the generated ID for each of the batched entities, so, the state of the entity after the insert would be different than the state before. Hibernate handles that in regular scenarios by calling the JDBC method for "getGeneratedKeys", which allows it to synchronize the data from the database with the data in its session, but it's not possible to do it for batches.

If Hibernate does knows what would be the ID for the entities (ie: assigned, hilo, uuid, ...) it can safely perform a batch the execution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜