public class OpenSessionInterceptor extends java.lang.Object implements MethodInterceptor, InitializingBean
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.
OpenSessionInViewInterceptor
,
OpenSessionInViewFilter
,
HibernateTransactionManager
,
TransactionSynchronizationManager
,
org.hibernate.SessionFactory#getCurrentSession()
Modifier and Type | Field and Description |
---|---|
private SessionFactory |
sessionFactory |
Constructor and Description |
---|
OpenSessionInterceptor() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Invoked by a BeanFactory after it has set all bean properties supplied
(and satisfied BeanFactoryAware and ApplicationContextAware).
|
SessionFactory |
getSessionFactory()
Return the Hibernate SessionFactory that should be used to create Hibernate Sessions.
|
java.lang.Object |
invoke(MethodInvocation invocation)
Implement this method to perform extra treatments before and
after the invocation.
|
protected Session |
openSession()
Open a Session for the SessionFactory that this interceptor uses.
|
void |
setSessionFactory(SessionFactory sessionFactory)
Set the Hibernate SessionFactory that should be used to create Hibernate Sessions.
|
public void setSessionFactory(SessionFactory sessionFactory)
public SessionFactory getSessionFactory()
public void afterPropertiesSet()
InitializingBean
This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.
afterPropertiesSet
in interface InitializingBean
public java.lang.Object invoke(MethodInvocation invocation) throws java.lang.Throwable
MethodInterceptor
Joinpoint.proceed()
.invoke
in interface MethodInterceptor
invocation
- the method invocation joinpointJoinpoint.proceed()
;
might be intercepted by the interceptorjava.lang.Throwable
- if the interceptors or the target object
throws an exceptionprotected Session openSession() throws DataAccessResourceFailureException
The default implementation delegates to the SessionFactory#openSession
method and sets the Session
's flush mode to "MANUAL".
DataAccessResourceFailureException
- if the Session could not be createdorg.hibernate.FlushMode#MANUAL