Spring的@Transactional源码中写道:
By default, a transaction will be rolling back on {@link RuntimeException}and {@link Error} but not on checked exceptions (business exceptions).
默认情况下,如果在事务中抛出了未检查异常(继承自 RuntimeException 的异常)或者 Error,则 Spring 将回滚事务;除此之外,Spring 不会回滚事务。
测试①:
默认spring事务只在发生未被捕获的 RuntimeException 时才回滚。
// 测试回滚成功案例,基于IllegalArgumentException(RuntimeException)实现回滚
@GetMapping("/testSuccess")
@Transactional// 如果不加,就不会回滚
public R testSuccess(@RequestParam("type") Integer type){
eduTeacherService.removeById("2");
if (type == 1){
throw new IllegalArgumentException("测试回滚成功案例!");
}
eduTeacherService.removeById("3");
return R.ok();
}
测试②:
Exception异常,事务回滚失败;
// 测试回滚失败案例,基于Exception实现回滚;
@GetMapping("/testFail")
@Transactional
public R testFail(@RequestParam("type") Integer type) {
try {
eduTeacherService.removeById("2");
if (type == 1){
throw new Exception("测试回滚失败案例!");
}
eduTeacherService.removeById("3");
} catch (Exception e) {
e.printStackTrace();
}
return R.ok();
}
测试③:
用rollbackFor解决Exception不进行事务回滚
rollbackFor = Exception.class + throws Exception
@GetMapping("/testFailRollbackFor")
// 配置rollbackFor
@Transactional(propagation= Propagation.REQUIRED,rollbackFor = Exception.class)
public R testFailRollbackFor(@RequestParam("type") Integer type) throws Exception {
eduTeacherService.removeById("2");
if (type == 1){
throw new Exception("测试回滚失败rollbackFor成功案例!");
}
eduTeacherService.removeById("3");
return R.ok();
}
测试④:
手动回滚解决Exception不进行事务回滚
catch:
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
@GetMapping("/testSuccessByHand")
@Transactional
public R testSuccessByHand(@RequestParam("type") Integer type) {
try {
eduTeacherService.removeById("2");
if (type == 1){
throw new Exception("测试回滚失败案例!");
}
eduTeacherService.removeById("3");
} catch (Exception e) {
e.printStackTrace();
//手动回滚,如果sql2()抛了异常,sql1()会回滚,不影响事物正常执行
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
}
return R.ok();
}