Make delicious recipes!

Batch processing in Hibernate

Caching in Hibernate can sometimes lead to Out-Of-Memory exception if too many objects are being inserted or updated (for example, in a loop).
When so many objects are being updated, hibernate tries to delay their updation in the database by keeping them in cache for as long as possible, thus leading to an Out-Of-Memory exception.

This can be resolved by calling session.flush() and session.clear() periodically after say every 100 updates.
Alternatively, a property can be added to hibernate.cfg.xml configuration file as follows:
(See line 25 below)

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:4000/yourdb-SID</property>
    <property name="hibernate.connection.username">admin</property>
    <property name="hibernate.connection.password">admin</property>
    <property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property>
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <property name="hibernate.cache.provider_class">
    <property name="hibernate.jdbc.batch_size">

    <mapping resource="Person.hbm.xml" />
    <mapping resource="Student.hbm.xml" />

Like us on Facebook to remain in touch
with the latest in technology and tutorials!

Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Email: (Your email is not shared with anybody)

Facebook comments:

Site Owner: Sachin Goyal