Discussion:
Dead lock problem
haiwen zhu
2014-08-04 11:44:03 UTC
Permalink
Problem description as follow

Table structure:


* CREATE TABLE `test` (*


* `id` int(11) unsigned NOT NULL AUTO_INCREMENT,*


* `name` char(50) NOT NULL DEFAULT '',*


* PRIMARY KEY (`id`),*


* UNIQUE KEY `name` (`name`)*

*) ENGINE=InnoDB AUTO_INCREMENT=81 DEFAULT CHARSET=utf8*
*INSERT INTO tes (name) VALUES ('a'),('b');* //make sure 'a' and 'b'
already exists

Script t1.php:
*<?php*
*$db->begin_transaction();*

*$db->query("REPLACE INTO test (name) value ('a')");*
*sleep(5);//make sure script t2 while here*

*$db->query("REPLACE INTO test (name) value ('a')");*
*$db->commit();*

Script t2.php
*<?php*
*$db->begin_transaction();*
*$db->query("REPLACE INTO test (name) value ('b')");*
*$db->commit();*

If execute t2.php after t1.php,there will be a dead lock,
execute *SHOW ENGINE INNODB STATUS* show that dead lack happened on second
sql statment
I hnow mysql have range lock, i still wonder why the second sql statment in
script t1.php grant lock repeated?
Look forward a clear explanation.
--
Best Regards,
HaiwenZhu
Loading...