在代码评审中指出并发问题
日英对照例句
看中文试着说,再展开核对日语和英语。
两个请求同时到达时,都可能查不到这条记录。
- 日语
- 2つのリクエストが同時に届くと、どちらも既存のレコードを見つけられない可能性があります。
- 英语
- If two requests arrive at the same time, both can find that no record exists.
这样两个请求都可能尝试插入同一个键。
- 日语
- その結果、両方のリクエストが同じキーで挿入しようとする可能性があります。
- 英语
- Both requests can then try to insert the same key.
可以在数据库里加唯一约束,再处理重复键错误吗?
- 日语
- データベースに一意制約を追加して、キーの重複エラーを処理できないでしょうか。
- 英语
- Could we add a unique constraint and handle the duplicate-key error?
我们可以加一个并发测试,验证最终只写入一条记录。
- 日语
- 並行実行のテストを追加して、書き込まれるレコードが1件だけであることを確認できます。
- 英语
- We can add a concurrency test to verify that only one record is written.
背诵与复述
复现场景 → 原因 → 修改建议 → 测试
核对日英背诵稿
- 日语
- 2つのリクエストが同時に届くと、どちらも既存のレコードを見つけられない可能性があります。その結果、両方のリクエストが同じキーで挿入しようとする可能性があります。データベースに一意制約を追加して、キーの重複エラーを処理できないでしょうか。並行実行のテストを追加して、書き込まれるレコードが1件だけであることを確認できます。
- 英语
- If two requests arrive at the same time, both can find that no record exists. Both requests can then try to insert the same key. Could we add a unique constraint and handle the duplicate-key error? We can add a concurrency test to verify that only one record is written.
工作追问
对方说重复概率极低。解释为什么仍然需要测试这一种请求时序。