org.springframework.orm.hibernate.support
Class OpenSessionInViewFilter

java.lang.Object
  extended byorg.springframework.web.filter.GenericFilterBean
      extended byorg.springframework.web.filter.OncePerRequestFilter
          extended byorg.springframework.orm.hibernate.support.OpenSessionInViewFilter
All Implemented Interfaces:
javax.servlet.Filter

public class OpenSessionInViewFilter
extends OncePerRequestFilter

Servlet 2.3 Filter that binds a Hibernate Session to the thread for the whole processing of the request. Intended for the "Open Session in View" pattern, i.e. to allow for lazy loading in web views despite the original transactions already being completed.

This filter works similar to the AOP HibernateInterceptor: It just makes Hibernate Sessions available via the thread. It is suitable for non-transactional execution but also for middle tier transactions via HibernateTransactionManager or JtaTransactionManager. In the latter case, Sessions pre-bound by this filter will automatically be used for the transactions and flushed accordingly.

WARNING: Applying this filter to existing logic can cause issues that have not appeared before, through the use of a single Hibernate Session for the processing of an entire request. In particular, the reassociation of persistent objects with a Hibernate Session has to occur at the very beginning of request processing, to avoid clashes will already loaded instances of the same objects.

Looks up the SessionFactory in Spring's root web application context. Supports a "sessionFactoryBeanName" filter init-param; the default bean name is "sessionFactory". Looks up the SessionFactory on each request, to avoid initialization order issues (if using ContextLoaderServlet, the root application context will get initialized after this filter).

NOTE: This filter will by default not flush the Hibernate session, as it assumes to be used in combination with middle tier transactions that care for the flushing, or HibernateAccessors with flushMode FLUSH_EAGER. If you want this filter to flush after completed request processing, override closeSession and invoke flush on the Session before closing it.

Since:
06.12.2003
Author:
Juergen Hoeller
See Also:
OpenSessionInViewInterceptor, closeSession(net.sf.hibernate.Session, net.sf.hibernate.SessionFactory), HibernateInterceptor, HibernateTransactionManager, SessionFactoryUtils.getSession(net.sf.hibernate.SessionFactory, boolean), TransactionSynchronizationManager

Field Summary
static java.lang.String DEFAULT_SESSION_FACTORY_BEAN_NAME
           
 
Fields inherited from class org.springframework.web.filter.OncePerRequestFilter
ALREADY_FILTERED_SUFFIX
 
Fields inherited from class org.springframework.web.filter.GenericFilterBean
logger
 
Constructor Summary
OpenSessionInViewFilter()
           
 
Method Summary
protected  void closeSession(net.sf.hibernate.Session session, net.sf.hibernate.SessionFactory sessionFactory)
          Close the given Session.
protected  void doFilterInternal(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.FilterChain filterChain)
          Same contract as for doFilter, but guaranteed to be just invoked once per request.
protected  java.lang.String getAlreadyFilteredAttributeName()
          This implementation appends the SessionFactory bean name to the class name, to be executed one per SessionFactory.
protected  net.sf.hibernate.Session getSession(net.sf.hibernate.SessionFactory sessionFactory)
          Get a Session for the SessionFactory that this filter uses.
protected  java.lang.String getSessionFactoryBeanName()
          Return the bean name of the SessionFactory to fetch from Spring's root application context.
protected  net.sf.hibernate.SessionFactory lookupSessionFactory()
          Look up the SessionFactory that this filter should use.
 void setSessionFactoryBeanName(java.lang.String sessionFactoryBeanName)
          Set the bean name of the SessionFactory to fetch from Spring's root application context.
 
Methods inherited from class org.springframework.web.filter.OncePerRequestFilter
doFilter, shouldNotFilter
 
Methods inherited from class org.springframework.web.filter.GenericFilterBean
addRequiredProperty, destroy, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setFilterConfig
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_SESSION_FACTORY_BEAN_NAME

public static final java.lang.String DEFAULT_SESSION_FACTORY_BEAN_NAME
See Also:
Constant Field Values
Constructor Detail

OpenSessionInViewFilter

public OpenSessionInViewFilter()
Method Detail

setSessionFactoryBeanName

public void setSessionFactoryBeanName(java.lang.String sessionFactoryBeanName)
Set the bean name of the SessionFactory to fetch from Spring's root application context.


getSessionFactoryBeanName

protected java.lang.String getSessionFactoryBeanName()
Return the bean name of the SessionFactory to fetch from Spring's root application context.


getAlreadyFilteredAttributeName

protected java.lang.String getAlreadyFilteredAttributeName()
This implementation appends the SessionFactory bean name to the class name, to be executed one per SessionFactory. Can be overridden in subclasses, e.g. when also overriding lookupSessionFactory.

Overrides:
getAlreadyFilteredAttributeName in class OncePerRequestFilter
See Also:
lookupSessionFactory()

doFilterInternal

protected void doFilterInternal(javax.servlet.http.HttpServletRequest request,
                                javax.servlet.http.HttpServletResponse response,
                                javax.servlet.FilterChain filterChain)
                         throws javax.servlet.ServletException,
                                java.io.IOException
Description copied from class: OncePerRequestFilter
Same contract as for doFilter, but guaranteed to be just invoked once per request. Provides HttpServletRequest and HttpServletResponse arguments instead of the default ServletRequest and ServletResponse ones.

Specified by:
doFilterInternal in class OncePerRequestFilter
Throws:
javax.servlet.ServletException
java.io.IOException

lookupSessionFactory

protected net.sf.hibernate.SessionFactory lookupSessionFactory()
Look up the SessionFactory that this filter should use. The default implementation looks for a bean with the specified name in Spring's root application context.

Returns:
the SessionFactory to use
See Also:
getSessionFactoryBeanName()

getSession

protected net.sf.hibernate.Session getSession(net.sf.hibernate.SessionFactory sessionFactory)
                                       throws DataAccessResourceFailureException
Get a Session for the SessionFactory that this filter uses. The default implementation invokes SessionFactoryUtils.getSession, and sets the Session's flushMode to NEVER.

Can be overridden in subclasses for creating a Session with a custom entity interceptor or JDBC exception translator.

Parameters:
sessionFactory - the SessionFactory that this filter uses
Returns:
the Session to use
Throws:
DataAccessResourceFailureException - if the Session could not be created
See Also:
SessionFactoryUtils.getSession(SessionFactory, boolean)

closeSession

protected void closeSession(net.sf.hibernate.Session session,
                            net.sf.hibernate.SessionFactory sessionFactory)
                     throws CleanupFailureDataAccessException
Close the given Session. The default implementation invokes SessionFactoryUtils.closeSessionIfNecessary.

Can be overridden in subclasses, e.g. for flushing the Session before closing it. See class-level javadoc for a discussion of flush handling.

Parameters:
session - the Session used for filtering
sessionFactory - the SessionFactory that this filter uses
Throws:
DataAccessResourceFailureException - if the Session could not be closed
CleanupFailureDataAccessException


Copyright (C) 2003-2004 The Spring Framework Project.