Working with batch processing

Sometimes, we need to save a large number of records in the database. Let's say we need 10,000 records in the database, and we use the basic approach to save the records. The way to do this is as follows:

Session session = SessionFactory.openSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<100000; i++ ) {
    Employee employee = new Employee(.....);
    session.save(employee);
}
tx.commit();
session.close();

Two known issues in the preceding method are as follows:

  • Hibernate will try to save each object to the database one by one; this will be time consuming and may increase the load on the database and application as well
  • The application may face OutOfMemoryException because hibernate saves all the new employee objects in the second-level cache

To overcome these problems and to make the application faster, we need to use batch processing. Hibernate supports batch processing, which is the same as a JDBC batch processing.

Getting ready

The following codes help you to create the required classes and tables for this recipe.

Creating the classes

To perform batch processing using hibernate, we need to create the Employee class and make a little change in configuration file; here, we need to make the changes in hibernate.cfg.xml. Update the following code:

Source file: Employee.java

@Entity
@Table(name = "employee")
public class Employee {

  @Id
  @GeneratedValue
  private long id;

  @Column(name = "name")
  private String name;

  // getters and setters  
}

Source file: hibernate.cfg.xml

…
 <property name="hibernate.jdbc.batch_size">
    50
</property>  
 <property name="hibernate.cache.use_second_level_cache"> 
false 
</property>  
…

How to do it…

First of all, we will take a look at an executable code snippet that shows how to use batch processing in hibernate:

Source file: BatchProcessingMain.java

Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
for (int i = 0; i < 10000; i++) {
  Employee employee = new Employee();
  employee.setName("Name : " + String.valueOf(i));
  session.save(employee);
/* Line 7 */  if (i % 50 == 0) {
/* Line 8 */   session.flush();
/* Line 9*/   session.clear();
  }
}
transaction.commit();
session.close();

The following is the output for the preceding code:

Hibernate: insert into employee (name) values (?)
Hibernate: insert into employee (name) values (?)
Hibernate: insert into employee (name) values (?)
.
.
.
Hibernate: insert into employee (name) values (?)

How it works…

First of all, the change in hibernate.cfg.xml is hibernate.jdbc.batch_size = 50, informing hibernate to create a batch of 50 for the batch operation. Another is hibernate.cache.use_second_level_cache = false, informing hibernate not to cache the object as we were doing a batch operation, and it is unnecessary to store the objects in the cache.

In the executable code, we looped 10,000 times and saved the records using the session.save(…) method. In Line 7, we checked whether the value of the i variable was equal to a multiple of 50 and then flushed and cleared the session.

The Session.flush() method was used to persist a record and sent to the database. You cannot actually see it from the database or using another session/thread because this record is not committed yet. Once a transaction is committed, the records are available for the other session/thread.

The Session.clear() method clears all the cached records from the session and releases the memory.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.24.30