# org.apache.kafka.common.errors.TransactionalCoordinatorFencedException: 纪元 5 的事务协调器已被较新的纪元 6 隔离

- **ID:** `kafka/transactional-coordinator-fenced`
- **领域:** kafka
- **类别:** protocol_error
- **验证级别:** ai_generated
- **修复率:** 82%

## 根因

事务协调器被具有更高纪元的新的协调器替换（例如，在领导变更后），导致旧协调器的请求被拒绝为隔离状态。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Kafka 2.8.0 | active | — | — |
| Kafka 3.0.0 | active | — | — |
| Kafka 3.4.0 | active | — | — |
| Kafka 3.6.0 | active | — | — |

## 解决方案

1. ```
   Handle the exception in the producer by retrying the transaction with a new transactional ID or by resetting the producer:

producer.initTransactions();
try {
    producer.beginTransaction();
    // Send messages
    producer.commitTransaction();
} catch (TransactionalCoordinatorFencedException e) {
    // Close and recreate the producer to get a fresh coordinator
    producer.close();
    producer = createNewProducer();
    producer.initTransactions();
    // Retry the transaction
}
   ```
2. ```
   Ensure the transactional.id is unique per producer instance and that the broker's transaction.state.log.replication.factor is sufficient to avoid coordinator failures:

transaction.state.log.replication.factor=3

Also monitor the broker logs for coordinator changes and consider increasing the number of transaction coordinator threads.
   ```

## 无效尝试

- **Increase the transaction.timeout.ms to a very high value** — Transaction timeout controls how long a transaction can remain open, not coordinator fencing; it does not prevent epoch conflicts. (85% 失败率)
- **Disable idempotent producer and transactions** — This avoids the error but disables exactly-once semantics, which may be required for the application; it is a workaround that changes behavior. (70% 失败率)
