Spring boot 手动开启事务 手动提交 手动回滚

直接上代码

@Service
public class XXXService {

    // 这两个必须要注入
    @Resource
    DataSourceTransactionManager dataSourceTransactionManager;
    @Resource
    TransactionDefinition transactionDefinition;


    public void method1(){
        // 开启事务
        TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
        try{
            //提交事务
            dataSourceTransactionManager.commit(transactionStatus);
        } catch (Exception e) {            
            // 回滚事务
            dataSourceTransactionManager.rollback(transactionStatus);
        }
    }

}
posted @ 2023-06-22 19:44  哩个啷个波  阅读(1296)  评论(0编辑  收藏  举报