java 如何获取Spring事务管理器实例?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4123355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to obtain Spring transaction manager instance?
提问by Roman
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. How can I obtain transactionManager
object?
但是,在一个地方我需要transactionManager.rollback()
手动完成,没有注释。如何获取transactionManager
对象?
回答by axtavt
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
TransactionTemplate
,如中所述10.6 Programmatic transaction management10.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:
也可以看看:
回答by Konstantin
If your object is configured by Spring you could off course inject a transactionmanager into it...
如果你的对象是由 Spring 配置的,你当然可以将一个事务管理器注入它......