Optimizing inserts \ updates in hibernate

Optimizing insert \ update

(the previous article was about optimizing fetches)

Lets says we have big table or small with big clob \ blob column. We trying to perform a big set of inserts or updates on this table. For simplicity this example will look like .. 

// begin transaction

for (i = 0; i < 100000; i++) {
                    TestTable tt = new TestTable();
                    tt.setValue(t);
                    tt.setCharStorage(charStorage);  // big clob, say we storing some file
                    //tt.setId(Long.valueOf(i)); // using hibernate sequence so not required
                    session.save(tt);
}

//end transaction

In case the memory foot print of the application is huge – 

Capture1

As seen in jconsole my process memory jumped from 5MB initial to 53 MB. The reason for this hibernate is cache all the newly create test table object in session cache. Once the transaction completes the memory usage comes down. It has potential of giving out of memory error.

We can avoid this by using batch inserts and updates. We would use below code, 

// begin transaction

for (i = 0; i < 100000; i++) {
TestTable tt = new TestTable();
tt.setValue(t);
tt.setCharStorage(charStorage);  // big clob, say we storing some file
//tt.setId(Long.valueOf(i)); // using hibernate sequence so not required
session.save(tt);

if ( i%50 == 0 ) { //50 is our batch size.
session.flush();
session.clear();
}
} // end of for loop

//end transaction

Now this code is place, the memory usage hardly increases.

Capture3

If i was not using sequence generator for my primary key, i would have also used hibernate property

<property name=”hibernate.jdbc.batch_size”>50</property>

Hibernate disables insert batching at the JDBC level transparently if you use an identity identifier generator.

This same approach works for updates as well.

Link to hibernate documentation on batching. http://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/batch.html

Advertisements

One thought on “Optimizing inserts \ updates in hibernate

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s