How to resolve Hibernate OutOfMemoryError problem – Hibernate Batch processing

When using Hibernate for saving a large number of entities to database, You can meet an OutOfMemoryException. The tutorial will show the case and resolve it with Hibernate Batch processing.

Related articles:
How to start development with Hibernate – XML Mapping File Hibernate

I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Hibernate: 5.2.10.Final
– MySql database

II. Problem & Solution

1. Problem

Using Hibernate to save 20000 entities to database as below code:

An Exception java.lang.OutOfMemoryError will be thrown somewhere around the 16700th records:

Hibernate Batch Processing - OutOfMemoryException - insert

– The same issue when using Hibernate to update 20000 entities with below code:

Why ?

Hibernate Batch Processing - hibernate caching

Because Hibernate has a mandatory cache is Session, and all the persisted objects will be stored in Session before commit() function is called. So OutOfMemoryException will be thrown out when Session cache meets the limit of Heap size.

2. Solution

Hibernate Batch processing will resolve the problem.

Step to configure Batch processing:

2.1 Config Batch size

– Set hibernate.jdbc.batch_size properties in hibernate.cfg.xml.
hibernate.jdbc.batch_size should be range from 10 to 50 (an reasonable number should be chose base on Object’s size).
– Hibernate uses hibernate.jdbc.batch_size to determine the number of persisted objects will be inserted as batch then release it from caching.

2.2 Modify Batch processing code

– With Batch Insert processing, modify code as below:

– With Batch Updates processing, modify code as below:

III. Practices

Step to do:
– Create Spring Maven project
– Create persistent class
– Setup Hibernate configuration
– Implement Hibernate Application
– Run & Check Results

1. Create Spring Maven project

Using Spring Tool Suite, create a Simple Spring Maven project.
Then modify pom.xml file with Hibernate & MySql dependencies:

2. Create persistent class

– Create a Data.java:

3. Setup Hibernate configuration

Under folder /src/main/resources:
– Create Hibernate Configuration file – hibernate.cfg.xml:
Define hibernate.jdbc.batch_size = 50

– Create a mapping file for Data class – Data.hbm.xml:

4. Implement Hibernate Application

– Firstly, create a Utilities class has a utility function: generatedRandomString()

– In Application.java, implement 4 functions:
setUpSessionFactory(): setup Hibernate Session Factory.
storeData(): save Data entities to database.
updateData(): update Data entities.
shutdown(): close Hiberbate Session Factory Object.

5. Run & Check Results

– Create a MySql data table by script:

5.1 Batch Insert

– Run the class Application.java with main modified function:

Insert successfully with Heap Memory Usage chart:

Hibernate Batch Processing - batch processing insert

5.2 Batch Update

– Run the class Application.java with main modified function:

Insert successfully with Heap Memory Usage chart:

Hibernate Batch Processing - batch processing update

IV. Sourcecode

SpringHibernateBatch


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*