# rpc错误：代码=中止 描述=事务已中止：检测到并发修改

- **ID:** `go/grpc-aborted-transaction-rollback`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

事务因与另一个并发事务冲突而中止，常见于分布式数据库。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.0 | active | — | — |
| 1.1 | active | — | — |

## 解决方案

1. **Retry the transaction with exponential backoff and jitter.** (85% 成功率)
   ```
   for i := 0; i < 5; i++ {
    err := client.Transaction(ctx, req)
    if err == nil { return nil }
    if status.Code(err) != codes.Aborted { return err }
    time.Sleep(time.Duration(100*(1<<i)) * time.Millisecond)
}
   ```
2. **Reduce contention by using smaller transactions or optimistic locking.** (75% 成功率)
   ```
   Design the system to minimize overlapping transactions, e.g., partition data by user ID.
   ```

## 无效尝试

- **Ignore the error and assume partial success.** — The transaction was fully aborted; no changes were applied. (100% 失败率)
- **Immediately retry without any delay.** — Immediate retry may hit the same conflict again, especially under high contention. (70% 失败率)
