Created
March 1, 2011 18:29
-
-
Save andrewyatz/849606 to your computer and use it in GitHub Desktop.
How to deadlock MySQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Setup the following schema | |
CREATE TABLE `seq` ( | |
`id` int(11) unsigned NOT NULL AUTO_INCREMENT, | |
`name` varchar(11) NOT NULL, | |
`value` int(11) NOT NULL, | |
PRIMARY KEY (`id`), | |
UNIQUE KEY `name_idx` (`name`) | |
) ENGINE=InnoDB; | |
-- In process 1 | |
set autocommit = 0; | |
select * from seq where name = 'foo' for update; | |
-- In process 2 | |
set autocommit = 0; | |
select * from seq where name = 'foo' for update; | |
-- In process 1 | |
insert into seq (name, value) values ('foo', 1); | |
-- process 1 will now block | |
-- In process 2 | |
insert into seq (name, value) values ('foo', 1); | |
-- Process 2 will deadlock & will allow process 1 to continue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is meant to describe why you encounter deadlocks sometimes when you don't expect to