Quantcast
Channel: Active questions tagged propagation - Stack Overflow
Viewing all articles
Browse latest Browse all 52

If an exception occurs within a nested Transaction with the propagation REQUIRED_NEW, the Spring framework will perform a rollback of the transaction

$
0
0

I have a method annotated with @Transactional that calls a second method annotated with @Transactional(propagation=Propagation.REQUIRES_NEW), which in turn calls a third method annotated with @Transactional. If the third method throws an exception and it is caught and suppressed in the second method, the transaction from the first method will be rolled back. However, if the third method does not have the @Transactional annotation, only the transaction from the second method will be rolled back. Why @Transactional affect on transaction from first method instead of new transaction from second?

@Serviceclass FirstClass {    private final SecondClass secondClass;    @Transactional    public void firstMethod() {        //... some code        secondClass.secondMethod();        //... some code    }}@Serviceclass SecondClass {    private final ThirdClass thirdClass;    @Transactional(Propagation.REQUIRES_NEW)    public void secondMethod() {        //... some code        try {            thirdClass.thirdMethod();            //... some code        }catch (Exception e){        }    }}@Serviceclass ThirdClass {    @Transactional // this transaction     public void thirdMethod() {        throw new RuntimeException();    }}

if we rewrite third method by this way:

@Serviceclass ThirdClass {    public void thirdMethod() {        throw new RuntimeException();    }}

In this scenario, the first transaction will not be rolled back.


Viewing all articles
Browse latest Browse all 52

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>