一、利用保存点
Object savepoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint();
TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savepoint);
二、利用多线程
@Transactional(rollbackFor = Exception.class)
public void test(){
try {
RefundEntity byId = refundService.getById(35);
byId.setStatus("oo");
refundService.updateById(byId);
System.out.println(1/0);
}catch (Exception e){
e.printStackTrace();
taskExecutor.execute(()->{operationLogService.OASave(35,RefundOperationEnum.OA_REJECT,"OK");});
throw new BizException("不和发");
}
}
不回滚的事务写到finally方法里(此种方法不好使)
@Transactional(rollbackFor = Exception.class)
public void test(){
try {
RefundEntity byId = refundService.getById(35);
byId.setStatus("hh");
refundService.updateById(byId);
System.out.println(1/0);
}catch (Exception e){
e.printStackTrace();
//执行顺序一
throw new BizException("不和发");
}finally {
//执行顺序二
operationLogService.OASave(35,RefundOperationEnum.OA_REJECT,"OK");
}
}
原因: 方法执行顺序:
1、开启事务(aop)
2、catch住异常
3、向上抛异常
4、执行finally方法
5、回滚或提交事务(aop)