问题
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