系列四十二、Spring的事务传播行为案例演示(二)#REQUIRED

一、演示Spring的默认传播行为(REQUIRED)

1.1、运行之前表中的数据

1.2、StockServiceImpl

java 复制代码
/**
 * @Author : 一叶浮萍归大海
 * @Date: 2023/10/30 15:43
 * @Description:
 */
@Service(value = "stockServiceREQUIRED")
public class StockServiceImpl extends ServiceImpl<StockMapper, StockDO> implements StockService {

    @Resource
    private StockMapper stockMapper;

    @Resource
    private IntegralService integralService;

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public void reduceStock(Long id, Integer num) {
        try {
            // 减库存
            StockDO dbStock = stockMapper.selectById(id);
            StockDO updateStock = new StockDO();
            BeanUtils.copyProperties(dbStock, updateStock);
            updateStock.setNum(dbStock.getNum() - num);
            stockMapper.updateById(updateStock);

            // 增加积分
            IntegralDO updateIntegral = new IntegralDO();
            updateIntegral.setPreIntegral(0);
            updateIntegral.setCurrentIntegral(1000);
            updateIntegral.setUserId(1L);
            integralService.addIntegral(updateIntegral);

            // 模拟异常
            int i = 10 / 0;
        } catch (BeansException e) {
            throw new RuntimeException(e);
        }
    }
}

1.3、IntegralServiceImpl

java 复制代码
/**
 * @Author : 一叶浮萍归大海
 * @Date: 2023/10/30 15:42
 * @Description:
 */
@Service(value = "integralServiceREQUIRED")
public class IntegralServiceImpl extends ServiceImpl<IntegralMapper, IntegralDO> implements IntegralService {

    @Resource
    private IntegralMapper integralMapper;

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public void addIntegral(IntegralDO updateIntegral) {
        integralMapper.insert(updateIntegral);
    }
}

1.4、测试结果

1.5、数据变化

stock表中的库存数量没有发生变化,integral(积分表)表没有增加积分。符合预期

相关推荐
小白男神5 天前
MySQL进阶学习四(存储过程、游标、触发器)
mysql
这个DBA有点耶5 天前
MVCC深入:Read View、版本链与快照读——InnoDB并发控制的内核
数据库·mysql·架构
DBA_G5 天前
从地面到云霄:GBase数据库在民航三大场景的落地实践
数据库
自由能燃气设备5 天前
商用全预混低氮冷凝锅炉免费方案vs付费方案对比+选型避坑指南
大数据·数据库·人工智能
科创致远5 天前
科创致远 ESOP 系统核心效能与实战价值展示
大数据·数据库·人工智能·精益工程
kybs19915 天前
全球灾害数据分析可视化 毕业设计-附源码66794
vue.js·spring boot·mysql·安全·django·c#·asp.net
2601_962218615 天前
万象生鲜系统称重自动多退少补算法解决生鲜非标品痛点
大数据·数据库·人工智能·python·算法
程序猿_极客5 天前
【免费】分享一套优质的基于SSM的校园失物招领系统的设计与实现,源码+文档+视频详解(讲解)
java·mysql·ssm·课程设计·失物招领系统
张洛闻Eren5 天前
k8s云原生【第十课】:水平 Pod 自动扩缩容
运维·数据库·云原生·kubernetes·github
于平安5 天前
MySQL-触发器
数据库·mysql