Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a post commit when using transaction in Spring

Due to certain reasons i have manually performed transaction commit and roll back using Spring PlatformTransactionManager, what i need to do is setup a hook so that a post commit action takes place after transaction has been committed.

By looking at:

 void commit(TransactionStatus status) throws TransactionException; 

I cant see how i can determine a transaction was successful other than assumming it so if no expception are thrown.

And i could use AOP as one option, but what about programmitcally doing it, maybe using callback method?

like image 704
user1555190 Avatar asked Feb 22 '13 14:02

user1555190


People also ask

How do you commit a transaction in Spring?

Commit a transaction by calling the commit() method on the Connection interface. This tells your database to perform all required consistency checks and persist the changes permanently. Rollback all operations performed during the transaction by calling the rollback() method on the Connection interface.

What is the use of @transactional annotation in Spring?

The @Transactional annotation makes use of the attributes rollbackFor or rollbackForClassName to rollback the transactions, and the attributes noRollbackFor or noRollbackForClassName to avoid rollback on listed exceptions. The default rollback behavior in the declarative approach will rollback on runtime exceptions.

What does the @transactional annotation mean?

The @Transactional annotation is metadata that specifies that an interface, class, or method must have transactional semantics; for example, "start a brand new read-only transaction when this method is invoked, suspending any existing transaction".


2 Answers

You could get exactly what you want by a simpler way, with TransactionSynchronizationManager and TransactionSynchronization

With TransactionSynchronizationManager, you have static methods to get information about current transaction, and you can register a TransactionSynchronization wich allows you to automatically do a post-commit as you call that

TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization(){            void afterCommit(){                 //do what you want to do after commit            } }) 

Be aware that the TransactionSynchronization is on a per-thread basis (which is often not a problem for a basic web request).

like image 109
Grooveek Avatar answered Sep 22 '22 11:09

Grooveek


Credit to Grooveek's answer and Alex's comment under it - I put this here because the combined suggestions provide a solid and cleaner solution that is hard to find around the net.

Using Spring 4+. if you need a callback on a @Transactional method after it successfully commits just add that in the beginning of the method:

@Service public class OneService {      @Autowired     OneDao dao;      @Transactional     public void a transactionalMethod() {         TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter(){             public void afterCommit(){                 //do stuff right after commit                 System.out.println("commit!!!");              }         });         //do db stuff         dao.save();     } } 
like image 37
Michail Michailidis Avatar answered Sep 22 '22 11:09

Michail Michailidis