mongodb
runtime_error
ai_generated
true
MongoServerError:事务 1234 因写入冲突而中止
MongoServerError: Transaction 1234 was aborted due to a write conflict
ID: mongodb/transaction-abort-due-to-write-conflict
85%修复率
88%置信度
1证据数
2023-06-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| mongodb-5.0 | active | — | — | — |
| mongodb-6.0 | active | — | — | — |
| mongodb-7.0 | active | — | — | — |
| mongodb-8.0 | active | — | — | — |
根因分析
两个并发事务尝试修改同一文档,导致一个事务因乐观并发控制而中止。
English
Two concurrent transactions attempted to modify the same document, causing one to abort due to optimistic concurrency control.
官方文档
https://www.mongodb.com/docs/manual/core/transactions-production-consideration/#write-conflicts解决方案
-
Implement a retry loop in the application: catch the write conflict error and re-run the transaction up to 3 times with exponential backoff.
-
Reduce transaction scope by accessing fewer documents or using smaller batch sizes to minimize contention.
-
Use a deterministic ordering for document updates within transactions to prevent deadlocks and conflicts.
无效尝试
常见但无效的做法:
-
50% 失败
Disabling transactions entirely removes atomicity guarantees for multi-document operations.
-
50% 失败
Increasing wtimeoutMS does not resolve write conflicts; it only affects write concern timeouts.
-
50% 失败
Setting retryWrites=true does not apply to transactions; transactions must be retried manually.