如何在Spring集成(Java DSL)中为完整流定义事务?
使用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))
@Bean
@Transactional
public IntegrationFlow myMessageFromMessageAmqpInboundFlow() {
return IntegrationFlows.from(myInboundChannel)
.transform(aMessageTransformer)
.transform(anotherMessageTransformer)
.channel(anOutputChannel)
.get();
}
但这导致'anotherMessageTransformer'中的'无会话异常'
答案 0 :(得分:2)
您需要关注此documentation,然后将其添加到您的流程中:
.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.