Spring @Transactional - Can I override rollbackFor

I call a service that has the following annotation:

@Transactional(rollbackFor=ExceptionA.class)
public void myMethodA(....) throws ExceptionA {
.
.
}

I am calling this method from another method in another Spring Bean.

@Transactional(rollbackFor=ExceptionB.class)
public void mainEntryPointMethod(....) throws ExceptionB {
.
  try {
    myMethodA()
  }
  catch (ExceptionA exp) {
   .
  }


.
}

My problem is that if myMethodA throws an exception, my transaction (which is passed from mainEntryPointMethod -> myMethodA by default) will be marked for rollback. Is there a way in which "rollbackFor" for an internal method can be overridden?

Thanks in advance Chris

+3
source share
1 answer

Solution # 1 You can specify that the method you call gets its own transaction. You can do this by annotating the method.

 @Transactional(propagation = Propagation.REQUIRES_NEW)

, (myMethodA) , , .

, .

№2 , .

+2

Source: https://habr.com/ru/post/1785977/


All Articles