Spring hibernate , how to call some method after transaction commit or transaction rollback
hibernate, java, spring, transactions
Solution
From Hibernate, you could extends `EmptyInterceptor` and override `afterTransactionCompletion()` method and register it in `SessionFactoryBean` or `HibernateTransactionManager`.
From Spring you could extends `TransactionSynchronizationAdapter` and override `afterCompletion()` and register when appropriate with `TransactionSynchronizationManager#registerSynchronization()`.
Edit
An Example of using Spring Aop to add a synchronization to all methods annotated with `@Transactional`
@Aspect
class TransactionAspect extends TransactionSynchronizationAdapter {
@Before("@annotation(org.springframework.transaction.annotation.Transactional)")
public void registerTransactionSyncrhonization() {
TransactionSynchronizationManager.registerSynchronization(this);
}
@Override
public void afterCompletion(int status) {
// code
}
}
Problem
I need to call some method after transaction succes or rollback. I am using as ``` <bean name="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor"> <property name="sessionFactory"> <ref local="mysessionFactory"/> </property> </bean> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="mysessionFactory"/> </property> </bean> <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/> ``` The application use some external web services which needs to be "cleaned" when the internal transaction gets rollbacked. Is there way how to accomplish this without using declarative transaction management.