How to obtain Spring transaction manager instance?

别来无恙 提交于 2019-12-12 13:22:26

问题


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?


回答1:


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.


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 since TransactionTemplate is a recommended approach for programmatic transaction demaracation.

See also:

  • 10. Transaction Management



回答2:


If your object is configured by Spring you could off course inject a transactionmanager into it...



来源:https://stackoverflow.com/questions/4123355/how-to-obtain-spring-transaction-manager-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!