如何在Spring事务管理中调用自定义回滚方法? [英] How to call a custom rollback method in Spring Transaction Management?

查看:763
本文介绍了如何在Spring事务管理中调用自定义回滚方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:春季3,自定义事务管理,JDBC事务

Environment: Spring 3, Custom Transaction Management, JDBC Transactions

我刚刚阅读了有关使用事务模板处理事务管理的Spring文档. 这似乎太复杂了,所以我想问:

I just read the Spring docs on using the transaction template to handle transaction management. It seemed overly complex so I want to ask:

我的大多数事务与JDBC相关,这意味着我只是在服务上声明了@Transactional.但是现在我正在对另一个站点进行REST服务调用,如果以下任何JDBC操作失败,则需要回滚,在这种情况下,我将提供回滚代码.

Most of my transactions are JDBC related, meaning I just declare an @Transactional on my service. But now I am making a REST service call to another site which needs to rollback if any of the following JDBC operations fail, I'll provide the rollback code in this case.

随着方法的发展,在事务中-我想保存对REST服务调用的引用(需要回滚该操作),并且在例外情况下,我myCustomRollback()称为的方法,可以访问先前存储的对象.

As I progress in my method, in my transaction - I want to save a reference to the REST service call (needed to roll back that action), and upon exception I just want a method myCustomRollback() called which can access the previously stored object.

为什么不只在transactionTemplate中提供一个用于存储内容的映射并在@Transactional注释上定义一个自定义回滚方法?

Why not just provide a map in the transactionTemplate for storing stuff and define a custom rollback method on the @Transactional annotation?

这是我的思考方式,我没有遵循Spring的思考方式.有人可以帮助我弥合我想要的东西与如何在春季最有效地完成它之间的鸿沟吗?我只需要执行一些特殊情况操作即可.

This is the way I think about it, I'm not following the way Spring thinks about this. Can someone help me bridge the gap between what I want and how I accomplish it most efficiently in Spring? I only need to do this for a few special case operations.

推荐答案

您可以使用AfterThrowing建议(当引发异常时)&在此处调用您的方法(myCustmRollback()),则可以使用TransactionSynchronizationManager类获取当前交易记录&回滚...

you can use the AfterThrowing advice (when an exception is thrown) & call your method (myCustmRollback()) there, you can use TransactionSynchronizationManager class to get thecurrent transaction & roll it back...

或者,您可以使用AroundAdvice开始&提交/回滚事务(这样,您可以通过TransactionSynchronizationManager类使用spring提供的事务管理器)

alternatively.. you can use the AroundAdvice to begin & commit/rollback your transaction (this way you can use the spring provided transaction manager by using the TransactionSynchronizationManager class)

这篇关于如何在Spring事务管理中调用自定义回滚方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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