Powered By Blogger

Monday, 1 August 2011

simulate the Dead Lock issue

use test ;
create table table_a ( emp int, ename varchar(100));
create table table_b ( emp int, ename varchar(100));
mysql> insert into table_a values (1,'anil');
mysql> insert into table_a values (2,'sunil');
mysql> insert into table_b values (2,'sunil');
mysql> insert into table_b values (1,'anil');
open two sessions

first session

prompt session1>
session1> start transaction;
session1> update table_a set ename='xxx' where eno=1;
session1> update table_2 set ename='xxx' where emp=1;

second session

prompt session2>
session2> use test;
session2> start transaction;
session2> update table_2 set ename='xxx' where emp=1;
session2> update table_a set ename='xxx' where eno=1;
session2>  update table_a set ename='zzz' where emp=1;
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction

The resolutions are as follows:
######################

By default MySQL will wait for 50 seconds for the "lock wait timeout" error .
In some rare cases InnoDB would commit queries partially after waiting for 50 seconds.
In order to avoid this, we should add  "innodb_rollback_on_timeout" and increase the
"innodb_lock_wait_timeout" status in my.cnf which are as follows:
innodb_lock_wait_timeout = 120 seconds
innodb_rollback_on_timeout

For more details about the deadlock
the usl is as follows:
http://dev.mysql.com/doc/refman/5.0/en/innodb-lock-modes.html

No comments:

Post a Comment