Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring - How to get reference to TransactionStatus object in a Transactional method

When using @Transactional in a service layer, is there any option to get a reference to the TransactionStatus instance created by the PlatformTransactionManager?

For instance, in the following code :

@Transactional
public void updateCustomer(...) {
    // do some business stuff
    // can we get here a reference to the TransactionStatus instance ?
}
like image 587
Christophe D Avatar asked Mar 11 '23 07:03

Christophe D


1 Answers

TransactionStatus status = TransactionAspectSupport.currentTransactionStatus();

The currentTransactionStatus method returns the transaction status of the current method invocation.

If you are interested in the result of a transaction, you could consider the TransactionSynchronizationAdapter which provides a convenient afterCompletion(int status) callback:

class AfterCompletionTransactionHandler
        extends TransactionSynchronizationAdapter {

    public @Override void afterCompletion(int status) { ... }

}
like image 82
Andrew Tobilko Avatar answered Apr 30 '23 07:04

Andrew Tobilko