Spring事务代码,

本文详细介绍了在SpringBoot项目中如何使用DataSourceTransactionManager进行事务管理。通过具体代码示例,展示了如何设置事务传播行为为PROPAGATION_REQUIRES_NEW,并在多服务调用场景下实现事务的统一管理和回滚。
WebApplicationContext contextLoader = ContextLoader.getCurrentWebApplicationContext();//springboot项目这个获取可能会是null
        DataSourceTransactionManager transactionManager = contextLoader.getBean("transactionManager", DataSourceTransactionManager.class);
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
        TransactionStatus status = transactionManager.getTransaction(def);
        try {
           for(String errCode:serviceList) {
              tBChnlMerPoolBasicService.changePool(errCode+"_"+cmp_next_old, errCode+"_"+cmp_next_new);
           }
//         Integer.parseInt(newCode);
        } catch (Exception e) {
            e.printStackTrace();
            transactionManager.rollback(status);
            sendErr("商户池切换失败", response);
            return;
        } 
        transactionManager.commit(status);

事务管理,事务定义,事务状态,

转载于:https://my.oschina.net/Skynet01/blog/3001015

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值