Spring Boot 2.7.2 with Spring Kafka fails during initialisation with Exception: The 'ProducerFactory' must support transactions`
See original GitHub issueI’m using Spring Boot v2.7.2 and the latest version of Spring Kafka provided by spring-boot-dependencies:
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
I want the app to load all configuration from application.yaml, hence I created the beans with this bare minimum configuration:
public class KakfaConfig {
@Bean
public ProducerFactory<Integer, FileUploadEvent> producerFactory() {
return new DefaultKafkaProducerFactory<>(Collections.emptyMap());
}
@Bean
public KafkaTemplate<Integer, FileUploadEvent> kafkaTemplate() {
return new KafkaTemplate<Integer, anEvent>(producerFactory());
}
}
It seems to work as expected and loads the configuration from the application.yaml below:
spring:
application:
name: my-app
kafka:
bootstrap-servers: localhost:9092
producer:
client-id: ${spring.application.name}
# transaction-id-prefix: "tx-"
template:
default-topic: my-topic
However, if I uncomment the transaction-id-prefix line, the application fails to start with the exception
java.lang.IllegalArgumentException: The 'ProducerFactory' must support transactions
The documentation in here reads
If you provide a custom producer factory, it must support transactions. See ProducerFactory.transactionCapable().
The only way I have found to make it work is adding the transaction prefix programmatically as shown in the snippet below:
@Bean
public ProducerFactory<Integer, FileUploadEvent> fileUploadProducerFactory() {
var pf = new DefaultKafkaProducerFactory<Integer, FileUploadEvent>(Collections.emptyMap());
pf.setTransactionIdPrefix("tx-");
return pf;
}
Any thoughts on how I can configure everything using application properties file?
Issue Analytics
- State:
- Created a year ago
- Comments:7 (4 by maintainers)

Top Related StackOverflow Question
AIUI, the intent is that transactions should be used and that the
ProducerFactoryshould support them. Thetransaction-id-prefixproperty can be set and this results in the auto-configuration of thekafkaTransactionManagerbean. However, if you define your ownProducerFactory(to constrain the types, for example) there’s no built-in way to have thetransaction-id-prefixapplied to thatProducerFactory.@dbaltor One option could be to use
@Valueto inject the transaction ID prefix:That’s very interesting. Thank you @garyrussell ! At this time, I think I do need to create my own beans as I’m using bespoken (De)Serializers in both of my Producer and Consumer factories.