关于数据库:乐观锁

58次阅读

共计 843 个字符,预计需要花费 3 分钟才能阅读完成。

乐观锁的惯例操作
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、

ab
select rev from table where id = $idselect 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  失败

ab
select rev from table where id = $idselect 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  失败

ab
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

ab
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
正文完
 0