springboot手动开启事务

手动开启事务,手动提交/回滚

@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);
        }
    });
}
posted @ 2021-12-20 11:07  cchilei  阅读(2234)  评论(0编辑  收藏  举报