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
}
}
}