defines a service:UserService where a method is defined as follows:
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void saveException(User user){
userDao.save(user);
throw new RuntimeException("");
}
if I call this method directly in the Controller layer, I can roll back, but if I wrap this method in another layer, it is as follows:
public void saveInnerException() {
User user1 = new User();
user1.setName("");
this.saveException(user1);
}
saveInnerException is still in the same service, and there is no transaction added to this method. At this time, if the Controller layer calls the saveInnerException method, the transaction will commit, which is strange. An exception has occurred and should be rolled back.
I did another test, and this time I redefined a service:UserService2, with a non-transactional method as follows:
public void test(){
User user = new User();
user.setName("");
userService.saveException(user);
}
if Controller calls the test method of UserService2, the transaction will be rolled back. There is also an extra layer of calls, why is the result different?