0

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

}
Yuriy Tsarkov
  • 2,461
  • 2
  • 14
  • 28
Bilgehan
  • 1,135
  • 1
  • 14
  • 41

1 Answers1

4

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.

kluckow
  • 181
  • 1
  • 4