MySQL建议使用悲观锁吗_mysql悲观锁解决并发问题

本文详细介绍了MySQL悲观锁的概念及使用,通过实例展示了如何在并发环境下解决数据一致性问题。悲观锁通过`SELECT ... FOR UPDATE`语句实现,确保数据在处理过程中不被其他事务修改。需要注意的是,使用悲观锁需关闭MySQL的自动提交,并谨慎处理锁的粒度,避免全表锁定。
摘要由CSDN通过智能技术生成

整个数据处理过程,将数据处于锁定状态。悲观锁的实现,依靠数据库提供的锁机制。

商品t_goods表中有一个字段status,status为0代表商品未被下单,status为1代表商品已经被下单,那么对某个商品下单时必须确保该商品status为0。假设商品的id为1。

不使用锁

//1.查询出商品信息select status from t_goods where id=1;//2.根据商品信息生成订单insert into t_orders (goods_id) values (1);//3.修改商品status为2update t_goods set status=2;

种场景在高并发访问的情况下很可能会出现问题。

使用悲观锁来实现

商品信息从查询出来到修改,中间有一个处理订单的过程,使用悲观锁的原理就是,在查询出goods信息后就把当前的数据锁定,直到修改完毕后再解锁。在这个过程中,因为goods被锁定,就不会出现有第三者来对其进行修改。

注:使用悲观锁,必须关闭mysql数据库的自动提交属性,因为MySQL默认使用autocommit模式,也就是说,当执行一个更新操作后,MySQL会立刻将结果进行提交。

使用命令设置MySQL为非autocommit模式: set autocommit=0;

就可以执行正常业务,具体如下:

/0.开始事务begin;/begin work;/start transaction; (三者选一就可以)//1.查询出商品信息select status from t_goods where id=1 for update;//2.根据商品信息生成订单insert into t_orders (goods_id) values (1);//3.修改商品status为2update t_goods set status=2;//4.提交事务commit;/commit work;

使用select…for update的方式,就通过数据库实现悲观锁。此时在t_goods表中,id为1的 那条数据就被锁定,其它的事务必须等本次事务提交之后才能执行。这样可以保证当前的数据不会被其它事务修改。

注意的是,在事务中,只有SELECT ... FOR UPDATE 或LOCK IN SHARE MODE 同一笔数据时会等待其它事务结束后才执行,一般SELECT ... 则不受此影响。当执行select status from t_goods where id=1 for update;后。在另外的事务中如果再次执行select status from t_goods where id=1 for update;则第二个事务会一直等待第一个事务的提交,此时第二个查询处于阻塞的状态,但是如果是在第二个事务中执行select status from t_goods where id=1;则能正常查询出数据,不会受第一个事务的影响。

MySQL select…for update的Row Lock与Table Lock

使用select…for update会把数据给锁住,不过需要注意一些锁的级别,MySQL InnoDB默认Row-Level Lock,所以只有「明确」地指定主键,MySQL 才会执行Row lock (只锁住被选取的数据) ,否则MySQL 将会执行Table Lock (将整个数据表单给锁住)。

例1: (明确指定主键,并且有此数据,row lock)

console1:查询出结果,但是把该条数据锁定

begin;select * from t_goods where id=1 for update;

console2:查询被阻塞

select * from t_goods where id=1 for update;

console2:如果console1长时间未提交,则会报错

select * from t_goods where id=1 for update

> 1205 - Lock wait timeout exceeded; try restarting transaction

> 时间: 51.023s

例2: (明确指定主键,若查无此数据,无lock)

console1:查询结果为空

begin;select * from t_goods where id=3 for update;

console2:查询结果为空,查询无阻塞,说明console1没有对数据执行锁定

mysql> select * from t_goods where id=3 for update;

例3: (无主键,table lock)

console1:查询name=小米笔记本 的数据,查询正常

begin;select * from t_goods where good_name='小米笔记本' for update;

console2:查询name=小米手环 的数据,查询阻塞,说明console1把表给锁住

begin;select * from t_goods where good_name='小米手环' for update;

console2:若console1长时间未提交,则查询返回为空

select * from t_goods where good_name='小米手环' for update

> Affected rows: 0

> 时间: 51.022s

例4: (主键不明确,table lock)

mysql> select * from t_goods where id>0 for update; //表锁

mysql> select * from t_goods where id<>1 for update; //表锁

需要注意的是,除了主键外,使用索引也会影响数据库的锁定级别,修改t_goods表,给status字段创建一个索引

例5: (明确指定索引,并且有此数据,row lock)

console1:

select * from t_goods where status=1 for update;

console2:查询status=1的数据时阻塞,超时后返回为空,说明数据被console1锁定

mysql> select * from t_goods where status=1 for update;

Query OK,-1 rows affected

console2:查询status=2的数据,能正常查询,说明console1只锁住了行,未锁表

mysql> select * from t_goods where status=2 for update;

例6: (明确指定索引,若查无此数据,无lock)

console1:查询status=3的数据,返回空数据

mysql> select * from t_goods where status=3 for update;

console2:查询status=3的数据,返回空数据

mysql> select * from t_goods where status=3 for update;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值