in my project I am using two transactional classes. Each class contains save method. When I add @Transactional(roolbackFor=Exception.class) annotation into these classes, for using like
@Component
public class CallerClass{
@Autowired
private TransactionClass1 class1;
@Autowired
private TransactionClass2 class2;
public void saveOperation(){
try{
class1.save();
class2.save();
}catch(Exception ex){
}
}
}
if class1.save method is executed without Exception but class2.save method throws an Exception, then class1.save operation is rolled back ?
No.
To have this behavior, the method containing these two calls would have to be transactional as well. Moreover, If you instantiate your classes using
new, the transactional interceptor won’t be used. You have to get instances of these classes from the application contect, or (preferrably), by dependency injection.