springspring-boottransactionsspring-integrationspring-transactions

How do I make the update query executed by JdbcPollingChannelAdapter transactional?


@Configuration
@EnableIntegration

public class IntegrationMain {
@Bean
public MessageChannel aChannel() {
    return new DirectChannel();
}
@Splitter(inputChannel = "inChannel", outputChannel = "outChannel")
public List<myEnity> splitter(Message message) {
}
@Bean
@InboundChannelAdapter(value = "inChannel", poller = @Poller(fixedDelay = "2000"))
public MessageSource<?> pollDbForQuery(DataSource dataSource) {
    JdbcPollingChannelAdapter myAdapter = new JdbcPollingChannelAdapter(dataSource, SQL); 
    myAdapter.setRowMapper();
    myAdapter.setUpdateSql("update query");
    retrun adapter;
}
}

I want to make the update query that is executed by the jdbcPollingadapter to become transactional. I saw some examples on the web but they were using a builder for Pollers and provide transaction support by setting a method called setAdvice(). I'm not able to achieve something similar using the JdbcPollingChannelAdapter right now.


Solution

  • See @Poller annotation:

    /**
     * @return The {@link org.springframework.integration.scheduling.PollerMetadata} bean
     * name.
     */
    String value() default "";
    

    So, you need to configure a PollerMetadata bean with a PeriodicTrigger for that Duration.ofSeconds(2). And there is a setAdviceChain() option where you can inject a TransactionInterceptor.

    And that is clearly explained in docs: https://docs.spring.io/spring-integration/docs/current/reference/html/configuration.html#configuration-using-poller-annotation