再小的个子,也能给沙漠留下长长的身影;再小的人物,也能让历史吐出重重的叹息。——余秋雨《文化苦旅》
我们可以手动管理事务
首先需要引用两个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); }
|