建表语句mysql
CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `orderNum` varchar(255) NOT NULL, `mobile` varchar(255) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `un_name` (`name`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
INSERT INTO `user` VALUES ('2', 'a', '1', '131');
INSERT INTO `user` VALUES ('4', 'b', '2', '132');
INSERT INTO `user` VALUES ('6', 'c', '3', '186');
使用命令mysql -hx.x.x.x -uroot -p123456 打开链接A,Bweb
链接A:
begin; //查询事务:无
select * from user for update; //查询事务:1个;查询锁:无
表太宽,下面两个图是一条记录:
sql
链接B:
begin;
select * from user for update; //查询事务:2个;锁:2个, 以下图
svg
链接B:等待超时
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
再次查询事务:2个;锁:0个测试
链接B: commit; //B事务结束,只剩下A事务。spa
链接A:
begin;
update user set orderNum=2 where mobile like ‘13%’; //B会阻塞
//update user set orderNum=2; //效果同上。
//select count(*) from user for update; //效果同上
//select count(*) from user; //不会阻塞链接B的for update查询3d
链接B:
select * from user for update;
select * from user //非for update查询永不阻塞。rest
排它锁 for updatecode
锁表
链接A: LOCK TABLES user WRITE; // show OPEN TABLES where In_use > 0; 下图
链接B: update user set orderNum=2; //阻塞
链接A: UNLOCK TABLES;
链接B: 阻塞消除orm