小编Wil*_*wan的帖子

使用@Transactional注解有必要吗?弹簧靴

所以我是 Spring 新手,目前对 @Transactional Annotation 感到困惑,我已经阅读了很多关于这个主题的问答,但似乎我仍然不明白。

这是我的问题:

  • 执行插入更新删除时需要@Transactional注解吗?当我试图证明这一点时,我仍然可以执行插入和更新。你知道为什么会这样吗?

  • 如果您使用或不使用@Transactional Annotation,是否会有任何性能优势或问题?就像连接管理一样。

  • 如果我在程序中不使用@Transactional 注解会发生什么?

  • 根据我使用 @Transactional 的经验,更新查询将在方法完成后刷新并提交,并且在某些情况下我不希望发生这种情况。例如:

@Transactional
private void doSomething() {
   TransactionEntity te = te.findById("");
   try {
      //fetch using feign and throw customTimCustomTimeoutException 
   } catch (CustomTimeoutException e) {
      te.workflowFailure("doSomething");
      repository.save(te);
   }
   //validation
   //business logic
   //save this save that
   //end of method and changes will be flushed and committed
}
Run Code Online (Sandbox Code Playgroud)

如果在方法结束时数据库脱机,它将回滚,您将失去所有进度。即使在 catch 块语句中的repository.save(te) 中,数据库可能没问题,并且您不想失去该进度。对此有什么解决方案或想法吗?

谢谢。

java spring jpa spring-data-jpa spring-boot

7
推荐指数
1
解决办法
6549
查看次数

标签 统计

java ×1

jpa ×1

spring ×1

spring-boot ×1

spring-data-jpa ×1