org.davesag.generica.servlet.filter
Class PersistenceManagementFilter

java.lang.Object
  extended byorg.davesag.generica.servlet.filter.PersistenceManagementFilter
All Implemented Interfaces:
Filter

public class PersistenceManagementFilter
extends Object
implements Filter

Filter which manages a ThreadLocal hibernate session. Obtain the session by calling Persistance.getSession(somesessionfactory).

Since:
Generica 1.0
Version:
CVS Revision $Id: PersistenceManagementFilter.java,v 1.8 2004/03/30 13:00:42 davesag Exp $
Author:
Dave Sag http://www.davesag.com, based on code written by Jeff Schnitzer

Field Summary
protected static ThreadLocal HIBERNATE
          Holds the current hibernate session, if one has been created.
 
Constructor Summary
PersistenceManagementFilter()
           
 
Method Summary
 void destroy()
          does nothing.
 void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
          open a new hibernate session if one has not already been opened, and then close it after the request has been handled.
static Session getSession(SessionFactory factory)
          Gets a session that will be closed by this filter at the end of the request.
 void init(FilterConfig filterConfig)
          does nothing.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HIBERNATE

protected static ThreadLocal HIBERNATE
Holds the current hibernate session, if one has been created.

Constructor Detail

PersistenceManagementFilter

public PersistenceManagementFilter()
Method Detail

init

public void init(FilterConfig filterConfig)
          throws ServletException
does nothing.

Specified by:
init in interface Filter
Parameters:
filterConfig - the filter config object
Throws:
ServletException - if anything went wrong.

doFilter

public void doFilter(ServletRequest request,
                     ServletResponse response,
                     FilterChain chain)
              throws IOException,
                     ServletException
open a new hibernate session if one has not already been opened, and then close it after the request has been handled.

Specified by:
doFilter in interface Filter
Parameters:
request - the http request
response - the http response
chain - the next filter in the chain.
Throws:
IOException - if anything went wrong with io.
ServletException - if anythign else went wrong.
IllegalStateException - if there is already a hibernate session with this thread.

getSession

public static Session getSession(SessionFactory factory)
                          throws HibernateException
Gets a session that will be closed by this filter at the end of the request. NOTE: ONLY ever call this method from within the context of a servlet request (specifically, one that has been associated with this filter). If you want a Hibernate session at some other time, call getSessionFactory() and open/close the session yourself.

Parameters:
factory - The session factory to use to create this session.
Returns:
an appropriate Hibernate Session object.
Throws:
HibernateException - if there was a problem.

destroy

public void destroy()
does nothing.

Specified by:
destroy in interface Filter


Dave Sag Generica API version 0.5.9 - (prerelease) Copyright 2003 - 2004 Dave Sag.