Class OpenSessionInterceptor
java.lang.Object
org.springframework.orm.hibernate5.support.OpenSessionInterceptor
- All Implemented Interfaces:
Advice
,Interceptor
,MethodInterceptor
,InitializingBean
Simple AOP Alliance
MethodInterceptor
implementation that binds a new
Hibernate Session
for each method invocation, if none bound before.
This is a simple Hibernate Session scoping interceptor along the lines of
OpenSessionInViewInterceptor
, just for use with AOP setup instead of
MVC setup. It opens a new Session
with flush mode "MANUAL" since the
Session is only meant for reading, except when participating in a transaction.
- Since:
- 4.2
- Author:
- Juergen Hoeller
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.Return the Hibernate SessionFactory that should be used to create Hibernate Sessions.invoke
(MethodInvocation invocation) Implement this method to perform extra treatments before and after the invocation.protected Session
openSession
(SessionFactory sessionFactory) Open a Session for the given SessionFactory.void
setSessionFactory
(SessionFactory sessionFactory) Set the Hibernate SessionFactory that should be used to create Hibernate Sessions.
-
Constructor Details
-
OpenSessionInterceptor
public OpenSessionInterceptor()
-
-
Method Details
-
setSessionFactory
Set the Hibernate SessionFactory that should be used to create Hibernate Sessions. -
getSessionFactory
Return the Hibernate SessionFactory that should be used to create Hibernate Sessions. -
afterPropertiesSet
public void afterPropertiesSet()Description copied from interface:InitializingBean
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
- Specified by:
afterPropertiesSet
in interfaceInitializingBean
-
invoke
Description copied from interface:MethodInterceptor
Implement this method to perform extra treatments before and after the invocation. Polite implementations would certainly like to invokeJoinpoint.proceed()
.- Specified by:
invoke
in interfaceMethodInterceptor
- Parameters:
invocation
- the method invocation joinpoint- Returns:
- the result of the call to
Joinpoint.proceed()
; might be intercepted by the interceptor - Throws:
Throwable
- if the interceptors or the target object throws an exception
-
openSession
protected Session openSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException Open a Session for the given SessionFactory.The default implementation delegates to the
SessionFactory.openSession()
method and sets theSession
's flush mode to "MANUAL".- Parameters:
sessionFactory
- the SessionFactory to use- Returns:
- the Session to use
- Throws:
DataAccessResourceFailureException
- if the Session could not be created- Since:
- 5.0
- See Also:
-