0
votes

I wonder, what happens when @Transactional(readonly=true) method calls @Transactional(readonly=false) method? Talking about the situation with propagation = Propagation.REQUIRED (within the outer transaction scope).

public class ServiceA {

   ServiceB serviceB;


   @Transactional(readonly = true)
   public void readOnly() {
       
      // some reading from repo

      serviceB.write()
   }

}


public class ServiceB {

   @Transactional
   public void write() {
     
     // some write to repo

   }

}

The same question for the opposite situation, what happens if @Transactional(readonly=false) method calls @Transactional(readonly=true) method? I suppose that for the last case it would just consider @Transactional(readonly=false) from the outer scope.

1

1 Answers

0
votes

Calling readOnly=false from readOnly=true doesn't work since the previous transaction continues.

This is because the previous transactional is being continued.

If you want this to work, you must have it start a new transaction. Sample:

public class ServiceB {

   @Transactional(propagation = Propagation.REQUIRES_NEW)
   public void write() {     
     // some write to repo ..
   }
}

In this case, it will work because a new transactional will be started.