锁列表
- 共享与列排他锁
- 意向锁
- 记录锁
- 间隙锁
- Next-Key锁
- 插入意向锁
- AUTO-INC锁
这次我们只来讨论和实验意向锁。
插入意向锁
插入意向锁是一种间隙锁形式的意向锁,在真正执行 INSERT 操作之前设置。
当执行插入操作时,总会检查当前插入操作的下一条记录(已存在的主索引节点)上是否存在锁对象,判断是否锁住了 gap,如果锁住了,则判定和插入意向锁冲突,当前插入操作就需要等待,也就是配合上面的间隙锁或者临键锁一起防止了幻读操作。
因为插入意向锁是一种意向锁,意向锁只是表示一种意向,所以插入意向锁之间不会互相冲突,多个插入操作同时插入同一个 gap 时,无需互相等待,比如当前索引上有记录 4 和 8,两个并发 session 同时插入记录 6,7。他们会分别为(4,8)加上 GAP 锁,但相互之间并不冲突
INSERT 语句在执行插入之前,会先在 gap 中加入插入意向锁,如果是唯一索引,还会进行 Duplicate Key 判断,如果存在相同 Key 且该 Key 被加了互斥锁,则还会加共享锁,然后等待(因为这个相同的 Key 之后有可能会回滚删除,这里非常容易死锁)。等到成功插入后,会在这条记录上加排他记录锁
实验
建表语句:
CREATE TABLE `sys_user` ( `id` int NOT NULL AUTO_INCREMENT,
`name` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '姓名',
`name_pinyin` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '姓名拼音',
`id_card` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '身份证号',
`phone` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '手机号',
`age` int(11) NOT NULL DEFAULT 1 COMMENT '年龄',
PRIMARY KEY (`id`),
UNIQUE KEY `uni_idx_id_card` (`id_card`) USING BTREE COMMENT '唯一索引-身份证号',
KEY `idx_age` (`age`) USING BTREE COMMENT '普通索引-年龄' ,
KEY `idx_phone_name` (`phone`,`name`) USING BTREE COMMENT '普通索引-手机号' ) ENGINE=InnoDB AUTO_INCREMENT=3495 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='用户'
插入记录
insert into sys_user (id, name, name_pinyin, id_card, phone, age)values (1, '小六', 'xiaoliu', 300000000, 13000008000, 10);
insert into sys_user (id, name, name_pinyin, id_card, phone, age)values (2, '小六', 'xiaoliu', 300000001, 13000008000, 11);
insert into sys_user (id, name, name_pinyin, id_card, phone, age)values (3, '小六', 'xiaoliu', 300000002, 13000008000, 13);
insert into sys_user (id, name, name_pinyin, id_card, phone, age)values (4, '小六', 'xiaoliu', 300000003, 13000008000, 20);
记录锁
插入意向锁插入间隙锁区间内(age字段)阻塞等待
时刻 | 事务A | 事务B |
---|---|---|
1 | begin; | begin; |
2 | select * from sys_user where age=13 for update; (在11和20之间添加了间隙锁)) | |
3 | insert into sys_user (id, name, name_pinyin, id_card, phone, age)values (5, ‘小六’, ‘xiaoliu’, 300000004, 13000008000, 12);(阻塞等待) | |
在事务B阻塞等待时,使用show engine innodb status查看事务(内容删除了一部分): |
---TRANSACTION 133643386, ACTIVE 34 sec inserting
insert into sys_user (id, name, name_pinyin, id_card, phone, age)values (5, '小六', 'xiaoliu', 300000004, 13000008000, 19)
------- TRX HAS BEEN WAITING 28 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 484 page no 46 n bits 80 index idx_age of table `employees`.`sys_user` trx id 133643386 lock_mode X locks gap before rec insert intention waiting
Record lock, heap no 5
------------------
两个插入意向锁插入相同间隙(age字段)正常插入
时刻 | 事务A | 事务B |
---|---|---|
1 | begin; | begin; |
2 | insert into sys_user (id, name, name_pinyin, id_card, phone, age)values (5, ‘小六’, ‘xiaoliu’, 300000004, 13000008000, 19); | |
3 | insert into sys_user (id, name, name_pinyin, id_card, phone, age)values (6, ‘小六’, ‘xiaoliu’, 300000005, 13000008000, 18); |
两个插入意向锁(age字段)死锁
时刻 | 事务A | 事务B |
---|---|---|
1 | begin; | begin; |
2 | select * from sys_user where age=14 for update; | select * from sys_user where age=14 for update; |
3 | insert into sys_user (id, name, name_pinyin, id_card, phone, age)values (6, ‘小六’, ‘xiaoliu’, 300000005, 13000008000, 14); (事务A和B插入相同记录) | |
4 | insert into sys_user (id, name, name_pinyin, id_card, phone, age)values (6, ‘小六’, ‘xiaoliu’, 300000005, 13000008000, 14); (事务A和B插入相同记录) |
死锁日志
------------------------
LATEST DETECTED DEADLOCK
------------------------
2022-03-01 15:04:27 140684024923904
*** (1) TRANSACTION:
TRANSACTION 133654731, ACTIVE 29 sec inserting
mysql tables in use 1, locked 1
LOCK WAIT 4 lock struct(s), heap size 1136, 3 row lock(s), undo log entries 1
MySQL thread id 4796, OS thread handle 140684279383808, query id 5765383 192.168.1.83 root update
insert into sys_user (id, name, name_pinyin, id_card, phone, age)values (6, '小六', 'xiaoliu', 300000005, 13000008000, 14)
*** (1) HOLDS THE LOCK(S):
RECORD LOCKS space id 484 page no 4 n bits 80 index PRIMARY of table `employees`.`sys_user` trx id 133654731 lock_mode X locks rec but not gap
Record lock, heap no 6 PHYSICAL RECORD: n_fields 8; compact format; info bits 128
0: len 4; hex 80000006; asc ;;
1: len 6; hex 000007f768cb; asc h ;;
2: len 7; hex 81000000ac0110; asc ;;
3: len 6; hex e5b08fe585ad; asc ;;
4: len 7; hex 7869616f6c6975; asc xiaoliu;;
5: len 9; hex 333030303030303035; asc 300000005;;
6: len 11; hex 3133303030303038303030; asc 13000008000;;
7: len 4; hex 8000000e; asc ;;
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 484 page no 46 n bits 80 index idx_age of table `employees`.`sys_user` trx id 133654731 lock_mode X locks gap before rec insert intention waiting
Record lock, heap no 5
*** (2) TRANSACTION:
TRANSACTION 133654788, ACTIVE 22 sec inserting
mysql tables in use 1, locked 1
LOCK WAIT 3 lock struct(s), heap size 1136, 2 row lock(s)
MySQL thread id 4798, OS thread handle 140684407801600, query id 5765426 192.168.1.83 root update
insert into sys_user (id, name, name_pinyin, id_card, phone, age)values (6, '小六', 'xiaoliu', 300000005, 13000008000, 14)
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 484 page no 46 n bits 80 index idx_age of table `employees`.`sys_user` trx id 133654788 lock_mode X locks gap before rec
Record lock, heap no 5
*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 484 page no 4 n bits 80 index PRIMARY of table `employees`.`sys_user` trx id 133654788 lock mode S locks rec but not gap waiting
Record lock, heap no 6 PHYSICAL RECORD: n_fields 8; compact format; info bits 128
0: len 4; hex 80000006; asc ;;
1: len 6; hex 000007f768cb; asc h ;;
2: len 7; hex 81000000ac0110; asc ;;
3: len 6; hex e5b08fe585ad; asc ;;
4: len 7; hex 7869616f6c6975; asc xiaoliu;;
5: len 9; hex 333030303030303035; asc 300000005;;
6: len 11; hex 3133303030303038303030; asc 13000008000;;
7: len 4; hex 8000000e; asc ;;
资源分配图
个人分析死锁图应该是这样的:
注意
- 死锁回滚时,InnoDB会选择资源最小的那个事务进行回滚。
- 不用过度担心死锁,因为死锁事务回滚后再次重试就可以了。
参考
- https://dev.mysql.com/doc/refman/8.0/en/innodb-locking.html#innodb-record-locks
- https://www.infoq.cn/article/zau0ewzsdtx9zofr6c8w
- https://segmentfault.com/a/1190000019745324