Skip to content

Kafka transaction not being commited when initiated in afterCommit of other transaction  #2393

@dmkaw

Description

@dmkaw

Affects Version(s):

2.9.0 (an earlier - starting 2.5.17, 2.6.12, 2.7.9 and 2.8.0)

We are using transaction synchronization mechanism for scheduling some business logic beforeCommit, afterCommit etc. (hibernate transaction), in this logic we sometimes are sending transactional kafka messages
Everything was working fine with kafka 2.7.1 + spring-kafka 2.7.6 (Spring boot 2.5.4)

After upgrading to kafka 3.2.0 + spring-kafka 2.9.0 we started to receive errors:

beginTransaction failed: CloseSafeProducer [delegate=org.apache.kafka.clients.producer.KafkaProducer@238e30ae]

java.lang.IllegalStateException: TransactionalId tx-fsm-core-jIAGrMnP58-0: Invalid transition attempted from state IN_TRANSACTION to state IN_TRANSACTION

I believe, problem was introduced in #2005 - in ProducerFactoryUtils commit on resourceHolder was moved from afterCompletion method to processResourceAfterCommit method.
Problem now is, that in TransactionSynchronization.afterCommit() kafka transaction is being attached to main transaction (hibernate) which is still active, but already commited, and because of that it is not being commited afterwards (main transaction afterCommit method was already fired, and resourceHolder commit method will not be executed)

Problem exist only when sending kafka messages in afterCommit.
beforeCommit, and synchronous processing (without transaction synchronization) is working fine.

To Reproduce

Explained above

Expected behavior

Kafka transaction properly commited when invoked in other transaction synchronization afterCommit method.

Sample

https://github.com/dmkaw/kafka-transaction-issue

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions