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

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

问题描述

由于某些原因,我已经手动执行了事务提交并使用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?

推荐答案

你可以通过一种更简单的方式得到你想要的东西,用 TransactionSynchronizationManager TransactionSynchronization

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

使用 TransactionSynchronizationManager ,您可以使用静态方法获取有关当前事务的信息,并且可以注册 TransactionSynchron ization 它允许你在调用时自动进行提交

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中使用事务时创建一个post提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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