-- 创建表test1 CREATE TABLE test1 ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(10) NOT NULL, PRIMARY KEY (id) ); insert into test1 values("hello");-- 创建表test2 CREATE TABLE test2 ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(10) NOT NULL, PRIMARY KEY (id) );-- Transcation 1 begin; insert into test2 select * from test1 where id = 1; delete from test1 where id = 1;-- Transcation 2 begin; insert into test2 select * from test1 where id = 1;3、具体执行顺序
Transcation1 | Transcation2 |
begin; — 这条sql得到test1表主键索引锁共享锁S(id=1) insert into test2 select * from test1 where id = 1; | |
begin; — 这条sql试图获取test1表主键索引锁共享锁S(id=1),但是已经被T1占有,所以它进入锁请求队列. insert into test2 select * from test1 where id = 1; | |
— 这条sql试图把自己拥有的test1表主键索引锁共享锁S(id=1)升级为排它锁X(id=1) — 这时T1也发起一个锁请求,这个时候mysql发现锁请求队列里边已存在一个事物T2对(id=1)的这条记录申请了S锁,死锁产生了。 delete from test1 where id = 1; | |
死锁产生后mysql根据两个事务的权重,事务2的权重更小,被选为死锁的牺牲者,rollback。 | |
T2 rollback 之后T1成功获取了锁执行成功 |
?***(1) TRANSACTION: ?TRANSACTION 0 677833455, ACTIVE 0 sec, process no 11393, OS thread id 278546 starting index read ?mysql tables in use 1, locked 1 ?LOCK WAIT 3 lock struct(s), heap size 320 ?MySQL thread id 83, query id 162348740 dcnet03 dcnet Searching rows for update ?update tab_test set state=1064,time=now() where state=1061 and time < date_sub(now(), INTERVAL 30 minute) (任务1的sql语句) ?***(1) WAITING FOR THIS LOCK TO BE GRANTED: (任务1等待的索引记录) ?RECORD LOCKS space id 0 page no 849384 n bits 208 index `PRIMARY` of table `db/tab_test` trx id 0 677833455 _mode X locks rec but not gap waiting ?Record lock, heap no 92 PHYSICAL RECORD: n_fields 11; compact format; info bits 0 ?0: len 8; hex 800000000097629c; asc b ;; 1: len 6; hex 00002866eaee; asc (f ;; 2: len 7; hex 00000d40040110; asc @ ;; 3: len 8; hex 80000000000050b2; asc P ;; 4: len 8; hex 800000000000502a; asc P*;; 5: len 8; hex 8000000000005426; asc T&;; 6: len 8; hex 800012412c66d29c; asc A,f ;; 7: len 23; hex 75706c6f6164666972652e636f6d2f6 8616e642e706870; asc xxx.com/;; 8: len 8; hex 800000000000042b; asc +;; 9: len 4; hex 474bfa2b; asc GK +;; 10: len 8; hex 8000000000004e24; asc N$;; ?*** (2) TRANSACTION: ?TRANSACTION 0 677833454, ACTIVE 0 sec, process no 11397, OS thread id 344086 updating or deleting, thread declared inside InnoDB 499 ?mysql tables in use 1, locked 1 ?3 lock struct(s), heap size 320, undo log entries 1 ?MySQL thread id 84, query id 162348739 dcnet03 dcnet Updating update tab_test set state=1067,time=now () where id in (9921180) (任务2的sql语句) ?*** (2) HOLDS THE LOCK(S): (任务2已获得的锁) ?RECORD LOCKS space id 0 page no 849384 n bits 208 index `PRIMARY` of table `db/tab_test` trx id 0 677833454 lock_mode X locks rec but not gap ?Record lock, heap no 92 PHYSICAL RECORD: n_fields 11; compact format; info bits 0 ?0: len 8; hex 800000000097629c; asc b ;; 1: len 6; hex 00002866eaee; asc (f ;; 2: len 7; hex 00000d40040110; asc @ ;; 3: len 8; hex 80000000000050b2; asc P ;; 4: len 8; hex 800000000000502a; asc P*;; 5: len 8; hex 8000000000005426; asc T&;; 6: len 8; hex 800012412c66d29c; asc A,f ;; 7: len 23; hex 75706c6f6164666972652e636f6d2f6 8616e642e706870; asc uploadfire.com/hand.php;; 8: len 8; hex 800000000000042b; asc +;; 9: len 4; hex 474bfa2b; asc GK +;; 10: len 8; hex 8000000000004e24; asc N$;; ?*** (2) WAITING FOR THIS LOCK TO BE GRANTED: (任务2等待的锁) ?RECORD LOCKS space id 0 page no 843102 n bits 600 index `idx_1` of table `db/tab_test` trx id 0 677833454 lock_mode X locks rec but not gap waiting ?Record lock, heap no 395 PHYSICAL RECORD: n_fields 3; compact format; info bits 0 ?0: len 8; hex 8000000000000425; asc %;; 1: len 8; hex 800012412c66d29c; asc A,f ;; 2: len 8; hex 800000000097629c; asc b ;; ?*** WE ROLL BACK TRANSACTION (1) ?(回滚了任务1,以解除死锁)原因分析:
update tab_test set state=1064,time=now() where state=1061 and time < date_sub(now(), INTERVAL 30 minute)”
执行时,MySQL会使用idx_1索引,因此首先锁定相关的索引记录,因为idx_1是非主键索引,为执行该语句,MySQL还会锁定主键索引。update tab_test set state=1067,time=now () where id in (9921180)
”几乎同时执行时,本语句首先锁定主键索引,由于需要更新state的值,所以还需要锁定idx_1的某些索引记录。select id from tab_test where state=1061 and time < date_sub(now(), INTERVAL 30 minute); update tab_test state=1064,time=now() where id in(......);关于MySQL死锁问题的实例分析及解决方法就介绍到这里了,希望本次的介绍能够对您有所收获!