NOTE

Database Optimistic Locking and Pessimistic Locking

1. Pessimistic Locking 2. Optimistic Locking Optimistic locking assumes that data conflicts generally will not occur, so conflicts are formally detected only when data is submitted for update. Version Number Add a version field. Query once first. When updating, add update xxx set version=version+1 where version=version to the original statement; if it fails, loop and retry. 3. Reference Database First-Type and Second-Type Lost Updates - Tencent Cloud

DatabasesCreated Updated 1 min readhistorical

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 conflicts are formally detected only when data is submitted for update.

  • Version number
    • Add a version field. Query once first. When updating, add update xxx set version=version+1 where version=version to the original statement; if it fails, loop and retry.

3. Reference

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub