Optimistic and pessimistic locking
Optimistic lock: Very optimistic, always think that there will be no problems, no matter what you do, do not lock, if there is a problem, update the value test again.
Pessimistic lock: very pessimistic, always think that there will be a problem, no matter what you do, you will be locked, and then operate.
One, optimistic lock plugin
Applicable scenario:
When updating a record, hope that this record has not been updated by others.
Optimistic lock implementation:
- When fetching the record, get the current version
- When updating, bring this version
- When performing an update, set version = newVersion where version = oldVersion
- If the version is wrong, the update fails
乐观锁:1.先查询,获得版本号 version = 1
A线程:
update user set name = "刘备",version =<