Creating a post commit when using transaction in Spring

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).

Leave a Comment