Kafka生产者引发错误从状态IN_TRANSACTION到状态IN_TRANSACTION的无效转换尝试

时间:2020-01-22 16:48:46

标签: apache-kafka transactions

我的卡夫卡生产者抛出错误“尝试从状态IN_TRANSACTION到状态IN_TRANSACTION的无效转换”。这是我想要达到的目标-

KafkaProducer producer = new KafkaProducer<>(props);
producer.initTransactions();
//transaction 1
producer.beginTransaction();
//send some messages
producer.commitTransaction();

//transaction 2
producer.beginTransaction(); //here it throws an exception "Invalid transition attempted from state IN_TRANSACTION to state IN_TRANSACTION".
//send some messages
producer.commitTransaction();

producer.close();

如果我在开始事务2之前再次调用 producer.initTransactions(); ,它将引发异常“尝试从状态READY到状态INITIALIZING的无效转换”。

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

每次只需为每笔交易创建新的生产者。

KafkaProducer producer = new KafkaProducer<>(props);
producer.initTransactions();
//transaction 1
producer.beginTransaction();
//send some messages
producer.commitTransaction();
producer.close();

KafkaProducer producer = new KafkaProducer<>(props);
producer.initTransactions();

//transaction 2
producer.beginTransaction(); //here it throws an exception "Invalid transition attempted from state IN_TRANSACTION to state IN_TRANSACTION".
//send some messages
producer.commitTransaction();

producer.close();
相关问题