[英]How to obtain Spring transaction manager instance?
I use annotations to mark methods which should be executed in a transaction. 我使用注释来标记应该在事务中执行的方法。
But, in one place I need to do transactionManager.rollback()
manually, without annotation. 但是,在一个地方我需要手动执行
transactionManager.rollback()
,而不需要注释。 How can I obtain transactionManager
object? 如何获取
transactionManager
对象?
If you want to rollback the current transaction, you may use 如果要回滚当前事务,可以使用
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
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. 请注意,它不会立即回滚事务 - 它设置“仅回滚”状态,因此在尝试提交期间将回滚事务。
TransactionTemplate
, as described in 10.6 Programmatic transaction management .
TransactionTemplate
,如10.6程序化事务管理中所述 。
Also you can obtain an instance of PlatformTransactionManager
, but it's not widely used since TransactionTemplate
is a recommended approach for programmatic transaction demaracation. 您还可以获取
PlatformTransactionManager
的实例,但由于TransactionTemplate
是程序化事务处理拆除的推荐方法,因此它没有被广泛使用。
See also: 也可以看看:
如果您的对象是由Spring配置的,那么您当然可以将一个事务管理器注入其中......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.