org.springframework.orm.hibernate.support
Class OpenSessionInViewFilter

org.springframework.web.filter.GenericFilterBean
  |
  +--org.springframework.web.filter.OncePerRequestFilter
        |
        +--org.springframework.orm.hibernate.support.OpenSessionInViewFilter

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. The latter will automatically use Sessions pre-bound by this filter.

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.

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

Field Summary
 final static StringDEFAULT_SESSION_FACTORY_BEAN_NAME
           

Fields inherited from class org.springframework.web.filter.OncePerRequestFilter
ALREADY_FILTERED_SUFFIX

Method Summary
 voidsetSessionFactoryBeanName(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

Methods inherited from class org.springframework.web.filter.GenericFilterBean
destroy, init

Field Detail

DEFAULT_SESSION_FACTORY_BEAN_NAME

public final static String DEFAULT_SESSION_FACTORY_BEAN_NAME
Method Detail

setSessionFactoryBeanName

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

Association Links

to Class java.lang.String

to Class java.lang.String