一.业务需求
方法中需要处理一定的业务逻辑,如果业务逻辑失败,则相关的业务操作回滚,同时记录一条错误日志
二.具体实现
//添加事务注解
@Transactional(rollbackFor = Exception.class)
public void changeBookPrice(Book book, Book bookEntity) {
//创建回滚点
Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint();
try {
//具体的业务逻辑操作
......
} catch (Exception e) {
log.error("报错的详细信息为:{}", e.getMessage());
//手动回滚事务
TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint);
//记录错误的日志
.......
}
}
详细解释
- 方法上添加@Transactional注解
- 方法开始的地方创建回滚点
- try中进行业务逻辑操作
- catch中手动进行事务回滚,并记录错误日志
- 上面的操作就能进行业务逻辑回滚,同时记录错误日志