手工调用Transaction
除了用 @Transactional 外还可以手工调用
PlatformTransactionManager transactionManager = (PlatformTransactionManager)SpringBeanUtil.getBean(PlatformTransactionManager.class); DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition(); transactionDefinition.setPropagationBehavior(Propagation.REQUIRED.value()); TransactionStatus transaction = null; Object result; try { transaction = transactionManager.getTransaction(transactionDefinition); // 业务代码 transactionManager.commit(transaction); } catch (Exception ex) { if (null != transaction && !transaction.isCompleted()) { transactionManager.rollback(transaction); } throw ex; } finally { if (null != transaction && !transaction.isCompleted()) { transactionManager.rollback(transaction); } }