springspring-bootspring-kafkaspring-transactions

Spring Boot 4 + Kafka in transaction mode


I created a simple application which just sends in a loop messages to Kafka using KafkaTemplate with transaction mode. But, unfortunately, my code doesn't work with @Transactional in the new Spring. I tried the same code with Spring 3.5.8, and everything worked.

application.yml

spring:
  kafka:
    producer:
      bootstrap-servers:
        - http://localhost:9092
        - http://localhost:9192
        - http://localhost:9292
      key-serializer: org.apache.kafka.common.serialization.IntegerSerializer
      value-serializer: org.apache.kafka.common.serialization.StringSerializer
      transaction-id-prefix: tx-
      properties:
        enable.idempotence: true
      acks: all

service

private final KafkaTemplate<Integer, String> kafkaTemplate;

    @Override
    @Transactional
    public void foo() {
        for (int i = 0; i < 10; i++) {
            kafkaTemplate.send("foo", i, "Foo #" + i);
        }
    }

The service is called from controller.

Probably I missed any dependencies during migrating but I'm not sure.

PS: with calling executeInTransaction everything also works, even in Spring 4.0.0.


Solution

  • I think I managed to reproduce your issue, and adding the org.springframework.transaction.annotation.EnableTransactionManagement annotation helped to solve the problem.

    Everything definitely worked after that, but I didn’t find any mention of this in the migration guide, so we’ll need to dig deeper.

    Hope this helped solve the problem.