I have function
@Transactional
public class TransactionalService {
@Transactional
public void DoSomeThing() {
//do something
}
@Transactional
public void LogInfo() {
//do something
}
}
So when DoSomeThing() got an error or a runtime exception, it got rollback. I want whenever the DoSomeThing() got rollback, the transaction will call the LogInfo() method.
I haven't have any idea what to use inside the annotation. Do anyone have idea how to do it.
Thank you.
CodePudding user response:
One approach is using @TransacationalEvenListener
-s See the documentation
@TransactionalEventListener(phase = TransactionPhase.AFTER_ROLLBACK)
public void logInfo(CustomSpringEvent event) {
System.out.println("This is called after rollback");
}
CodePudding user response:
A transaction is rolled back when a runtime error occurs. Therefore, by getting an error, we can be sure that we have called another method when the transaction is rolled back
@Transactional
public class TransactionalService {
@Transactional
public void DoSomeThing() {
try {
doSomeThing;
} catch (Exception e) {
LogInfo();
}
}
@Transactional
public void LogInfo() {
//do something
}
}