乐观锁的惯例操作
select rev from table where id = $id
update table set name=$name, rev=rev+1 where id = $id and rev=$rev
此时有两个线程 a 和 b 来批改用户名
分成 4 中状况
1、
a |
b |
select rev from table where id = $id |
select rev from table where id = $id |
update table set name=$name, rev=rev+1 where id = $id and rev=$rev rev=2 |
|
|
update table set name=$name, rev=rev+1 where id = $id and rev=$rev 失败 |
|
|
a |
b |
select rev from table where id = $id |
select rev from table where id = $id |
|
update table set name=$name, rev=rev+1 where id = $id and rev=$rev rev=2 |
update table set name=$name, rev=rev+1 where id = $id and rev=$rev 失败 |
|
|
|
a |
b |
select rev from table where id = $id |
|
update table set name=$name, rev=rev+1 where id = $id and rev=$rev rev=2 |
|
|
select rev from table where id = $id |
|
update table set name=$name, rev=rev+1 where id = $id and rev=$rev rev=3 |
a |
b |
|
select rev from table where id = $id |
|
update table set name=$name, rev=rev+1 where id = $id and rev=$rev rev=2 |
select rev from table where id = $id |
|
update table set name=$name, rev=rev+1 where id = $id and rev=$rev rev=3 |
|