首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Spring中使用事务时创建post commit

在Spring中使用事务时创建post commit
EN

Stack Overflow用户
提问于 2013-02-22 22:14:31
回答 3查看 39.9K关注 0票数 37

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

通过查看以下内容:

代码语言:javascript
复制
 void commit(TransactionStatus status) throws TransactionException;

我不知道如何确定一个事务是成功的,除了假设它,所以如果没有抛出异常。

我可以使用AOP作为一种选择,但是如果通过编程的方式来做呢,比如使用回调方法?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-02-22 22:49:40

使用TransactionSynchronizationManagerTransactionSynchronization,您可以通过一种更简单的方式获得您想要的内容

使用TransactionSynchronizationManager,您可以使用静态方法来获取有关当前事务的信息,并且可以注册一个TransactionSynchronization,该as允许您在调用时自动执行提交后操作

代码语言:javascript
复制
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization(){
           void afterCommit(){
                //do what you want to do after commit
           }
})
票数 70
EN

Stack Overflow用户

发布于 2017-04-10 19:31:54

归功于Grooveek的回答和Alex的评论-我之所以把它放在这里,是因为综合起来的建议提供了一个在网络上很难找到的可靠和干净的解决方案。

使用Spring 4+。如果在@Transactional方法成功提交后需要回调,只需将其添加到方法的开头:

代码语言:javascript
复制
@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();
    }
}
票数 26
EN

Stack Overflow用户

发布于 2020-07-04 05:21:07

从Spring 4.2开始,就可以使用基于注释的配置为post提交事件(或者更一般的事务同步事件,例如回滚)定义侦听器。这基于core spring中的事件处理。使用这种方法测试代码会更容易,因为您避免了对TransactionSynchronizationManager的直接依赖,而这种依赖在单元测试中很可能不是活动的。您可以轻松地测试事务性服务是否发布了事件,以及侦听器在接收事件时是否执行了正确的操作。

所以不用再费劲了,下面就是如何设置它:

在这个例子中,我们假设你有一个Customer实体和一个CustomerRepository (以及相应的ORM )。

首先,您需要一个新的事件类型NewCustomerEvent

代码语言:javascript
复制
// NewCustomerEvent.java
// Just a regular pojo for the event
public class NewCustomerEvent {

    public String email;

    // constructor, setters and getters omitted
}

然后使用@TransactionalEventListener定义一个侦听器。默认情况下,这将在成功提交后执行,但可以使用phase参数进行更改:

代码语言:javascript
复制
// NewCustomerEventListener.java
@Component
public class NewCustomerEventListener {

    @TransactionalEventListener
    public void handleNewCustomerEvent(NewCustomerEvent newCustomerEvent) {
        // handle new customer event
    }
}

最后,您可以使用一个ApplicationEventPublisher来增强您的事务服务,一旦发送了所有事务语句,您就可以在该an上调用publish。

代码语言:javascript
复制
// CustomerRespositoryService.java
@Service
public class CustomerRepositoryService {

    @Inject
    private ApplicationEventPublisher applicationEventPublisher;

    @Inject
    private CustomerRepository customerRepository;

    @Transactional
    public void createCustomer(String email) {
        Customer customer = new Customer(email);
        customerRespotory.save(customer);
        applicationEventPublisher.publish(new NewCustomerEvent(email));
    }

}

另请参阅:

票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15026142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档