简体   繁体   中英

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. How can I obtain transactionManager object?

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:

如果您的对象是由Spring配置的,那么您当然可以将一个事务管理器注入其中......

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM