Spring实现Transactional执行成功/回滚后执行额外操作

首先函数一定要有@Transactional
然后,任意位置添加这段

TransactionSynchronizationManager.registerSynchronization(
                new TransactionSynchronizationAdapter() {
                    @Override
                    public void afterCommit() {
                        runnable.run();
                    }
                }
        );

如果要实现cancel,可以参考 https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/support/TransactionSynchronization.html

Leave a Reply

Your email address will not be published. Required fields are marked *