Spring Framework 5.3.16 Spring Boot 2.5.10
When TransactionTemplate
is retrieved from another class, PROPAGATION_REQUIRES_NEW
of TransactionTemplate
does not work.
Here is part of the code that reproduces it:
@Transactional(isolation = Isolation.READ_COMMITTED)
@Retryable(ObjectOptimisticLockingFailureException.class)
public boolean demo() {
Child child = childRepository.findFirstByOrderById().orElseThrow();
// if using `Transactions.getNew()`, the new transaction will not be actually created, which is not expected
// check the mysql general log, we can see the `insert` and `update` scripts are executed in the same transaction
// weirdly, changing `Transactions.getNew()` to `transactionTemplate` can fix it
Transactions.getNew().execute(status -> {
Assert.state(status.isNewTransaction(), "New transaction required");
Parent parent = new Parent();
parent.setChild(child);
// inserting a parent record will lock(S lock) the child row
// to avoid deadlock, we start a new transaction to insert it
return parentRepository.save(parent);
});
// updating a record will lock(X lock) the row
child.setName("child-a");
childRepository.save(child);
return true;
}
Please see the reproduction: https://github.com/Edsuns/transaction-template-issues.
Comment From: snicoll
When TransactionTemplate is retrieved from another class
Thanks for the sample but I don't understand what that means. The sample is also quite involved, mixing the annotation model with programmatic registration of transactions using custom code. It would be nice to clarify what you mean by "another class" and reduce the sample to the bare minimum. Thanks!
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: spring-projects-issues
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.