Skip to content

Incompatibility between the TracePlatformTransactionManager and the KafkaTransactionManager #2063

@cedric-schaller

Description

@cedric-schaller

Problem description
This problem was noted after a migration from Spring Boot 2.4.1 (which was working fine) to Spring Boot 2.6.0.

When using a Kafka transaction manager together with Sleuth, the following error occurs:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'org.springframework.boot.autoconfigure.kafka.KafkaAnnotationDrivenConfiguration': Bean instantiation via constructor failed; nested
exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate
[org.springframework.boot.autoconfigure.kafka.KafkaAnnotationDrivenConfiguration]: Constructor threw exception; nested exception is
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'kafkaTransactionManager' is expected to be of
type 'org.springframework.kafka.transaction.KafkaAwareTransactionManager' but was actually of type
'org.springframework.cloud.sleuth.instrument.tx.TracePlatformTransactionManager'

...

Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'kafkaTransactionManager' is expected to be of type 'org.springframework.kafka.transaction.KafkaAwareTransactionManager' but was actually of type 'org.springframework.cloud.sleuth.instrument.tx.TracePlatformTransactionManager'
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1390)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory$DependencyObjectProvider.getIfUnique(DefaultListableBeanFactory.java:2063)
	at org.springframework.boot.autoconfigure.kafka.KafkaAnnotationDrivenConfiguration.<init>(KafkaAnnotationDrivenConfiguration.java:94)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
	at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:211)
	... 21 common frames omitted

Reproduction
In order to reproduce the problem, please use this sample and have a local Kafka instance running on the default port.

Analysis

  • The TracePlatformTransactionManagerBeanPostProcessor wraps the bean kafkaTransactionManager of type KafkaTransactionManager in a TracePlatformTransactionManager.
  • The KafkaTransactionManager is created in KafkaAutoConfiguration if spring.kafka.producer.transaction-id-prefix is set and a type KafkaAwareTransactionManager is expected (and enforced) for this bean.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions