1.如果有事务 tran1方法调用没有(或者有)事务 tran2?的方法,那么事务 tran1 和事务 tran2?都会生效;
例1:
@Transactional(rollbackFor = Exception.class)
public void tran1() {
User user = new User();
user.setId(1);
user.setUsername("张三丰");
user.setYear("89");
userRepository.save(user);
tran2();
System.out.println("执行后tran2==testThrow1**********");
}
@Transactional(rollbackFor = Exception.class)
public void tran2() {
User user = new User();
user.setId(2);
user.setUsername("刘少花");
user.setYear("12");
userRepository.save(user);
throw new IllegalArgumentException("日期条件解析错误");
}
例2:
@Transactional(rollbackFor = Exception.class)
public void tran1() {
User user = new User();
user.setId(1);
user.setUsername("张三丰");
user.setYear("89");
userRepository.save(user);
tran2();
System.out.println("执行后tran2==testThrow1**********");
}
public void tran2() {
User user = new User();
user.setId(2);
user.setUsername("刘少花");
user.setYear("12");
userRepository.save(user);
throw new IllegalArgumentException("日期条件解析错误");
}
例3:多加另一个tran3方法,不管中间方法tran2有没有事务,最终事务还是生效的
?例3-1
@Transactional(rollbackFor = Exception.class)
public void tran1() {
User user = new User();
user.setId(1);
user.setUsername("张三丰");
user.setYear("89");
userRepository.save(user);
tran2();
System.out.println("执行后tran2==testThrow1**********");
}
public void tran2() {
User user = new User();
user.setId(2);
user.setUsername("刘少花");
user.setYear("12");
userRepository.save(user);
tran3();
System.out.println("执行后tran2==testThrow2**********");
}
@Transactional(rollbackFor = Exception.class)
public void tran3() {
User user = new User();
user.setId(3);
user.setUsername("王二毛");
user.setYear("30");
userRepository.save(user);
System.out.println("执行后tran2==testThrow2*111111111111111*");
throw new IllegalArgumentException("日期条件解析错误");
}
?例3-2:
@Transactional(rollbackFor = Exception.class)
public void tran1() {
User user = new User();
user.setId(1);
user.setUsername("张三丰");
user.setYear("89");
userRepository.save(user);
tran2();
System.out.println("执行后tran2==testThrow1**********");
}
public void tran2() {
User user = new User();
user.setId(2);
user.setUsername("刘少花");
user.setYear("12");
userRepository.save(user);
tran3();
System.out.println("执行后tran2==testThrow2**********");
}
public void tran3() {
User user = new User();
user.setId(3);
user.setUsername("王二毛");
user.setYear("30");
userRepository.save(user);
System.out.println("执行后tran2==testThrow2*111111111111111*");
throw new IllegalArgumentException("日期条件解析错误");
}
?例3-3:
??
@Transactional(rollbackFor = Exception.class)
public void tran1() {
User user = new User();
user.setId(1);
user.setUsername("张三丰");
user.setYear("89");
userRepository.save(user);
tran2();
System.out.println("执行后tran2==testThrow1**********");
}
@Transactional(rollbackFor = Exception.class)
public void tran2() {
User user = new User();
user.setId(2);
user.setUsername("刘少花");
user.setYear("12");
userRepository.save(user);
tran3();
System.out.println("执行后tran2==testThrow2**********");
}
public void tran3() {
User user = new User();
user.setId(3);
user.setUsername("王二毛");
user.setYear("30");
userRepository.save(user);
System.out.println("执行后tran2==testThrow2*111111111111111*");
throw new IllegalArgumentException("日期条件解析错误");
}
?最终结果都不会插入数据库中
?
2.在同一个类中一个没有事务tran1的方法调用另一个有事务 tran2 的方法,那么那个有事务tran2 的方法是失效的
public void tran1() {
User user = new User();
user.setId(1);
user.setUsername("张三丰");
user.setYear("89");
userRepository.save(user);
tran2();
System.out.println("执行后tran2==testThrow1**********");
}
@Transactional(rollbackFor = Exception.class)
public void tran2() {
User user = new User();
user.setId(2);
user.setUsername("刘少花");
user.setYear("12");
userRepository.save(user);
throw new IllegalArgumentException("日期条件解析错误");
}
执行后2条数据插入正常
?
|