手动开启事务,手动提交/回滚
@Resource
DataSourceTransactionManager dataSourceTransactionManager;
public void updateMonitorRecord() {
taskExecutor.execute(() -> {
//开启事务
DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);//创建一个新事务
TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(definition);
try {
//do something 业务代码
//提交
dataSourceTransactionManager.commit(transactionStatus);
} catch (Exception e) {
//手动回滚
dataSourceTransactionManager.rollback(transactionStatus);
}
});
}