首页 > 解决方案 > Spring Integration (DSL):在同一个事务中运行两个 JPA 查询

问题描述

如何让两个 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-integrationspring-integration-dsl

解决方案


好吧,看来我只需要将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();
    }

推荐阅读