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

相关推荐
墨神谕14 分钟前
认识一下Unicode(统一码)
android·java·数据库
xywww16832 分钟前
Claude Opus 5 API 接入实战:国内项目上线前的网络、Key、限流和排错清单
大数据·linux·网络·数据库·云计算·aws
yuezhilangniao41 分钟前
某专科医院老数据库迁移实战记录-Oracle10g sqlserver2008等
数据库
白猫不黑44 分钟前
SQL注入实战:手工注入全流程详解
网络·数据库·sql·web安全·网络安全·信息安全
kirs_ur10 小时前
ECC & LDPC — SSD 的数据卫士
服务器·数据库·性能优化
是三一seven11 小时前
Sql注入基础
数据库·安全·网络安全
Sirens.11 小时前
MySQL表设计进阶-约束范式连接索引与事务
android·数据库·mysql
字节跳动开源13 小时前
火山引擎开源 Agent 驱动的搜索自迭代技术
数据库·开源·agent
Oo大司命oO15 小时前
藏在正则表达式里的陷阱
数据库·mysql·正则表达式
_oP_i16 小时前
mysql统计数据库使用存储大小
数据库