Spring integration 使用TransactionInterceptor抛出java.lang.IllegalStateException:无法将反应性事务应用于非反应性返回类型:void

Spring integration 使用TransactionInterceptor抛出java.lang.IllegalStateException:无法将反应性事务应用于非反应性返回类型:void,spring-integration,Spring Integration,我的代码是: @Bean(R2DBC_TRANSACTION_INTERCEPTOR) public TransactionInterceptor R2DBCTransactionInterceptor(ReactiveTransactionManager reactiveTransactionManager) { return new TransactionInterceptorBuilder(true) .transactionMa

我的代码是:

  @Bean(R2DBC_TRANSACTION_INTERCEPTOR)
    public TransactionInterceptor R2DBCTransactionInterceptor(ReactiveTransactionManager reactiveTransactionManager) {
        return new TransactionInterceptorBuilder(true)
                .transactionManager(reactiveTransactionManager)
                .isolation(Isolation.READ_COMMITTED)
                .propagation(Propagation.REQUIRES_NEW)
                .build();
    }

    @ServiceActivator(inputChannel = "create invoice",
            adviceChain = {IntegrationConfiguration.R2DBC_TRANSACTION_INTERCEPTOR})
    public Mono<Invoice> createInvoice(Invoice invoice) {
        return invoiceRepository.save(invoice);
    }
@Bean(R2DBC\u事务\u拦截器)
公共事务接收器R2DBCTransactionInterceptor(ReactiveTransactionManager ReactiveTransactionManager){
返回新TransactionInterceptorBuilder(true)
.transactionManager(reactiveTransactionManager)
.isolation(隔离.READ_提交)
.propagation(propagation.REQUIRES_NEW)
.build();
}
@ServiceActivator(inputChannel=“创建发票”,
adviceChain={IntegrationConfiguration.R2DBC_TRANSACTION_INTERCEPTOR})
公共发票(发票){
返回invoiceRepository.save(发票);
}
我得到一个异常java.lang.IllegalStateException:无法将反应性事务应用于非反应性返回类型:void 在org.springframework.transaction.interceptor.transactionspectsupport.lambda$invokeWithinTransaction$0(transactionspectsupport.java:348)~[spring-tx-5.2.8.RELEASE.jar:5.2.8.RELEASE]

即使在消息传递过程中没有异常,createInvoice方法也会调用该异常

有针对这种情况提供的演示吗? 我的配置错误吗

new TransactionInterceptorBuilder(true)
参见其JavaDocs:

* When the {@code handleMessageAdvice} option is in use, this builder produces
* {@link TransactionHandleMessageAdvice} instance.
然后我们转到前面提到的
TransactionHandleMessageAdvice

 * When this {@link org.aopalliance.aop.Advice}
 * is used from the {@code request-handler-advice-chain}, it is applied
 * to the {@link org.springframework.messaging.MessageHandler#handleMessage}
 * (not to the
 * {@link org.springframework.integration.handler.AbstractReplyProducingMessageHandler.RequestHandler#handleRequestMessage}),
 * therefore the entire downstream process is wrapped to the transaction.
现在,如果我们查看一下
MessageHandler#handleMessage
,我们将看到:

void handleMessage(Message<?> message) throws MessagingException;
void handleMessage(消息消息消息)抛出消息异常;
因此,这就是注入事务拦截器的地方,也是解决方案失败的地方,因为
void
handleMessage()
返回

您确实需要考虑是否确实需要该
@ServiceActivator
下的整个子流的事务(尤其是反应性事务)

我的意思是,您可能只需要
新建TransactionInterceptorBuilder()
就可以使您的
@ServiceActivator
具有事务性,并且已经与该反应式TX管理器进行了交互,因为您确实返回了
Mono

参见其JavaDocs:

* When the {@code handleMessageAdvice} option is in use, this builder produces
* {@link TransactionHandleMessageAdvice} instance.
然后我们转到前面提到的
TransactionHandleMessageAdvice

 * When this {@link org.aopalliance.aop.Advice}
 * is used from the {@code request-handler-advice-chain}, it is applied
 * to the {@link org.springframework.messaging.MessageHandler#handleMessage}
 * (not to the
 * {@link org.springframework.integration.handler.AbstractReplyProducingMessageHandler.RequestHandler#handleRequestMessage}),
 * therefore the entire downstream process is wrapped to the transaction.
现在,如果我们查看一下
MessageHandler#handleMessage
,我们将看到:

void handleMessage(Message<?> message) throws MessagingException;
void handleMessage(消息消息消息)抛出消息异常;
因此,这就是注入事务拦截器的地方,也是解决方案失败的地方,因为
void
handleMessage()
返回

您确实需要考虑是否确实需要该
@ServiceActivator
下的整个子流的事务(尤其是反应性事务)

我的意思是,您可能只需要
新建TransactionInterceptorBuilder()
就可以使您的
@ServiceActivator
具有事务性,并且已经与该反应式TX管理器进行了交互,因为您确实返回了
Mono