Saving entities in a batch

This post explains how to save large number of entities in one batch or one unit of work.

First we need to set the below property in configuration file
hibernate.jdbc.batch_size

The value will be an integer, which indicates the number of insert statements to be grouped together as one unit of work or one batch.

Below is the contents of configuration file

Configuration file

<?xml version=”1.0″?>
<!DOCTYPE hibernate-configuration PUBLIC “-//Hibernate/Hibernate Configuration DTD 3.0//EN” “http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”&gt;
<hibernate-configuration>
<session-factory>
<property name=”hibernate.connection.driver_class”>org.gjt.mm.mysql.Driver</property>
<property name=”hibernate.connection.password”>MySQL</property>
<property name=”hibernate.connection.url”>jdbc:mysql://localhost:3306/hibernate</property>
<property name=”hibernate.connection.username”>root</property>
<property name=”hibernate.connection.password”>admin</property>
<property name=”hibernate.dialect”>org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name=”show_sql”>false</property>
<property name=”format_sql”>false</property>
<property name=”hibernate.hbm2ddl.auto”>create</property>
<property name=”hibernate.jdbc.batch_size”>25</property>

<mapping resource=”model/Series.hbm.xml”/>
</session-factory>
</hibernate-configuration>

Main Code


1  import model.Series;
2  
3  import org.hibernate.Session;
4  import org.hibernate.SessionFactory;
5  import org.hibernate.Transaction;
6  
7  public class HibernateDemo8 {
8   public static void main(String[] args) {
9       SessionFactory sessionFactory = HibernateUtil1.createSessionFactory();
10      Session session = sessionFactory.openSession();
11      int batch_size = session.getSessionFactory().getSessionFactoryOptions().getJdbcBatchSize();
12      Transaction tx = session.beginTransaction();
13      for(int i = 0; i < 100; i++) {
14          Series series = new Series();
15          series.setId(i);
16          series.setDescription("Description" + i);
17          series.setName("Name" + i);
18          
19          session.save(series);
20          if(i%batch_size == 0) {
21              System.out.println("flushing at " + i);
22              session.flush();
23              session.clear();
24          }
25      }
26      tx.commit();
27      session.close();
28      HibernateUtil1.shutdown();
29  }
30 }

Explanation

In the configuration file, we set the “hibernate.jdbc.batch_size” to 25 indicating we want to group 25 insert statements as one unit of work or one batch.

We obtain the above configured value in the main code using the below code snippet. Refer to line 11 in the main code
SessionFactory.getSessionFactoryOptions().getJdbcBatchSize();

Once we obtain the batch size, we begin the transaction at line 12

In the for loop we are trying to save 100 entities, with 25 entities in per unit of work. Every call to save method, puts the entity in the cache.

We check whether the batch size has been reached using the below condition
i%batch_size == 0

Once reached, we call the flush method to synchronize the database with the values in the cache.

Then we call clear method to clear the cache containing these entries so that we can add another 25 entities in the cache.

In this way, every 25 entities per batch is saved to the database.

Once out of for loop, all the changes done until now are committed to the database by calling commit on the transaction.

Output

flushing at 0
flushing at 25
flushing at 50
flushing at 75
committing at 100

Leave a Reply