-->
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: When to close session on a J2EE application
PostPosted: Fri Mar 16, 2007 9:29 am 
Newbie

Joined: Thu Feb 22, 2007 6:44 am
Posts: 3
In our J2EE application, should we everytime we perform a hibernate request close the session? For instance, should we close it in the finally block, like in this code:

Code:
public KategoriData getKategoriById(String id) throws BaatkatalogenException {
   if (id == null)
       throw new BaatkatalogenException("id er null");

   Session session = HibernateUtil.getSessionFactory().getCurrentSession();
   session.beginTransaction();
   Criteria criteria = session.createCriteria(KategoriData.class);
   criteria.add(Expression.eq("id", id));
   criteria.setMaxResults(1);
   try {
       return (KategoriData) criteria.uniqueResult();
   } catch (HibernateException e) {
       throw new BaatkatalogenException ;
   } finally {
       if(session.isOpen()) {
      session.close();
       }
   }
    }



If not, then when should we close it?

Because I dont know when the user is finished using the application. I am using c3p0, and I have defined idle time period and timeout, will that result in closing the sessions when the idle time is finished?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 16, 2007 10:48 am 
Expert
Expert

Joined: Thu Sep 04, 2003 8:23 am
Posts: 368
Maybe you can use opensessioninviewfilter then your session will be close after your view is rendered if you are in a web based environment

Another way of doing is using a framework to handle transactions like Spring or EJB3 container. Thus this container will handle session management

_________________
Seb
(Please don't forget to give credits if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 16, 2007 10:59 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
BTW, if you need to load an object by id, use the get() or load() method instead of issuing a query (where you could use Restrictions.idEq() method instead of the Expression.eq()) and then setting maxResults to one (better off fetching unique results with the uniqueResult() method instead of the list() method/maxResults pairing).

Did not know if it was just an example you made up or if that was real production code.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 25, 2007 7:02 am 
C3P0 Developer
C3P0 Developer

Joined: Tue Jan 06, 2004 8:58 pm
Posts: 145
Quote:
I am using c3p0, and I have defined idle time period and timeout, will that result in closing the sessions when the idle time is finished?


c3p0's idle test period and timeout/maxIdleTime only affect Connections in the pool and not checked out. Connections associated with a hibernate Session are, from c3p0's perspective, checked out of the pool and in use by the client application.

c3p0 is written at a lower level than hibernate -- it doesn't know anything about Sessions. Your application should clean up the Sessions it works with -- the pool can't and won't do it for you, except in unusual circumstances and very rough ways (e.g. destroying Connections it considers leaked owing to an unreturnedConnectionTimeout).

good luck!

Steve


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.