Top Description Fields Constructors Methods
org.springframework.orm.hibernate5.support

public Class OpenSessionInViewInterceptor

extends Object
implements AsyncWebRequestInterceptor
Class Inheritance
All Implemented Interfaces
org.springframework.web.context.request.AsyncWebRequestInterceptor, org.springframework.web.context.request.WebRequestInterceptor
Imports
org.apache.commons.logging.Log, .LogFactory, org.hibernate.FlushMode, .HibernateException, .Session, .SessionFactory, org.springframework.dao.DataAccessException, .DataAccessResourceFailureException, org.springframework.lang.Nullable, org.springframework.orm.hibernate5.SessionFactoryUtils, .SessionHolder, org.springframework.transaction.support.TransactionSynchronizationManager, org.springframework.ui.ModelMap, org.springframework.util.Assert, org.springframework.web.context.request.AsyncWebRequestInterceptor, .WebRequest, org.springframework.web.context.request.async.CallableProcessingInterceptor, .WebAsyncManager, .WebAsyncUtils

Spring web request interceptor that binds a Hibernate Session to 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 transactions via org.springframework.orm.hibernate5.HibernateTransactionManager as well as for non-transactional execution (if configured appropriately).

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.

Author
Juergen Hoeller
Since
4.2
See Also
OpenSessionInViewFilter, OpenSessionInterceptor, org.springframework.orm.hibernate5.HibernateTransactionManager, TransactionSynchronizationManager, SessionFactory#getCurrentSession()

Field Summary

Modifier and TypeField and Description
protected final Log
public static final String
PARTICIPATE_SUFFIX

Suffix that gets appended to the SessionFactory toString() representation for the "participate in existing session handling" request attribute.

private SessionFactory
sessionFactory

References Deprecated SessionFactory is deprecated or references (maybe indirectly) at least one deprecated element.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
afterCompletion(WebRequest
the current web request
request
,
Exception
exception thrown on handler execution, if any
ex
)

Implements org.springframework.web.context.request.WebRequestInterceptor.afterCompletion.

Unbind the Hibernate Session from the thread and close it.

public void
afterConcurrentHandlingStarted(WebRequest
the current request
request
)

Implements org.springframework.web.context.request.AsyncWebRequestInterceptor.afterConcurrentHandlingStarted.

Called instead of postHandle and afterCompletion, when the handler started handling the request concurrently.

private boolean
private boolean
protected String
getParticipateAttributeName()

Return the name of the request attribute that identifies that a request is already intercepted.

public SessionFactory
getSessionFactory()
References Deprecated SessionFactory is deprecated or references (maybe indirectly) at least one deprecated element.

Return the Hibernate SessionFactory that should be used to create Hibernate Sessions.

private SessionFactory
obtainSessionFactory()
References Deprecated SessionFactory is deprecated or references (maybe indirectly) at least one deprecated element.

protected Session

Returns:

the Session to use
openSession
()
References Deprecated Session is deprecated or references (maybe indirectly) at least one deprecated element.

Open a Session for the SessionFactory that this interceptor uses.

public void
postHandle(WebRequest
the current web request
request
,
ModelMap
the map of model objects that will be exposed to the view (may be null). Can be used to analyze the exposed model and/or to add further model attributes, if desired.
model
)

Implements org.springframework.web.context.request.WebRequestInterceptor.postHandle.

Intercept the execution of a request handler after its successful invocation, right before view rendering (if any).

public void
preHandle(WebRequest
the current web request
request
)

Implements org.springframework.web.context.request.WebRequestInterceptor.preHandle.

Open a new Hibernate Session according and bind it to the thread via the TransactionSynchronizationManager.

public void
setSessionFactory(SessionFactory sessionFactory)
References Deprecated SessionFactory is deprecated or references (maybe indirectly) at least one deprecated element.

Set the Hibernate SessionFactory that should be used to create Hibernate Sessions.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

loggerback to summary
protected final Log logger
PARTICIPATE_SUFFIXback to summary
public static final String PARTICIPATE_SUFFIX

Suffix that gets appended to the SessionFactory toString() representation for the "participate in existing session handling" request attribute.

See Also
getParticipateAttributeName
sessionFactoryback to summary
private SessionFactory sessionFactory

References Deprecated

SessionFactory is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Annotations
@Nullable

Constructor Detail

OpenSessionInViewInterceptorback to summary
public OpenSessionInViewInterceptor()

Method Detail

afterCompletionback to summary
public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException

Implements org.springframework.web.context.request.WebRequestInterceptor.afterCompletion.

Unbind the Hibernate Session from the thread and close it.

Parameters
request:WebRequest

Doc from org.springframework.web.context.request.WebRequestInterceptor.afterCompletion.

the current web request

ex:Exception

Doc from org.springframework.web.context.request.WebRequestInterceptor.afterCompletion.

exception thrown on handler execution, if any

Annotations
@Override
See Also
TransactionSynchronizationManager
afterConcurrentHandlingStartedback to summary
public void afterConcurrentHandlingStarted(WebRequest request)

Implements org.springframework.web.context.request.AsyncWebRequestInterceptor.afterConcurrentHandlingStarted.

Doc from org.springframework.web.context.request.AsyncWebRequestInterceptor.afterConcurrentHandlingStarted.

Called instead of postHandle and afterCompletion, when the handler started handling the request concurrently.

Parameters
request:WebRequest

the current request

Annotations
@Override
applySessionBindingInterceptorback to summary
private boolean applySessionBindingInterceptor(WebAsyncManager asyncManager, String key)
decrementParticipateCountback to summary
private boolean decrementParticipateCount(WebRequest request)
getParticipateAttributeNameback to summary
protected String getParticipateAttributeName()

Return the name of the request attribute that identifies that a request is already intercepted.

The default implementation takes the toString() representation of the SessionFactory instance and appends PARTICIPATE_SUFFIX.

getSessionFactoryback to summary
public SessionFactory getSessionFactory()

References Deprecated

SessionFactory is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Return the Hibernate SessionFactory that should be used to create Hibernate Sessions.

Annotations
@Nullable
obtainSessionFactoryback to summary
private SessionFactory obtainSessionFactory()

References Deprecated

SessionFactory is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

openSessionback to summary
protected Session openSession() throws DataAccessResourceFailureException

References Deprecated

Session is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Open a Session for the SessionFactory that this interceptor uses.

The default implementation delegates to the SessionFactory#openSession method and sets the Session's flush mode to "MANUAL".

Returns:Session

the Session to use

Exceptions
DataAccessResourceFailureException:
if the Session could not be created
See Also
FlushMode#MANUAL
postHandleback to summary
public void postHandle(WebRequest request, ModelMap model)

Implements org.springframework.web.context.request.WebRequestInterceptor.postHandle.

Doc from org.springframework.web.context.request.WebRequestInterceptor.postHandle.

Intercept the execution of a request handler after its successful invocation, right before view rendering (if any).

Allows for modifying context resources after successful handler execution (for example, flushing a Hibernate Session).

Parameters
request:WebRequest

the current web request

model:ModelMap

the map of model objects that will be exposed to the view (may be null). Can be used to analyze the exposed model and/or to add further model attributes, if desired.

Annotations
@Override
preHandleback to summary
public void preHandle(WebRequest request) throws DataAccessException

Implements org.springframework.web.context.request.WebRequestInterceptor.preHandle.

Open a new Hibernate Session according and bind it to the thread via the TransactionSynchronizationManager.

Parameters
request:WebRequest

Doc from org.springframework.web.context.request.WebRequestInterceptor.preHandle.

the current web request

Annotations
@Override
setSessionFactoryback to summary
public void setSessionFactory(SessionFactory sessionFactory)

References Deprecated

SessionFactory is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Set the Hibernate SessionFactory that should be used to create Hibernate Sessions.