简体   繁体   English

使用声明式事务管理时,如何显式提交Spring db事务?

[英]How can one explicitly commit a spring db transaction when using declarative transaction management?

I am using Spring declarative database transaction management using the @Transactional annotation on my java methods. 我在Java方法上使用@Transactional批注使用Spring声明性数据库事务管理。

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. 我尝试从spring上下文自动连接当前的HibernateTransactionManager并使用它来提交,但是它不提交事务。

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. 使用TransactionAspectSupport.currentTransactionStatus()获取TransactionStatus将事务管理器注入到您的bean中(假设您使用的是hibernate),尝试在事务管理器中调用doCommit(DefaultTransactionStatus status)。

try this out not sure it will work or not because as per spring doc 试试这个不确定它是否可以正常工作,因为根据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. 如果您绝对需要它,则可以使用程序化回滚,但是面对实现干净的基于POJO的体系结构时,它的用法就不那么理想了。

i have not tested but if you set setRollbackOnly to TransactionStatus that might also do the job. 我还没有测试过,但是如果将setRollbackOnly设置为TransactionStatus,那也可能会完成这项工作。

嵌套事务上面所seeked,可以使用REQUIRES_NEW传播史,但照顾自我调用的实现,说明这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM