문제

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:

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top