再小的个子,也能给沙漠留下长长的身影;再小的人物,也能让历史吐出重重的叹息。——余秋雨《文化苦旅》

我们可以手动管理事务

首先需要引用两个Bean

1
2
3
4
@Resource
private TransactionDefinition transactionDefinition;
@Resource
private IArticleEnclosureService articleEnclosureService;

然后是使用

1
2
3
4
5
6
7
8
9
10
11
      PlatformTransactionManager transactionManager = Objects.requireNonNull(transactionTemplate.getTransactionManager());
TransactionStatus transactionStatus = transactionManager.getTransaction(transactionDefinition);
if (逻辑执行正确) {
//提交事务
transactionManager.commit(transactionStatus);
} else {
// 回滚事务
transactionManager.rollback(transactionStatus);
// 记录日志
log.error(e);
}

或者是

1
2
3
4
5
6
7
8
9
10
11
      PlatformTransactionManager transactionManager = Objects.requireNonNull(transactionTemplate.getTransactionManager());
TransactionStatus transactionStatus = transactionManager.getTransaction(transactionDefinition);
try{
//提交事务
transactionManager.commit(transactionStatus);
} catch (Exception e) {
// 回滚事务
transactionManager.rollback(transactionStatus);
// 记录日志
log.error(e);
}