在 Spring 中使用事务时创建提交后 [英] Creating a post commit when using transaction in Spring

查看:20
本文介绍了在 Spring 中使用事务时创建提交后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,我使用 Spring PlatformTransactionManager 手动执行了事务提交和回滚,我需要做的是设置一个钩子,以便在事务提交后进行提交后操作.

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.

通过查看:

 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.

而且我可以使用 AOP 作为一种选择,但是如何以编程方式执行它,也许使用回调方法?

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

推荐答案

使用 TransactionSynchronizationManagerTransactionSynchronization

使用TransactionSynchronizationManager,您可以使用静态方法来获取有关当前事务的信息,并且您可以注册一个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
           }
})

请注意,TransactionSynchronization 是基于每个线程的(这对于基本的 Web 请求通常不是问题).

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

这篇关于在 Spring 中使用事务时创建提交后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆