-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Hibernate calls in a loop
PostPosted: Wed Apr 27, 2011 4:14 pm 
Newbie

Joined: Tue Jul 19, 2005 1:28 pm
Posts: 17
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


Top
 Profile  
 
 Post subject: Re: Hibernate calls in a loop
PostPosted: Wed Apr 27, 2011 5:51 pm 
Newbie

Joined: Fri Apr 08, 2011 6:24 am
Posts: 9
http://docs.jboss.org/hibernate/core/3. ... ch-inserts

meybe You haven't to start new transactions for every save, meybe only one tr?
And Your app hangs. Any exceptions?


Top
 Profile  
 
 Post subject: Re: Hibernate calls in a loop
PostPosted: Wed Apr 27, 2011 6:04 pm 
Newbie

Joined: Tue Jul 19, 2005 1:28 pm
Posts: 17
Hi,

I will have to flush the session because there may be other threads that may query the database after insert, so all the inserts cannot be in one big transaction.

No, there are no exceptions, just the dao.save call hangs indefinitely.


Top
 Profile  
 
 Post subject: Re: Hibernate calls in a loop
PostPosted: Mon May 02, 2011 5:57 pm 
Newbie

Joined: Tue Jul 19, 2005 1:28 pm
Posts: 17
Can anyone please help me on this?


Top
 Profile  
 
 Post subject: Re: Hibernate calls in a loop
PostPosted: Wed May 04, 2011 11:21 am 
Newbie

Joined: Tue Jul 19, 2005 1:28 pm
Posts: 17
I fixed this problem.

The problem is in the way Spring and Hibernate manage sessions and transactions. After enabling a lot of logging information, I found that if there is any DAO call that is Spring transaction demarcated, and if you are using OpenSessionViewFilter, the sessions are not closed immediately after a transaction, rather they are put in a deferred close mode. In other words, the sessions are not closed and hence the underlying JDBC connection is not released. Since I am making calls in a loop, the transaction manager is creating session after session and not closing them fully, thus exhausting the jdbc pool. The solution to this is to set hibernate.connection.release_mode = after_transaction. This way the underlying JDBC connection is released as soon as the transaction is complete.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.