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

}

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes it is working.Is it best way or different solutions?Regards
So far I didn't step over any other solution using the 'spring way'.
This post might also prove useful: metamorphant.de/blog/posts/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.