-->
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: proper session managment
PostPosted: Mon Jan 09, 2006 2:33 pm 
Newbie

Joined: Fri Dec 17, 2004 12:54 pm
Posts: 13
Hi,
I'm using Hibernate in a web application framework and have a thread local object that has a method used by a lot of different classes during the request cycle to get a hibernate session.

In regards to this I'm stuck on something very simple. When a class requests a session does the thread local object call sessionfactory.createSession() for each getSession() request or does it create the session just once at the begining of the page request cycle and return a reference to that single session object in the getSession() method?

If a session should be created for each call to getSession() then what is the point of using a thread local object?

Thanks for any input.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 2:43 pm 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
No, it should just create a single Session possibly during the first get() or during some initilaization. Every other call should just use the
threadLocal.get() to obtain the Session that was created earlier. Refer to the tutorials(and caveatemptor) for concrete examples.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 4:16 pm 
Newbie

Joined: Fri Dec 17, 2004 12:54 pm
Posts: 13
Thanks for the info, from looking at the tomcat example with the HibernateUtil object I'm a little confused as to what a class should do when its done using a its share on the single session per thread. Does it call the close method on it? I guess what I'm getting at is it normal to have multiple open/close cycles on a single session in the thread local object? Thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 11:39 pm 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
krebsnet wrote:
Thanks for the info, from looking at the tomcat example with the HibernateUtil object I'm a little confused as to what a class should do when its done using a its share on the single session per thread. Does it call the close method on it? I guess what I'm getting at is it normal to have multiple open/close cycles on a single session in the thread local object? Thanks again.

Ideally you could add the session to the current thread of execution in a Servlet Filter. The filter then chains down to the next filter and so on and at the end, the request processing starts. You could then provide a finally block that will close the session. That way , you dont have to worry about application code closing/opening session. Stub code would be like this

Code:
public class HibernateFilter extends ServletFilter
{
      private SessionFactory factory;
      private ThreadLocal sessionHolder = new ThreadLocal();
     public void init(FilterConfig config)
    {
          //Creation the SessionFactory here
    } 
    public void doFilter(HttpServletRequest req,
                                     Httpservletresponse res,
                                     FilterChain chain)
      {
              try
             {
                  //Create the Session from SessionFactory  and bind it to sessionHolder
                 chain.doFilter(req,res,chain);
                 ....
             }
             finally
             {
                  //Close the Session
             }
      }
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 10, 2006 2:38 am 
Newbie

Joined: Fri Dec 17, 2004 12:54 pm
Posts: 13
Sweet: I think I have the plot now. Begining of the request, create a session off the singleton session factory, set the session in a thread local object. Let other classes during the request grab a hold of the session to query/insert data, then at the end of the request close the session regardless of success or exception. Thanks for the help.


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.