微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MySQL自增死锁

1、环境:

自建MysqL 5.7
innodb_autoinc_lock_mode=1

2、复现过程

2.1 建表t1 、 tt

 CREATE TABLE `t1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uqi_name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8
CREATE TABLE `tt` (
  `id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8

2.2 向tt表写入数据

MysqL> insert into tt values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10);
Query OK, 10 rows affected (0.00 sec)
Records: 10  Duplicates: 0  Warnings: 0

2.3 复现

session-1:

MysqL> begin;
Query OK, 0 rows affected (0.00 sec)

MysqL> insert into t1(name) select  *from tt where id<=5;
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

MysqL> 

session-2(处理锁等待状态):

MysqL>  insert into t1(name) select  *from tt where id<=5;
等待中...

session-1(显示插入成功):

MysqL>  insert into t1(name) select  *from tt where id>5;
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

MysqL> 

session-2(已经提示死锁回滚):

MysqL>  insert into t1(name) select  *from tt where id<=5;
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction

2.4 show engine innodb status 日志

------------------------
LATEST DETECTED DEADLOCK
------------------------
2022-06-24 22:47:54 0x7f87505f3700
*** (1) TRANSACTION:
TRANSACTION 11872922, ACTIVE 40 sec inserting
MysqL tables in use 2, locked 1
LOCK WAIT 3 lock struct(s), heap size 1136, 1 row lock(s), undo log entries 1
MysqL thread id 4, OS thread handle 140219145197312, query id 207 127.0.0.1 root Sending data
insert into t1(name) select  *from tt where id<=5
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 743 page no 4 n bits 72 index uqi_name of table `ceshi`.`t1` trx id 11872922 lock mode S waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
 0: len 1; hex 31; asc 1;;
 1: len 4; hex 8000001a; asc     ;;

*** (2) TRANSACTION:
TRANSACTION 11872917, ACTIVE 100 sec setting auto-inc lock
MysqL tables in use 2, locked 1
3 lock struct(s), heap size 1136, 1 row lock(s), undo log entries 5
MysqL thread id 3, OS thread handle 140219145729792, query id 208 127.0.0.1 root Sending data
insert into t1(name) select  *from tt where id>5
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 743 page no 4 n bits 72 index uqi_name of table `ceshi`.`t1` trx id 11872917 lock_mode X locks rec but not gap
Record lock, heap no 2 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
 0: len 1; hex 31; asc 1;;
 1: len 4; hex 8000001a; asc     ;;

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
TABLE LOCK table `ceshi`.`t1` trx id 11872917 lock mode AUTO-INC waiting
*** WE ROLL BACK TRANSACTION (1)

2.5总结:

1、session-1先插入数据
2、session-2 插入数据一定要产生行锁等待,此时自增锁并没有释放,还是持有这个表的自增锁。
3、session-1 写入任意数据(不能指定自增值),会去申请自增锁,但自增锁又被 seesion-2 会话持有。session-2 又在等session-1 释放行锁。
3、锁等待环路发生,MysqL自动杀掉回滚成本较小的会话。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐