Keep transaction within Spring Integration flow

后端 未结 2 462
猫巷女王i
猫巷女王i 2020-12-10 19:40

Inboud gateway:



        
相关标签:
2条回答
  • 2020-12-10 20:10

    If it's a MessagingGateway injected into your code, you can simply start your transaction at the gateway and, since all channels are direct, the entire flow will run in the same transaction. Simply annotate your gateway method with @Transactional and add <tx:annotation-driven/> or @EnableTransactionManagement to your context (and a transaction manager).

    Or you can start your transaction even earlier if you want other stuff in the transaction...

    @Transactional
    public void foo() {
        ...
        Object reply = myGateway.exchange(Object foo);
        ...
    }
    

    Just be sure to invoke foo() from another bean so that the class containing foo() is wrapped in a transaction proxy (by @EnableTransactionManagement or <tx:annotation-driven/>).

    If it's a gateway such as an http inbound gateway, add an @Transactional gateway after the inbound gateway to start the transaction. (Add a service-activator that refs a <gateway/> that exchanges Message<?> and is annotated with @Transactional).

    0 讨论(0)
  • 2020-12-10 20:12

    One more amazing trick without writing any Java code:

    <channel id="input"/>
    
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="bean(input)"/>
    </aop:config>
    
    <tx:advice id="txAdvice">
        <tx:attributes>
         <tx:method name="send"/>
        </tx:attributes>
    </tx:advice>
    

    With this all your direct singlethreaded message flow will be wrapped to the TX on the message sending to the channel input

    0 讨论(0)
提交回复
热议问题