再小的个子,也能给沙漠留下长长的身影;再小的人物,也能让历史吐出重重的叹息。——余秋雨《文化苦旅》
我们可以手动管理事务
首先需要引用两个Bean
@Resource private TransactionDefinition transactionDefinition; @Resource private IArticleEnclosureService articleEnclosureService;
然后是使用
PlatformTransactionManager transactionManager = Objects.requireNonNull(transactionTemplate.getTransactionManager()); TransactionStatus transactionStatus = transactionManager.getTransaction(transactionDefinition); if (逻辑执行正确) { //提交事务 transactionManager.commit(transactionStatus); } else { // 回滚事务 transactionManager.rollback(transactionStatus); // 记录日志 log.error(e); }
或者是
PlatformTransactionManager transactionManager = Objects.requireNonNull(transactionTemplate.getTransactionManager()); TransactionStatus transactionStatus = transactionManager.getTransaction(transactionDefinition); try{ //提交事务 transactionManager.commit(transactionStatus); } catch (Exception e) { // 回滚事务 transactionManager.rollback(transactionStatus); // 记录日志 log.error(e); }