24.1 Overview
We may want to develop the back end utility or programs where we need to update the bulk data in the database or may need to insert huge number of records. In such scenarios we need to flush the session at a regular interval to make sure that we did not hit OutOfMemoryException because hibernate first level cache is session cache.
Flushing and clearing the session at a regular intervals will persist the data and will reuse the memory and hence we can avoid the out of memory exception. This approach is known as batch processing and in this chapter, we will discuss more about batch processing in hibernate.
24.2 Setting JDBC Batch Size
For Batch processing in hibernate, we need to set the JDBC batch size in hibernate cfg file using hibernate.jdbc.batch_size property. The ideal value of this property is between 10 to 50.
Make sure you flush and clear the session after the insert and update of records equal to the number you configure in hibernate.jdbc.batch_size property.
For batch updates use Scroll method for optimal performance.
24.3 Example
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.url"> jdbc:mysql://localhost:3306/tutorial </property> <property name="hibernate.connection.username"> root </property> <property name="hibernate.connection.password"> password </property> <property name="hibernate.jdbc.batch_size">50</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> <mapping resource="book-mapping.hbm.xml" /> </session-factory> </hibernate-configuration>
book-mapping.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.tutorial.hibernate.entity.Book" table="Book"> <meta attribute="class-description"> This class contains the employee detail. </meta> <id name="id" type="int" column="id"> <generator class="native"/> </id> <property name="isbn" column="isbn" type="string"/> <property name="name" column="name" type="string"/> <property name="author" column="author" type="string"/> <property name="publisher" column="publisher" type="string"/> <property name="price" column="price" type="int"/> </class> </hibernate-mapping>
Book Entity
package com.tutorial.hibernate.entity; public class Book { private int id; private String isbn; private String name; private String author; private String publisher; private int price; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getIsbn() { return isbn; } public void setIsbn(String isbn) { this.isbn = isbn; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getPublisher() { return publisher; } public void setPublisher(String publisher) { this.publisher = publisher; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } }
Create Book Table- Use the below create script to create book table.
CREATE TABLE 'book' ( 'id' int(11) NOT NULL AUTO_INCREMENT, 'isbn' varchar(255) DEFAULT NULL, 'name' varchar(255) DEFAULT NULL, 'author' varchar(255) DEFAULT NULL, 'publisher' varchar(255) DEFAULT NULL, 'price' int(11) DEFAULT NULL, PRIMARY KEY ('id'); )
Test Program to insert 1000 records of Book
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.tutorial.hibernate.entity.Book; public class Test { private static SessionFactory factory; public static void main(String args[]) { Configuration cfg = new Configuration().configure(); factory = cfg.buildSessionFactory(); Session session = factory.openSession(); Transaction tx = session.beginTransaction(); for(int i=0;i<1000;i++) { Book book = new Book(); book.setAuthor("Author-"+i ); book.setName("Hibernate Batch-" + i); book.setIsbn("A1234-"+i); book.setPrice(133); book.setPublisher("Dummy-"+i); session.save(book); if(i%50==0) { session.flush(); session.clear(); } } tx.commit(); factory.close(); } }
Running the above program will insert 1000 records