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.  [ 4 posts ] 
Author Message
 Post subject: Idle connections with c3p0 pooling - is it normal?
PostPosted: Fri Aug 31, 2007 11:02 am 
Beginner
Beginner

Joined: Wed May 23, 2007 1:07 pm
Posts: 28
I am using hibernate in a JSP application deployed in Tomcat.

I currently have the following pool configuration:

C3P0 parameters:
<property name="hibernate.c3p0.min_size">2</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.max_statements">50</property>

Code between sessionFactory.openSession() and session.close():
Code:
try
      {         
         _currentSession.beginTransaction();         
         Query allItems = _currentSession.createQuery(query);
         list = allItems.list();         
         _currentSession.getTransaction().commit();
               
}
      catch (HibernateException e) {
         _currentSession.getTransaction().rollback();
         SQLException sqlexc = new SQLException();
         sqlexc.setStackTrace(e.getStackTrace());
         throw new DataException(e.getMessage(),sqlexc);         
      }
      finally
      {
         flushSession();
         releaseResources();
      }


where the flush and releaseresources methods are:

Code:
private void flushSession()
   {
      try
      {
         if(_currentSession != null)
         {
            _currentSession.flush();
         }
      }
      catch (HibernateException e) {
         // TODO: handle exception
      }
   }

   public void releaseResources() {
      
      try {

            if (_currentSession != null) {
               //_currentSession.connection().close();               
               _currentSession.close();
               flushSession();
               _currentSession = null;
            }

      } catch (HibernateException e) {
         // TODO: handle exception
      }

   }


My problem is that, having a look at the postgres processes created (for example in PgAdminIII, I see that, for each call of this query, I get one to three processes with

Current Query - <IDLE>

I am confused and don't understand what's happening - isn't the connection pool supposed to reuse connections instead of creating new ones? It seems these Idle processes just accumulate forever.

Any help greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 31, 2007 12:07 pm 
Beginner
Beginner

Joined: Wed May 23, 2007 1:07 pm
Posts: 28
On a follow up to the question, can this be related with the fact that I'm creating a session factory on each request, like so:

Code:
   public HibernateSOPManager() {
      Configuration configuration = new Configuration();
      configuration.configure();   

      try {
         _sessionFactory = configuration.buildSessionFactory();
         
         
      } catch (HibernateException e) {
         throw new IllegalArgumentException("Cannot build session factory: " + e);
      }   


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 04, 2007 3:53 am 
Regular
Regular

Joined: Fri May 12, 2006 4:05 am
Posts: 106
wild_oscar wrote:
On a follow up to the question, can this be related with the fact that I'm creating a session factory on each request, like so:


Yep, I'm pretty sure that's the point. I'm not 100% certain, but I think that when using c3p0 each SessionFactory will build its own connection-pool, so my guess is you should get at least 2 (your c3p0 min_size) new connections for each SessionFactory built.
It also seems you don't close() your SessionFactory when you're done with it, cause this should release the acquired resources (so the connections should disappear).

But using a new SessionFactory per request is bad practice anyway, because building SessionFactories is really expensive - in fact it's running the complete hibernate-startup including parsing of all mapping- and configuration-files etc. Also SessionFactories use a considerable amount of resources (e.g. memory), so your current application should leak those resources heavily because of unused SessionFactories.

You should try to generate only one SessionFactory and reuse it. You could register it with JNDI or keep it as a Singleton in some HibernateUtil-class or smoething like that.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 04, 2007 5:18 am 
Beginner
Beginner

Joined: Wed May 23, 2007 1:07 pm
Posts: 28
piet.t wrote:
wild_oscar wrote:
On a follow up to the question, can this be related with the fact that I'm creating a session factory on each request, like so:


Yep, I'm pretty sure that's the point. I'm not 100% certain, but I think that when using c3p0 each SessionFactory will build its own connection-pool, so my guess is you should get at least 2 (your c3p0 min_size) new connections for each SessionFactory built.
It also seems you don't close() your SessionFactory when you're done with it, cause this should release the acquired resources (so the connections should disappear).

But using a new SessionFactory per request is bad practice anyway, because building SessionFactories is really expensive - in fact it's running the complete hibernate-startup including parsing of all mapping- and configuration-files etc. Also SessionFactories use a considerable amount of resources (e.g. memory), so your current application should leak those resources heavily because of unused SessionFactories.

You should try to generate only one SessionFactory and reuse it. You could register it with JNDI or keep it as a Singleton in some HibernateUtil-class or smoething like that.


Thank you for the input.
In the meantime I followed my huntch and I believe I've solved the issue.

I am using Struts 2 + Hibernate, so I created an Interceptor responsible for putting the SessionFactory in the application context map, so it is only created once for the application.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.