Is there any possibility to use declarative tx management (via @Transactional) with @KafkaListener annotated method ? I would like to use it in order to, for example, define separate tx timeout per listener. My setup is as follows:
TransactionManager:
@Bean
@ConditionalOnBean(value = {HibernateTransactionManager.class})
public ChainedKafkaTransactionManager<Object, Object> chainedHibernateTm(KafkaTransactionManager<String, String> kafkaTransactionManager,
org.springframework.orm.hibernate5.HibernateTransactionManager hibernateTransactionManager) {
return new ChainedKafkaTransactionManager<>(
kafkaTransactionManager,
hibernateTransactionManager);
}
KafkaListener:
@KafkaListener(topic = "my_topic")
@Transactional(timeout = 5)
public void handleMessage(SomeMessage message){
}
The problem is - KafkaMessageListenerContainer creates it's own transaction before such method is invoked - it uses its own TransactionTemplate:
@Nullable
private TransactionTemplate determineTransactionTemplate() {
return this.transactionManager != null
? new TransactionTemplate(this.transactionManager)
: null;
}
TransactionInterceptor is not used. So how to set specific tx timeout for concrete @KafkaListener method ?