在使用声明式事务管理时,如何显式提交spring db事务? [英] How can one explicitly commit a spring db transaction when using declarative transaction management?

查看:409
本文介绍了在使用声明式事务管理时,如何显式提交spring db事务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在一种情况下,我想要显式地提交当前事务(在这个例子中,我使用了@Transactional注解)



如何做到这一点?



我试过自动接线当前的HibernateTransactionManager从spring上下文,并使用它来提交,但它不提交事务。



我试过的代码是: / p>

  transactionManager.commit(transactionManager 
.getTransaction(new DefaultTransactionDefinition()));


解决方案

我没有尝试,但你可以做这与一些技巧,我可以想到



使用TransactionAspectSupport.currentTransactionStatus()
获取TransactionStatus注入事务管理器到你的bean(假设你正在使用hibernate)
尝试在事务管理器中调用doCommit(DefaultTransactionStatus status)。



尝试这个方法不能确定它是否工作,因为按照spring doc



交易


强烈建议您尽可能使用声明方法
回滚。程序化回滚是可用的
你绝对需要它,但它的使用情况下,面对实现一个
清洁基于POJO的架构。


我没有测试,但如果你设置setRollbackOnly到TransactionStatus也可以做的工作。


I am using Spring declarative database transaction management using the @Transactional annotation on my java methods.

In one case, I would like to explicitly commit the current transaction (the one wrapping the currently executing method) prior to the method returning.

How can this be done?

I have tried auto wiring the current HibernateTransactionManager from the spring context and using that to commit, but it doesn't commit the transaction.

The code I have tried is:

transactionManager.commit(transactionManager
  .getTransaction(new DefaultTransactionDefinition()));

解决方案

i have not tried but you might be able to do this with some trick which i can think of

get TransactionStatus using TransactionAspectSupport.currentTransactionStatus() inject transaction manager to your bean (assuming you are using hibernate) try to invoke doCommit(DefaultTransactionStatus status) in transaction manager.

try this out not sure it will work or not because as per spring doc

transaction

You are strongly encouraged to use the declarative approach to rollback if at all possible. Programmatic rollback is available should you absolutely need it, but its usage flies in the face of achieving a clean POJO-based architecture.

i have not tested but if you set setRollbackOnly to TransactionStatus that might also do the job.

这篇关于在使用声明式事务管理时,如何显式提交spring db事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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