# 调用 TransactWriteItems 操作时出错 (TransactionCanceledException)：事务已取消，请参考取消原因了解具体细节 [ConditionalCheckFailed, None]

- **ID:** `aws/dynamodb-transaction-canceled`
- **领域:** aws
- **类别:** data_error
- **错误码:** `TransactionCanceledException`
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

DynamoDB 事务中一个或多个项目的条件表达式失败，导致整个事务回滚。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| DynamoDB API 2012-08-10 | active | — | — |
| AWS SDK v2.21.0 | active | — | — |

## 解决方案

1. ```
   从错误中解析取消原因数组。对于每个项目，检查条件表达式逻辑。Python 示例：`error.response['Error']['Code'] == 'TransactionCanceledException' and 'ConditionalCheckFailed' in error.response['Error']['Message']`
   ```
2. ```
   使用 DynamoDB Streams 捕获前镜像，并在重试前离线验证条件逻辑。
   ```
3. ```
   简化事务：减少每个事务的项目数，或拆分为单独的 PutItem/UpdateItem 调用并添加重试逻辑。
   ```

## 无效尝试

- **Retry the transaction with exponential backoff without checking conditions** — Retrying a transaction with a failing condition will keep failing; the condition must be fixed first. (95% 失败率)
- **Increase DynamoDB write capacity units** — Throughput is not the issue; this is a condition check failure, not a throttling error. (85% 失败率)
- **Ignore cancellation reasons and assume transient error** — Cancellation reasons like 'ConditionalCheckFailed' indicate a specific logic error that must be addressed. (90% 失败率)
