事务提交后,异步执行

文章探讨了Spring框架中`@Transactional`和`@Async`注解在方法同步与异步执行中的行为,指出同一类中`@Async`不生效导致事务回滚的问题,以及不同服务间`@Async`有效但不影响主方法事务的情况。
摘要由CSDN通过智能技术生成

背景:

有个业务是这样的,先执行导入操作,导入提交后再异步执行其他业务,数据依赖导入数据。

1. 主方法

@Transactional(rollbackFor = Exception.class)
    public void testA() {
        long begin = System.currentTimeMillis();
        BrandInfo brandInfo = new BrandInfo();
        brandInfo.setBrandCode("brandCode1");
        brandInfo.setBrandName("brandName1");
        this.save(brandInfo);
        TransactionSynchronizationManager.registerSynchronization(
                new TransactionSynchronization() {
                    @Override
                    public void afterCommit() {
                        productInfoService.testA();
                    }
                }
        );
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("主方法执行时间:"+(end - begin));
    }

2. 异步执行方法,需要不同的service

@Async
    @Transactional(rollbackFor = Exception.class)
    public void testA() {
        long begin = System.currentTimeMillis();
        try {
            LambdaQueryWrapper<BrandInfo> brandInfoQueryWrapper = Wrappers.lambdaQuery();
            List<BrandInfo> brandInfoList = brandInfoMapper.selectList(brandInfoQueryWrapper);
            System.out.println("查询后数据:" + JSONUtil.toJsonStr(brandInfoList));
            for (int i = 0; i < 10; i++) {
                ProductInfo productInfo = new ProductInfo();
                productInfo.setProductCode("productCode" + i);
                productInfo.setProductName("productName" + i);
//                if (i == 5) {
//                    throw new RuntimeException("异步执行异常");
//                }
                this.save(productInfo);
            }

            Thread.sleep(5000);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            BrandInfo brandInfo = new BrandInfo();
            brandInfo.setBrandCode("B1");
            brandInfo.setBrandName("B2");
            brandInfoService.testInsert(brandInfo);
            //e.printStackTrace();
        }
        System.out.println("异步执行");
        long end = System.currentTimeMillis();
        System.out.println("异步方法执行时间:" + (end - begin));
    }

3. 实体类

@Data
public class BrandInfo extends BaseEntity{
    private String brandCode;
    private String brandName;
}
@Data
public class ProductInfo extends BaseEntity{
    private String productCode;
    private String productName;
}

结语:

1. @Transactional和@Async的不生效的问题,同一个类 @Async不生效,导致子方法回滚,主方法也会回滚。如果是不同service @Async生效,不会导致主方法回滚。

2.同一个类的不同方法,A方法没有@Transactional,B方法有@Transactional,A调用B方法,事务不起作用。原理:spring 在扫描bean的时候会扫描方法上是否包含@Transactional注解,如果包含,spring会为这个bean动态地生成一个子类(即代理类,proxy),代理类是继承原来那个bean的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值