0

I could not make spring-cloud-stream-binder-kafka work for following use case:

Start @transaction (Rest controller) DB update/inserts Send Kafka message

Before the transaction has committed, the consumer (configured with @EnableBinding and @StreamListener) is able to read the records. This consumer is already configured with read_committed isolation level.

I'm not sure is it an issue or any configuration from my side.

Tried to configure the bean, ChainedTransactionManager, but had some other problems.

santhosh
  • 33
  • 1
  • 5

1 Answers1

0

The following worked for me

@Bean(name = "chainedTransactionManager")
  @Primary
  public PlatformTransactionManager chainedTransactionManager(JpaTransactionManager jpaTM,
      BinderFactory binders) {
    Binder<MessageChannel,?,?> binder = binders.getBinder("kafka", MessageChannel.class);
    if (binder instanceof KafkaMessageChannelBinder) {
      ProducerFactory<byte[], byte[]> pf =
          ((KafkaMessageChannelBinder) binder).getTransactionalProducerFactory();
      KafkaTransactionManager<byte[], byte[]> ktm = new KafkaTransactionManager<>(pf);
      ktm.setTransactionSynchronization(
          AbstractPlatformTransactionManager.SYNCHRONIZATION_ON_ACTUAL_TRANSACTION);
      return new ChainedKafkaTransactionManager<Object, Object>(jpaTM, ktm);
    } else {
      return jpaTM;
    }
  }

Can get more details at https://github.com/spring-cloud/spring-cloud-stream-binder-kafka/issues/729

santhosh
  • 33
  • 1
  • 5