java - Spring @Transactional 行为
问题描述
当我创建一个带有两个实体的方法@Transactional 时,一个用于更新,一个用于插入,Spring 不使用相同的事务进行更新和插入是正常的吗?
@Transactional
public void myMethod(args...){
Entity1 entity1 = entity1Repository.findById(args);
entity1.setValue(args);
Entity2 entity2 = new Entity2();
entity2.setValue(args);
entity2Respository.save(entity2);
}
在休眠日志中,我看到两个事务,一个用于 findBy 和 update,另一个用于插入。
解决方案
你从哪里调用 myMethod() ?如果它来自同一个类,那么@Transactional
注释甚至不存在,因为没有在 spring 代理对象上调用方法,并且您将有单独的事务用于更新和插入。
推荐阅读
- amazon-web-services - 基于客户端浏览器限制对静态 s3 站点的访问
- shell - 入口 shell 脚本(启动前端和后端)应该有什么名称?
- c# - C# - 以类型安全的方式将输入对象映射到效果对象
- angular - 如何将 aria-expanded 设置为 html 选择
- google-sheets - 谷歌表格将文本分成多列
- python - 为什么 Cx_Freeze 会加载这么多库
- r - 包括具有不同观测年数的数据框的趋势
- twitter-bootstrap - 带有所选项目的输入组以填充 Bootstrap 4.5.3 中的只读字段
- javascript - JavaScript:如何在对象数组中动态更改键位置?
- python - 登录页面问题(烧瓶,python)