分布式锁

1.数据库中乐观锁与悲观锁的例子

悲观锁:一般都需要依赖数据库的锁实现。使用select...for update,锁住行,查询出数据后进行更新

针对mysql Innodb引擎,会使用行锁或者表锁

乐观锁:使用版本号列实现

select version from table where id= 1;//假设读取的version=1

update table set num = 10,version= version+1 where id= 1 and version =1;//如果有多个事务同时提交,只有一个成功

原文地址:https://www.cnblogs.com/enjoy-ourselves/p/5934021.html