public class OpenSessionInViewInterceptor extends java.lang.Object implements WebRequestInterceptor
Sessionto the thread for the entire processing of the request.
This class is a concrete expression of the "Open Session in View" pattern, which is a pattern that allows for the lazy loading of associations in web views despite the original transactions already being completed.
This interceptor makes Hibernate Sessions available via the current thread,
which will be autodetected by transaction managers. It is suitable for service layer
as well as for non-transactional execution (if configured appropriately).
NOTE: This interceptor will by default not flush the Hibernate
Session, with the flush mode being set to
It assumes that it will be used in combination with service layer transactions
that handle the flushing: the active transaction manager will temporarily change
the flush mode to
FlushMode.AUTO during a read-write transaction,
with the flush mode reset to
FlushMode.NEVER at the end of each
transaction. If you intend to use this interceptor without transactions, consider
changing the default flush mode (through the
In contrast to
OpenSessionInViewFilter, this interceptor is configured
in a Spring application context and can thus take advantage of bean wiring..
WARNING: Applying this interceptor 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 with already
loaded instances of the same objects.
|Modifier and Type||Field and Description|
Suffix that gets appended to the
|Constructor and Description|
|Modifier and Type||Method and Description|
Unbind the Hibernate
Return the name of the request attribute that identifies that a request is already intercepted.
Open a Session for the SessionFactory that this interceptor uses.
Intercept the execution of a request handler after its successful invocation, right before view rendering (if any).
Open a new Hibernate
public static final java.lang.String PARTICIPATE_SUFFIX
toString()representation for the "participate in existing session handling" request attribute.
protected final Log logger
private SessionFactory sessionFactory
public void setSessionFactory(SessionFactory sessionFactory)
public SessionFactory getSessionFactory()
public void preHandle(WebRequest request) throws DataAccessException
Sessionaccording to the settings of this
HibernateAccessorand bind it to the thread via the
public void postHandle(WebRequest request, ModelMap model)
Allows for modifying context resources after successful handler execution (for example, flushing a Hibernate Session).
public void afterCompletion(WebRequest request, java.lang.Exception ex) throws DataAccessException
Sessionfrom the thread and close it).
protected Session openSession() throws DataAccessResourceFailureException
The default implementation delegates to the
SessionFactory.openSession method and
Session's flush mode to "MANUAL".
DataAccessResourceFailureException- if the Session could not be created
protected java.lang.String getParticipateAttributeName()
The default implementation takes the
SessionFactory instance and appends