如何处理Spring集成流(JavaDSL)的事务

如何处理Spring集成流(JavaDSL)的事务,java,spring,spring-integration,spring-integration-dsl,Java,Spring,Spring Integration,Spring Integration Dsl,如何在spring集成(JavaDSL)中为完整流定义事务 通过Spring集成,我们可以定义一个示例流: @Bean public IntegrationFlow myMessageFromMessageAmqpInboundFlow() { return IntegrationFlows.from(myInboundChannel) .transform(aMessageTransformer) .transform(anotherMe

如何在spring集成(JavaDSL)中为完整流定义事务

通过Spring集成,我们可以定义一个示例流:

@Bean
public IntegrationFlow myMessageFromMessageAmqpInboundFlow() {
    return IntegrationFlows.from(myInboundChannel)
            .transform(aMessageTransformer)
            .transform(anotherMessageTransformer)
            .channel(anOutputChannel)
            .get();
}
我需要一个超过整个流程的事务。当前,当我使用“aMessageTransformer”访问数据库时,事务将在处理此消息转换器后关闭。 但我需要一个在处理“anotherMessageTransformer”时仍未提交的事务

我希望我只需要添加一个“@Transactional”(或@Transactional(propagation=propagation.REQUIRED,readOnly=true))

但这会导致“anotherMessageTransformer”中出现“无会话异常”

您需要遵循这一点,因此将其添加到您的流程中:

.transform(aMessageTransformer, e -> e.transactional(true))
其中,
.transactional()
是关于:

/**
 * Specify a {@link TransactionInterceptor} {@link Advice} with default
 * {@code PlatformTransactionManager} and {@link DefaultTransactionAttribute} for the
 * {@link MessageHandler}.
 * @param handleMessageAdvice the flag to indicate the target {@link Advice} type:
 * {@code false} - regular {@link TransactionInterceptor}; {@code true} -
 * {@link org.springframework.integration.transaction.TransactionHandleMessageAdvice}
 * extension.
 * @return the spec.
 */
public S transactional(boolean handleMessageAdvice) {
TransactionHandleMessageAdvice
表示:

* When this {@link Advice} is used from the {@code request-handler-advice-chain}, it is applied
 * to the {@link MessageHandler#handleMessage}
 * (not to the
 * {@link org.springframework.integration.handler.AbstractReplyProducingMessageHandler.RequestHandler#handleRequestMessage}),
 * therefore the entire downstream process is wrapped to the transaction.

首先,您的消息流是否发生在同一个线程中?
* When this {@link Advice} is used from the {@code request-handler-advice-chain}, it is applied
 * to the {@link MessageHandler#handleMessage}
 * (not to the
 * {@link org.springframework.integration.handler.AbstractReplyProducingMessageHandler.RequestHandler#handleRequestMessage}),
 * therefore the entire downstream process is wrapped to the transaction.