Issue
Hi I have two different methods and they use different datasources and transaction manager.I use @Transactional
attribute and what I want, if my second method throws exception than my first method do its rollback. But it is not working, first method cant rollback. What am I missing?
@Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED,
transactionManager = myTransactionManager", propagation = Propagation.REQUIRED)
public void saveTest(TblTest testEntity) {
mySecondDBSource.saveTest2(testEntity);(use MyTransactionManager2) //Do job
testTableRepository.save(testEntity); (Use myTransactionManager) //throws Exception
}
//in mySecondDBSource class there is another method
@Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED,
transactionManager = "MyTransactionManager2", propagation = Propagation.REQUIRED)
public void saveTest2(TblTest2 testEntity) {
testTableRepository2.save(testEntity);
}
Solution
Spring Data offers a way to handle so called chained/distributed transactions via ChainedTransactionManager.
See spring-transactional-with-a-transaction-across-multiple-data-sources.
Here is also a simple guide on medium.
Answered By - kluckow
Answer Checked By - Katrina (JavaFixing Volunteer)