NOTE
Database Optimistic Locking and Pessimistic Locking
Optimistic locking checks conflicts when committing an update, while pessimistic locking locks data in advance. These notes compare both approaches and version-based implementations.
This is a historical learning note and may contain outdated or incomplete understanding.
1. Pessimistic Locking
select aaa from bbb where aaa=ccc for update
2. Optimistic Locking
Optimistic locking assumes that data conflicts generally will not occur, so it checks for conflicts only when an update is submitted.
- Version number
- Add a
versionfield. Read the data first, then incorporateupdate xxx set version=version+1 where version=versioninto the original update statement; if the update fails, loop and retry.
- Add a
3. Reference
Drag to pan, scroll to zoom, and click a node to expand or collapse it. Large maps start with deeper levels collapsed; click nodes to expand them.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub