Search code examples
spring-integrationspring-transactionsspring-integration-dsl

Use of ConsumerEndpointSpec.transactional() in Spring Integration DSL


I have a question about the behavior of transactional() in the following example:

    @Bean
    IntegrationFlow myFlow(
            EntityManagerFactory entityManagerFactory,
            TransactionManager transactionManager
    ) {
        return IntegrationFlows.from(MY_CHANNEL)
                .routeToRecipients(route -> route
                        .recipientFlow(flow -> flow
                                .handle(Jpa.updatingGateway(entityManagerFactory)
                                        .namedQuery(DELETE_EVERYTHING)))
                        .recipientFlow(flow -> flow
                                .handle(Jpa.updatingGateway(entityManagerFactory)))
                        .transactional(transactionManager))
                .get();
    }

The idea is that I'm first deleting the contents of a database table, and immediately after I'm filling that same table with new data. Will .transactional() in this example make sure that the first step (deletion) is only committed to the DB if the second step (inserting new data) is successful? What part of the documentation can I refer to for this behavior?


Solution

  • You need to read this documentation: https://docs.spring.io/spring-integration/docs/current/reference/html/transactions.html#transactions.

    You assumption is correct as long as both recipients are performed on the same thread and, therefore, sequentially.

    The TransactionInterceptor is going to be applied for the RecipientListRouter.handleMessage(). And that's where that "auction" for recipient happens.

    Do you have any problem with that configuration since you have came to us with such a question?