|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.web.filter.GenericFilterBean org.springframework.web.filter.OncePerRequestFilter org.springframework.orm.hibernate.support.OpenSessionInViewFilter
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.
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 |
public static final java.lang.String DEFAULT_SESSION_FACTORY_BEAN_NAME
Constructor Detail |
public OpenSessionInViewFilter()
Method Detail |
public void setSessionFactoryBeanName(java.lang.String sessionFactoryBeanName)
protected java.lang.String getSessionFactoryBeanName()
protected java.lang.String getAlreadyFilteredAttributeName()
getAlreadyFilteredAttributeName
in class OncePerRequestFilter
lookupSessionFactory()
protected void doFilterInternal(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.FilterChain filterChain) throws javax.servlet.ServletException, java.io.IOException
OncePerRequestFilter
doFilterInternal
in class OncePerRequestFilter
javax.servlet.ServletException
java.io.IOException
protected net.sf.hibernate.SessionFactory lookupSessionFactory()
getSessionFactoryBeanName()
protected net.sf.hibernate.Session getSession(net.sf.hibernate.SessionFactory sessionFactory) throws DataAccessResourceFailureException
Can be overridden in subclasses for creating a Session with a custom entity interceptor or JDBC exception translator.
sessionFactory
- the SessionFactory that this filter uses
DataAccessResourceFailureException
- if the Session could not be createdSessionFactoryUtils.getSession(SessionFactory, boolean)
protected void closeSession(net.sf.hibernate.Session session, net.sf.hibernate.SessionFactory sessionFactory) throws CleanupFailureDataAccessException
Can be overridden in subclasses, e.g. for flushing the Session before closing it. See class-level javadoc for a discussion of flush handling.
session
- the Session used for filteringsessionFactory
- the SessionFactory that this filter uses
DataAccessResourceFailureException
- if the Session could not be closed
CleanupFailureDataAccessException
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |