Hi,
Here is my setup:
Hibernate version : 3.3.2
Spring version: 3.0
Transaction Manager with Spring Transaction Advice
I am having a problem with making hibernate calls in a loop. I know that it is not advisable to make database calls in a loop, unfortunately that is the way the business logic is.
The problem I am having is that when I am making repeated calls to hibernate in a loop (around 1000 calls), spring is creating a session & connection for each call and eventually the application hangs after iterating for a value equal to the max connections value in hibernate.config.xml. I believe this is the problem of using spring transaction advice of PROPAGATION=REQUIRED. So I changed the transaction advice to PROPAGATION=NEVER and I am managing the transaction myself with frequent flushes. However, the application still hangs after iterating for a while. Here is a sample code
Code:
Session currentSession = businessDao.getCurrentSession();
for(int i=0; i < 1000; i++)
{
businessDao.saveUsingSession(currentSession, object);
}
//Business Dao code
public void saveUsingSession(Session session, Object a)
{
Transaction t = session.beginTransaction();
session.save(a);
t.commit();
session.flush();
}
//spring transaction advice
<tx:method name="saveUsingSession" PROPAGATION="NEVER"/>
Here is my hibernate.cfg.xml
Code:
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">30</property>
<property name="hibernate.c3p0.timeout">600</property>
<property name="hibernate.c3p0.maxStatementsPerConnection">100</property>
<property name="hibernate.jdbc.batch_size">40</property>
<property name="hibernate.c3p0.idle_test_period">300</property>
<property name="connection.provider_class">
org.hibernate.connection.C3P0ConnectionProvider
</property>
<property name="dialect">
org.hibernate.dialect.MySQLInnoDBDialect
</property>
With PROPAGATION=NEVER, the loop runs about 200 times before it hangs. With PROPAGATION=REQUIRED and not using the saveWithSession version, the code does not go more than 30 iterations. Can someone please help me how to solve this problem.
Thanks