Search code examples
javaspringjpatransactional

Chained transactional api calls spring


I have a problem where I am not getting my transactions are commited

My Service class is @Transactional annotated

@Transactional
class MyService{
   public void find();
   public void return(); //Return will only happen if find is updated in DB
   public void cancel(){
          find();
          return();
   }
}

My class have 3 api they internally call the respective Dao, and Dao as are not annotated as Transactional.

Now if I call cancel, I will call first find and then the return(). But return() need the find() need to be updated the DB with FIND status.

But since all are happening on the same transaction untill the cancel is returned the transactions are not commited. Can someone help me to understand this situation and fix it.

I dunno how can I use Propagation in this situation.

Thanks in advance..


Solution

  • Exerpt from Spring Transcation documentation (Spring Doc)

    In proxy mode (which is the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual transaction at runtime even if the invoked method is marked with @Transactional.

    The calls to find() and return() inside cancel() method are not transactional. If you want it to be transactional you can do something like applicationContext.getBean(this.getClass()); and call your methods.