MySQL死鎖問題

LZC發表於2022-10-21

新建表

CREATE TABLE `t2` (
  `id` int NOT NULL AUTO_INCREMENT,
  `a` int DEFAULT NULL,
  `b` int DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `a` (`a`),
  KEY `b` (`b`)
) ENGINE=InnoDB;

初始化資料

insert into t2(a,b)values(30,30),(60,60);
事務1 事務2 事務3
t1 begin;
insert into t2(a,b)values(90,90) on duplicate key update b = b + 1;
t2 insert into t2(a,b)values(90,90) on duplicate key update b = b + 1;(阻塞)
t3 insert into t2(a,b)values(90,90) on duplicate key update b = b + 1;(阻塞)
t4 rollback;
t5 Query OK, 1 row affected ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章