先看如下代码,主要做两件事情
写入日志表一条记录,tiExceptionLogService.insertExceptionRecord();
更新订单主表,result = ppDeliveryService.updatePpDelivery(ppDelivery);
@ApiOperation(value = "模拟更新订单主表异常-并发1", notes = "模拟更新订单主表异常-并发1")
@PostMapping(value = "updateDeliveryException1")
@Transactional(rollbackFor = Exception.class)
public PpDelivery updateDeliveryException1(@RequestBody PpDelivery ppDelivery) {
tiExceptionLogService.insertExceptionRecord(ppDelivery, "", "模拟事物问题");
for (int i = 0; i < 3; i++) {
Integer result = 0;
Boolean isException = false;
try {
result = ppDeliveryService.updatePpDelivery(ppDelivery);
Thread.sleep(20000);
if (result == 1) {
break;
}
} catch (Exception e) {
logger.error("ylToDPSOrderStatusFeedbackImpl update exception ppDelivery: {}", JSON.toJSONString(ppDelivery, SerializerFeature.WriteMapNullValue), e);
tiExceptionLogService.insertExceptionRecord(ppDelivery, e.getMessage(), "更新订单主表失败,数据库执行新增异常");
isException = true;
}
if (result == 0 && Boolean.FALSE.equals(isException)) {
logger.error("ylToDPSOrderStatusFeedbackImp update failed ppDelivery: {}", JSON.toJSONString(ppDelivery, SerializerFeature.WriteMapNullValue));
tiExceptionLogService.insertExceptionRecord(ppDelivery, String.valueOf(result), "更新订单主表失败,数据库执行新增未返回成功");
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
logger.error("ylToDPSOrderStatusFeedbackImpl interruptedException ppDelivery: {}", JSON.toJSONString(ppDelivery, SerializerFeature.WriteMapNullValue));
}
}
return ppDelivery;
}
疑问:如果更新订单表异常,比如某个字段过长,那么日志表是否会写入成功
异常如下
猜想1,因为该方法加了声明式事物,更新方法执行异常,写入逻辑肯定也会回滚
猜想2,声明式事物中所捕获的异常,不包含数据库异常,是无法捕获的,写入逻辑正常写入,不会回滚
结论
符合猜想2
20230320 记录一次数据库写入异常
代码
异常
结果:回滚了
奇怪