数据库
首页 > 数据库> > MySQL自增死锁

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自动杀掉回滚成本较小的会话。

标签:自增,name,tt,mysql,t1,死锁,session,MySQL,id
来源: https://www.cnblogs.com/nanxiang/p/16415667.html