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

相关推荐
海浪仙人掌8 小时前
应收账款分析如何操作?应收账款分析有哪些注意事项?
数据库·人工智能
Elastic 中国社区官方博客8 小时前
在 Elasticsearch 中回填时间序列数据:通过批量 API 加载数月的历史指标数据
大数据·运维·数据库·人工智能·elasticsearch·搜索引擎·全文检索
Horn Still Sounds8 小时前
Linux网络并发服务器模型与SQLite数据库学习笔记
linux·服务器·数据库
昵称画9 小时前
POC验证怎么设计用例?不走过程的实操要点
大数据·数据库·人工智能·低代码·excel
TDengine (老段)9 小时前
TDengine Catalog 与元数据缓存
大数据·数据库·物联网·缓存·时序数据库·tdengine
CarIise10 小时前
MySQL数据库表关系与多表查询:从表间关系设计到JOIN联表查询
数据库·mysql
网络工程小王10 小时前
【LLM开发实验】FastAPI 学习笔记
数据库·笔记·学习·mysql·fastapi
Aime_Perfect10 小时前
sqlserver always on
服务器·数据库·sqlserver
摘星编程10 小时前
从“数据出库“到“模型入库“:DolphinDB 库内机器学习全流程实践
数据库
Sagittarius_A*10 小时前
【好靶场】SQL注入-时间盲注
数据库·sql