|
[Deprecated API] | ||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | CURRENT API | |||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Object | +--com.interface21.orm.hibernate.HibernateInterceptor
This interceptor binds a new Hibernate Session to the thread before a method call, closing and removing it afterwards in case of any method outcome. If there already was a pre-bound Session (e.g. from HibernateTransactionManager, or from a surrounding Hibernate-intercepted method), the interceptor simply takes part in it.
Application code must retrieve a Hibernate Session via SessionFactoryUtils' getSession method, to be able to detect a thread-bound Session. It is preferable to use getSession with allowCreate=false, as the code relies on the interceptor to provide proper Session handling. Typically the code will look as follows:
public void doHibernateAction() {
Session session = SessionFactoryUtils.getSession(this.sessionFactory, false);
try {
...
}
catch (HibernateException ex) {
throw SessionFactoryUtils.convertHibernateAccessException(ex);
}
}
Note that the application must care about handling HibernateExceptions itself, preferably via delegating to SessionFactoryUtils' convertHibernateAccessException that converts them to ones that are compatible with the com.interface21.dao exception hierarchy (like HibernateTemplate does).
Unfortunately, this interceptor cannot convert checked HibernateExceptions to unchecked dao ones automatically. The intercepted method would have to throw HibernateException to be able to achieve this - thus the caller would still have to catch or rethrow it, even if it will never be thrown if intercepted.
This class can be considered a declarative alternative to HibernateTemplate's callback approach. The advantages are:
Note: This class, like all of Spring's Hibernate support, requires Hibernate 2.0 (initially developed with RC1).
SessionFactoryUtils.getSession(net.sf.hibernate.SessionFactory, boolean),
HibernateTransactionManager,
HibernateTemplate| Constructor Summary | |
HibernateInterceptor()
|
|
| Method Summary | |
java.lang.Object |
invoke(org.aopalliance.MethodInvocation methodInvocation)
|
void |
setForceFlush(boolean forceFlush)
If a flush of the Hibernate Session should be forced after executing the target method. |
void |
setSessionFactory(net.sf.hibernate.SessionFactory sessionFactory)
Set the SessionFactory that this instance should create thread-bound Sessions for. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
public HibernateInterceptor()
| Method Detail |
public void setSessionFactory(net.sf.hibernate.SessionFactory sessionFactory)
public void setForceFlush(boolean forceFlush)
A forced flush leads to immediate synchronization with the database, even if in a Hibernate transaction. This causes inconsistencies to show up and throw a respective exception immediately. But the drawbacks are:
public java.lang.Object invoke(org.aopalliance.MethodInvocation methodInvocation)
throws java.lang.Throwable
invoke in interface org.aopalliance.MethodInterceptor
|
[Deprecated API] | ||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | CURRENT API | |||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||