• 请不要在回答技术问题时复制粘贴 AI 生成的内容
sjysgms
V2EX  ›  程序员

这段代码,事务还执行吗?

  •  
  •   sjysgms · Jul 7, 2023 · 2342 views
    This topic created in 1071 days ago, the information mentioned may be changed or developed.

    各位大佬们,小白请教一下,这段代码还会执行事务吗?用的 MybatisPlus,缩略代码如下

    @Repository public class xxxRepository extends ServiceImpl {

    @Resource
    private TransactionTemplate transactionTemplate;
    
    public boolean saveOrUpdate(xxxx) {
        List<xxxDO> insertList = new ArrayList<>();
        List<xxxDO> updateList = new ArrayList<>();
        transactionTemplate.execute((status) -> {
            this.saveBatch(insertList);
            this.updateBatchById(updateList);
            return Boolean.TRUE;
        });
        return Boolean.TRUE;
    }
    

    }
    这段代码中 saveBatch 和 updateBatchById 是 mybatisplus 的方法,用的 @Transactional(rollbackFor = Exception.class)注解修饰,在继承的情况下,@Transactional 拥有事务的能力是因为 spring aop 生成代理了对象,这种方法直接调用了 this 对象的方法不会生成事务 但是,在此之外我还用了编程式事务包住了他们,这个编程式事务会生效吗? 麻烦各位大佬了

    4 replies    2023-07-08 08:25:31 +08:00
    milesians
        1
    milesians  
       Jul 7, 2023
    试一试不就可以了?
    antipro
        2
    antipro  
       Jul 7, 2023 via Android
    我猜应该会生效,毕竟外部已经有事务开始了,内部调用方法不会有任何改变,否则,我们是不是都不能在事务方法里调用自己的私有方法了?
    ttoh
        3
    ttoh  
       Jul 7, 2023 via iPhone
    目测会生效。验证的话,可以把 org.springframework.transaction.interceptor 这个包的日志级别设成 trace ,就可以看到 spring 每个事物打开和关闭的日志了。
    vishun
        4
    vishun  
       Jul 8, 2023
    感觉不生效,这就是同一类内部调用啊,不走 aop 怎么会生效呢?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2791 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 12:47 · PVG 20:47 · LAX 05:47 · JFK 08:47
    ♥ Do have faith in what you're doing.