如何获取Spring事务管理器实例

如何获取Spring事务管理器实例

本文介绍了如何获取Spring事务管理器实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用注解来标记应该在事务中执行的方法.

I use annotations to mark methods which should be executed in a transaction.

但是,在一个地方我需要手动执行 transactionManager.rollback(),没有注释.如何获取 transactionManager 对象?

But, in one place I need to do transactionManager.rollback() manually, without annotation. How can I obtain transactionManager object?

推荐答案

如果要回滚当前事务,可以使用

If you want to rollback the current transaction, you may use

    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

请注意,它不会立即回滚事务 - 它设置了仅回滚"状态,因此在尝试提交期间事务将被回滚.否则,如果您需要程序化事务划分,您可以使用 TransactionTemplate,如 10.6 程序化事务管理.

Note that it doesn't rollback the transaction immediately - it sets the "rollback only" status, so transaction will be rolled back during attempt to commit.


Otherwise, if you need a programmatic transaction demaracation, you may use TransactionTemplate, as described in 10.6 Programmatic transaction management.

您也可以获取PlatformTransactionManager 的实例,但它并未被广泛使用,因为TransactionTemplate 是程序化事务划分的推荐方法.

Also you can obtain an instance of PlatformTransactionManager, but it's not widely used since TransactionTemplate is a recommended approach for programmatic transaction demaracation.

另见:

这篇关于如何获取Spring事务管理器实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 06:55