# org.apache.kafka.common.errors.ProducerFencedException：由于分区重新分配，生产者纪元被隔离

- **ID:** `kafka/transactional-producer-epoch-fenced-during-partition-reassignment`
- **领域:** kafka
- **类别:** protocol_error
- **验证级别:** ai_generated
- **修复率:** 81%

## 根因

事务性生产者的纪元被无效化，因为它正在写入的分区被重新分配到不同的代理，导致事务协调器隔离了该生产者。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Kafka 3.3.0 | active | — | — |
| Kafka 3.5.0 | active | — | — |
| Kafka 3.7.0 | active | — | — |

## 解决方案

1. ```
   Initialize a new transactional producer with a new transactional.id after catching the exception. Example in Java: try { producer.initTransactions(); } catch (ProducerFencedException e) { producer.close(); producer = createNewProducerWithNewTransactionalId(); producer.initTransactions(); }
   ```
2. ```
   Ensure partition reassignment is completed before starting transactional writes. Use 'bin/kafka-reassign-partitions.sh --bootstrap-server localhost:9092 --verify --reassignment-json-file reassign.json' to confirm completion.
   ```

## 无效尝试

- **Setting transaction.timeout.ms to 300000 (5 minutes)** — Increasing transaction.timeout.ms does not prevent epoch fencing; it only delays transaction expiration. (90% 失败率)
- **Restarting the producer application without changing transactional.id** — Restarting the producer without resetting the transactional.id will still use the fenced epoch. (95% 失败率)
