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

戏子无情 提交于 2019-12-13 15:26:45

问题


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()));

回答1:


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.




回答2:


The nested transaction, that being seeked above, can be achieved using REQUIRES_NEW propogation, BUT by taking care of self invocation, as explained here



来源:https://stackoverflow.com/questions/11461554/how-can-one-explicitly-commit-a-spring-db-transaction-when-using-declarative-tra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!