Spring hibernate,如何在事务提交或事务回滚之后调用某种方法 [英] Spring hibernate , how to call some method after transaction commit or transaction rollback

查看:802
本文介绍了Spring hibernate,如何在事务提交或事务回滚之后调用某种方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在事务成功或回滚之后,我需要调用一些方法。我正在使用

 < bean name =openSessionInViewInterceptorclass =org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor > 
< property name =sessionFactory>
< ref local =mysessionFactory/>
< / property>
< / bean>

< bean id =txManagerclass =org.springframework.orm.hibernate3.HibernateTransactionManager>
< property name =sessionFactory>
< ref local =mysessionFactory/>
< / property>
< / bean>

< tx:注解驱动的事务管理器=txManagerproxy-target-class =true/>

应用程序使用一些外部Web服务,当内部事务回滚时需要清除这些服务。
如何在不使用声明式事务管理的情况下完成此任务。

解决方案


  • 从Hibernate开始,您可以扩展 code>并覆盖
    afterTransactionCompletion()方法并将其注册到
    SessionFactoryBean HibernateTransactionManager

  • 从Spring可以扩展 TransactionSynchronizationAdapter 覆盖 afterCompletion(),并且在适当时用
    进行注册TransactionSynchronizationManager#registerSynchronization()

    $ p








    $ b $

    使用Spring Aop为所有使用 @Transactional

    @Aspect $ b $ class TransactionAspect extends TransactionSynchronizationAdapter {

    @Before(@ annotation(org.springframework.transaction.annotation.Transactional))
    public void registerTransactionSyncrho nization(){
    TransactionSynchronizationManager.registerSynchronization(this);

    }

    @Override
    public void afterCompletion(int status){
    // code
    }
    }


    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.

    解决方案

    • 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 
        }
    }
    

    这篇关于Spring hibernate,如何在事务提交或事务回滚之后调用某种方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆