【问题标题】:Spring Integration (DSL): Run two JPA queries in the same transactionSpring Integration (DSL):在同一个事务中运行两个 JPA 查询
【发布时间】:2021-01-08 10:22:01
【问题描述】:

如何让两个 JPA 查询在同一个事务中运行?

请参见下面的示例。如果第二个查询失败,我希望回滚第一个查询。

    @Bean
    IntegrationFlow dbFlow() {
      return IntegrationFlows.from("poiChannel")
        .routeToRecipients(r -> r
          .recipientFlow(
            (f) -> f.handle(Jpa.outboundAdapter(entityManagerFactory)
                .jpaQuery("delete from PointOfInterest"),
              e -> e.transactional()
            )
          )
          .recipientFlow(
            (f) -> f.handle(Jpa.outboundAdapter(entityManagerFactory)
                .entityClass(PointOfInterest.class),
               e -> e.transactional(true)
            )
          )
        )
        .get();
    }

【问题讨论】:

    标签: spring-integration spring-integration-dsl


    【解决方案1】:

    好吧,看来我只需将transactional() 命令上移一级到routeToRecipients() 流。

        @Bean
        IntegrationFlow dbFlow() {
          return IntegrationFlows.from("poiChannel")
            .routeToRecipients(r -> r
              .recipientFlow(
                (f) -> f.handle(Jpa.outboundAdapter(entityManagerFactory)
                    .jpaQuery("delete from PointOfInterest")
                )
              )
              .recipientFlow(
                (f) -> f.handle(Jpa.outboundAdapter(entityManagerFactory)
                    .entityClass(PointOfInterest.class)
                )
              )
              .transactional()
            )
            .get();
        }
    

    【讨论】:

    • 嗯,这绝对是正确的逻辑。您的两个 JPA 接收者在各自的子流程中工作。确实希望它们不会相互影响。要使两个子流程在同一个事务中工作,您确实必须将事务定义移到两个子流程之上。
    • 谢谢@ArtemBilan。通常也是因为在这里非常活跃并回答了所有 Spring Integration 问题。
    猜你喜欢
    • 2011-06-19
    • 2018-04-15
    • 2018-03-02
    • 1970-01-01
    • 2019-07-18
    • 2014-11-02
    • 2018-01-14
    • 2017-11-06
    • 1970-01-01
    相关资源
    最近更新 更多