假设现在库存为1,现在有A和B同时购买
先开启一个事务
begin;
select stock from good where id=1 for update;
最后在执行
update good set stock=stock-1 where id=1
最后在
commit
但是这个时候B也是select stock from good where id=1 for update;注意:for update不能省略..这个时候会出现被锁住,无法被读取.
所以这就能够保证了商品剩余数量为1的一致性.