如何获取Spring事务管理器实例?
我使用注释来标记应在事务中执行的方法。
但是,在一个地方我需要手动执行 transactionManager.rollback()
,而不需要注释。如何获取 transactionManager
对象?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你想回滚当前事务,你可以使用
注意,它不会立即回滚事务 - 它设置“仅回滚”状态,因此事务将在尝试提交期间回滚。
Otherwise, if you need a programmatic transaction demaracation, you may use
TransactionTemplate
, as described in 10.6 Programmatic transaction management.您还可以获取
PlatformTransactionManager
的实例,但它并未得到广泛使用,因为TransactionTemplate
是编程式事务划分的推荐方法。另请参阅:
If you want to rollback the current transaction, you may use
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.Also you can obtain an instance of
PlatformTransactionManager
, but it's not widely used sinceTransactionTemplate
is a recommended approach for programmatic transaction demaracation.See also:
如果你的对象是由 Spring 配置的,你当然可以向其中注入一个事务管理器......
If your object is configured by Spring you could off course inject a transactionmanager into it...