Spring @Transactional - Can I Override rollbackFor
I am calling a service which has the following annotation:
@Transactional(rollbackFor=ExceptionA.class)
public void myMethodA(....) throws ExceptionA {
.
.
}
I am calling this method from an other 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 propagation开发者_开发百科) will be marked for rollback. Is there a way in which the 'rollbackFor' for the inner method can be overriden?
Thanks in advance Chris
Solution #1 You can specify that the method you are calling gets its own transaction. You can do that by annotating the method.
@Transactional(propagation = Propagation.REQUIRES_NEW)
That way if the method (myMethodA) is marked for roll back only that transaction will be rolled back and not the caller transaction.
That only works if you are allowed to let the called method manage its own transaction and don't want to roll back the caller transaction.
Solution #2 Maybe you can try to subclass and change the transactional annotation attributes.
精彩评论