一、在springBoot使用事物时,发现事务并没有正常执行,没有进行回滚:
@GetMapping("/addincome/1")
@Transactional
public String addIncome1(@RequestParam("name") String name, @RequestParam("amount") double amount) {
try {
User user = new User();
user.setName(name);
userMapper.insert(user);
Income income = new Income();
income.setUserId(user.getId());
income.setAmount(amount);
income.setOperateDate(new Timestamp(System.currentTimeMillis()));
incomeMapper.insert(income);
return RESULT_SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return RESULT_FAILED + ":" + e.getMessage();
}
}
@GetMapping("/addincome/2")
@Transactional
public String addIncome2(@RequestParam("name") String name, @RequestParam("amount") double amount) {
try {
User user = new User();
user.setName(name);
userMapper.insert(user);
this.throwRuntimeException();
Income income = new Income();
income.setUserId(user.getId());
income.setAmount(amount);
income.setOperateDate(new Timestamp(System.currentTimeMillis()));
incomeMapper.insert(income);
return RESULT_SUCCESS;
} catch (Exception e) {
e.printStackTrace();
throw e;
// return RESULT_FAILED + ":" + e.getMessage();
}
}
但是并没有进行回滚操作。
分析:
默认spring事务
只在发生未被捕获的 RuntimeException
时才回滚。spring aop
异常捕获原理:被拦截的方法需显式抛出异常,并不能经任何处理,这样aop代理
才能捕获到方法的异常,才能进行回滚,默认情况下aop只捕获 RuntimeException 的异常
,但可以通过配置来捕获特定的异常并回滚。换句话说在service的方法中不使用try catch 或者在catch
中最后加上throw new runtimeexcetpion()
,这样程序异常时才能被aop捕获进而回滚。
解决办法:
1.首先确认数据库支持事务。即为InnoDB
。
方案一:手动回滚。给注解加上参数如:@Transactional(rollbackFor=Exception.class)
方案二:如上述分析。MyException改为继承RuntimeException的异常
。并且在service上层要继续捕获这个异常
并处理。
方案三:在service层方法的catch语句
。
增加:TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
语句,手动回滚,这样上层就无需去处理异常。