1. 程式人生 > >最簡單理解Mysql共享、排他鎖和樂觀、悲觀鎖

最簡單理解Mysql共享、排他鎖和樂觀、悲觀鎖

共享鎖
select * from xx where id = 10 lock in share mode

排他鎖
select * from xx where id = 10 for update

樂觀鎖
select num,version from xx where id = 10
update xx set num=num-1 where id =10 and xx.version = 32

悲觀鎖
select num from xx where id = 10 for update
update xx set num=num-1 where id = 10


樂觀鎖和悲觀鎖不是真實存在的鎖,是兩個使用場景,其中悲觀鎖用到了排它鎖,樂觀鎖沒有用鎖,可能提交失敗,可嘗試多次重試