【问题标题】:Spring Data Transaction ControlSpring 数据事务控制
【发布时间】:2019-04-11 14:55:52
【问题描述】:

问题是关于在 crudrepository、jparepository ext 中使用多个事务。在我的项目中,有两个实体。 RequestEntity 和 SendingMailEntity。我的方法中的工作流程:

1) 保存 RequestEntity ,

2)发送信息服务(这是我们购买的休息服务,我们无法控制它的任何异常。)

3) 保存 SendingMailEntity。

当我们在 2 号或 3 号出现异常时,由于 spring jpa 控制的回滚,我们丢失了 requestEntity。

requestEntity 的记录永远不会丢失。

@Transactional
public RequestEntity create(RequestEntity entity) {

    entity=requestRepository.save(entity);
    sendMail(entity);
}

@Transactional(propagation=Propagation.REQUIRES_NEW)
public SendingMailEntity sendMail(RequestEntity entity) {

         /*
          *
          */
   informationService(entity.*,*,*);
         /*
          *
          */

     sendingMailRepository.save(sendingMailEntity);
}

此代码块不起作用。当 sendMail 出错时,RequestEntity 没有保存。

【问题讨论】:

  • 如果您无法控制“信息服务”,那么为什么要将其包含在事务中。如果整个过程不是原子的,则将失败的代码从事务中分离出来或正确处理异常。您是否尝试过 Propagation=NOT_SUPPORTED
  • 我试过了,还是不行。实际上,它是关于旧习惯的。使用休眠会话时,我们可以在线提交或回滚我们想要的地方。我无法控制弹簧数据。

标签: java spring spring-boot spring-data-jpa spring-transactions


【解决方案1】:

处理sendMail中的所有异常,不要让它返回到调用函数。

如果单独的异常处理不起作用,您也可以尝试将sendMail 移动到带有override 的新公共类中

【讨论】:

    猜你喜欢
    • 2013-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    相关资源
    最近更新 更多