乐观锁

乐观锁的常规操作
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

你可能感兴趣的:(redismysql数据库)