miercuri, 27 iulie 2016

Hibernate session-level batching via Spring MVC example

Thanks to Hibernate team (especially to Vlad Mihalcea) starting with version 5.2 we have support for session-level batching. Basically, this is possible via Session#setJdbcBatchSize() and Session#getJdbcBatchSize(). In order to build an example via Spring MVC, you will need Hibernate 5.2 and Spring 4.3.1. The relevant part of the code is:

productDAO.persistWithBatching(n, 10);

And ...

public void persistWithBatching(int n, int batchsize) {

 Session session = getEntityManager().unwrap(Session.class);

 for (int i = 0; i < n; i++) {
      Product p = new Product(i);

      // Flush a batch of inserts and release memory
      if (i % session.getJdbcBatchSize() == 0 && i > 0) {


Note: Setting session-level batching will override the value of hibernate.jdbc.batch_size if it is set. Nevertheless, if you want to globally set the batch size then it is recommended to use the hibernate.jdbc.batch_size.

The complete example is here.

Niciun comentariu:

Trimiteți un comentariu