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

309 天前
 sjysgms

各位大佬们,小白请教一下,这段代码还会执行事务吗?用的 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 对象的方法不会生成事务 但是,在此之外我还用了编程式事务包住了他们,这个编程式事务会生效吗? 麻烦各位大佬了

1243 次点击
所在节点    程序员
4 条回复
milesians
309 天前
试一试不就可以了?
antipro
309 天前
我猜应该会生效,毕竟外部已经有事务开始了,内部调用方法不会有任何改变,否则,我们是不是都不能在事务方法里调用自己的私有方法了?
ttoh
309 天前
目测会生效。验证的话,可以把 org.springframework.transaction.interceptor 这个包的日志级别设成 trace ,就可以看到 spring 每个事物打开和关闭的日志了。
vishun
309 天前
感觉不生效,这就是同一类内部调用啊,不走 aop 怎么会生效呢?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/954946

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX