简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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