spring-bootspring-integrationspring-transactionsspring-dslspring-integration-dsl

Spring Integration DSL adding mid flow transaction


I want to make specific part of flow as transactional. For instance, I want to make the first two transform operation in one transactional block. Here is the flow code that I use:

@Bean
public IntegrationFlow createNumberRange() {

    return IntegrationFlows.from("npEventPubSubChannel")

            .transform(...) 
            .transform(...)// should be transactional with above transform together
            .transform(...) // non transactional
            .handle((payload, headers) -> numbRepository.saveAll(payload))
            .get();

}

I found a workaround as adding another handle and directing flow to transactional gateway like this one:

.handle("transactionalBean", "transactionalMetod") //Then implemented messagingGateway which consists of transactional method.

I also found mid flow transactional support but couldn't find an example to work on.

Is there an elegant solution rather than directing to another gateway in the middle of the flow?


Solution

  • If you want to wrap two transformers into the transaction, you don't have choice unless hide that call behind transactional gateway. That is fully similar when you do raw Java:

    @Transactional
    void myTransactionalMethod() {
        transform1();
        transform2();
    }
    

    I'm sure you are agree with me that we always have to do this way to have them both in the same transaction.

    With Spring Integration Java DSL you can do this though:

    .gateway(f -> f
                .transform(...)
                .transform(...),
            e -> e.transactional())
    

    Do you agree it is similar to what we have with the raw Java and not so bad from the elegance perspective?