系列四十二、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(积分表)表没有增加积分。符合预期

相关推荐
直有两条腿11 分钟前
【数据迁移】HBase Bulkload批量加载原理
大数据·数据库·hbase
言之。32 分钟前
ClickHouse 数据更新策略深度解析:突变操作与最佳实践
服务器·数据库·clickhouse
m0_64880493_江哥1 小时前
用正则方法从中英文本提取英文的python示例
python·mysql·正则表达式
白衣鸽子2 小时前
数据库高可用设计的灵魂抉择:CAP权衡
数据库·后端
九皇叔叔2 小时前
Linux Shell 正则表达式:从入门到实战,玩转文本匹配与处理
linux·mysql·正则表达式
DokiDoki之父3 小时前
Mybatis—入门 & (配置)SQL提示和日志输出
数据库·sql·mybatis
Wang's Blog3 小时前
MySQL: 高并发电商场景下的数据库架构演进与性能优化实践
mysql·性能优化·数据库架构
TDengine (老段)4 小时前
TDengine 数据函数 LN 用户手册
大数据·数据库·物联网·时序数据库·tdengine·涛思数据
机灵猫4 小时前
Redis 在订单系统中的实战应用:防重、限流与库存扣减
数据库·redis·缓存
木易2.04 小时前
从零构建RAG知识库管理系统(二)
数据库·oracle