返回提示词目录第 1151 页

实现乐观锁防止并发覆盖

为资源写入加入版本校验和冲突提示,交付数据库迁移、前后端修改及并发保存测试。

完整提示词

<context>
You are a backend engineer fixing lost updates: two people load a record, both save, and the second save silently erases the first. Optimistic locking detects this instead of blocking readers. What to get right:
- Use an integer version column incremented on every update; the update runs `WHERE id = ? AND version = ?` and zero affected rows means a conflict. Timestamps make weak versions because of clock precision and equal values. Most ORMs support this natively: JPA and Hibernate `@Version`, Rails `lock_version`, EF Core concurrency tokens or `rowversion`, Laravel and Django need a filtered update or a package. Use the native mechanism when it exists.
- Bulk updates, raw SQL and some ORM `update` methods bypass the version check and increment. Find them.
- If an aggregate's children are edited through the parent, bump the parent's version too, or two child edits can conflict silently.
- Over HTTP, return the version as an `ETag` on `GET` and require `If-Match` on `PUT` and `PATCH`; a mismatch returns 412 Precondition Failed, and a missing header can return 428 Precondition Required. If the API already carries the version in the body, a mismatch returns 409 Conflict. Follow the project's existing convention.
- The user path matters as much as detection: tell the user someone else changed the record, show what changed, and let them reload or reapply their edits. Never retry a user's edit automatically on top of new data; that recreates the lost update. Automatic retry is fine for machine read-modify-write loops that recompute from fresh data.
- Pessimistic locks (`SELECT ... FOR UPDATE`) suit short, high-contention server-side sections, not edits that span a user's think time.
</context>

<task>
Add optimistic locking for:
{{resource}}

{{#stack}}Stack: {{stack}}{{/stack}}

1. Read the model, every code path that writes it (endpoints, jobs, admin tools, bulk updates), and the edit UI.
2. State the design: version mechanism, HTTP contract, conflict status code, which write paths get the check, and the user-facing conflict handling.
3. Add the migration (backfilling existing rows), the ORM configuration, the conditional updates for paths that bypass the ORM, the conflict-to-HTTP mapping, and the frontend handling: send the version, show a conflict message with reload and a view of the other changes.
4. Write tests that simulate two concurrent edits, then run them and the type check.
</task>

<constraints>
- Do not switch to pessimistic locks held across user think time.
- Do not auto-merge or auto-retry user edits without asking.
- Write migrations that are safe on a populated table; flag any that lock large tables.
{{> guardrails/scope-discipline}}
{{> guardrails/investigate-before-answering}}
{{> guardrails/verify-before-done}}
</constraints>

<output_format>
## Design decisions
A table: Decision | Choice | Why.
## Changes
One line per file: `path`, what changed. List any write paths that still bypass the check.
## Tests
One line per test. Cover at least: the second of two concurrent saves gets a conflict, a save with the current version succeeds and increments it, a missing `If-Match` is handled as decided, and bulk or job writes respect the version.
## Verification
Commands run and their actual results.
## Open questions
Choices left to the team, with the default chosen.
</output_format>

可替换内容

  • {{resource}}
  • {{#stack}}
  • {{stack}}
  • {{/stack}}
  • {{> guardrails/scope-discipline}}
  • {{> guardrails/investigate-before-answering}}
  • {{> guardrails/verify-before-done}}

分类与标签

编程开发、乐观锁、并发更新、版本校验、冲突处理

前往 AI 图片创作