美文网首页
普通索引导致死锁

普通索引导致死锁

作者: tomorrowsg | 来源:发表于2021-07-19 22:17 被阅读0次
CREATE TABLE `author` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `author_id` varchar(50) NOT NULL DEFAULT '' COMMENT '作者号',
  `author_name` varchar(50) NOT NULL DEFAULT '' COMMENT '作者名',
  `content` varchar(255) NOT NULL DEFAULT '' COMMENT '作者简介',
  PRIMARY KEY (`id`),
  KEY `idx_content` (`content`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8 COMMENT='作者表'
id author_id author_name content
1 0001 张三 222
2 0002 李四 222
3 0003 王五 222

其中id为主键,content为普通索引

session1 session2
start transaction; start transaction;
update author set author_name = '5555' where id = 1;(持有id为1的行锁)
update author set author_name = '8888' where content = '222';(获取idx_content的锁,然后继续尝试获取id为1、2、3三行的行锁,由于事务1已经获取了id为1的行锁。所以这里会阻塞)
update author set author_name = '999' where content = '222';(尝试获取idx_content的锁,与事务2死锁)
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
commit; commit;

由于事务1最后提交了一个修改sql。因此事务1的修改会生效,事务2会报异常并回滚。

mysql加锁过程参考文章:https://www.cnblogs.com/xiaohuangya/articles/3552990.html

相关文章

网友评论

      本文标题:普通索引导致死锁

      本文链接:https://www.haomeiwen.com/subject/dzkkmltx.html