2 tyhqiushen tyhqiushen 于 2017.01.06 17:45 提问

一条简单SQL语句出现死锁情况,请帮忙各位大神帮忙分析一下。

小弟有一SQL语句现在在多并发的情况下会出现死锁。希望各位大神帮忙给小弟分析一下。

 UPDATE accountinfo SET usecnt = 20, version = version+1 WHERE usecnt > 0 AND accountid = 12 and version = 102

其中accountid为主键,usecnt和version没有任何索引。
当前事务使用的事务隔离级别为:READ_COMMITTED

发生死锁的日志如下:

 *** (1) TRANSACTION:
TRANSACTION 1331664, ACTIVE 40 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 11 lock struct(s), heap size 2936, 6 row lock(s), undo log entries 9
MySQL thread id 106, OS thread handle 0x334c, query id 2285 192.168.25.85 root updating
UPDATE accountinfo SET usecnt = 20, version = version+1 
            WHERE usecnt > 0 AND accountid = 12 and version = 102
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 2151 page no 3 n bits 80 index `PRIMARY` of table `stamp_server_db`.`accountinfo` trx id 1331664 lock_mode X locks rec but not gap waiting
Record lock, heap no 10 PHYSICAL RECORD: n_fields 35; compact format; info bits 0
 *** (2) TRANSACTION:
TRANSACTION 1331673, ACTIVE 39 sec starting index read
mysql tables in use 1, locked 1
11 lock struct(s), heap size 2936, 6 row lock(s), undo log entries 9
MySQL thread id 109, OS thread handle 0x3910, query id 2355 192.168.25.85 root updating
UPDATE accountinfo SET usecnt = 20, version = version+1 
            WHERE usecnt > 0 AND accountid = 12 and version = 102

*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 2151 page no 3 n bits 80 index `PRIMARY` of table `stamp_server_db`.`accountinfo` trx id 1331673 lock mode S locks rec but not gap
Record lock, heap no 10 PHYSICAL RECORD: n_fields 35; compact format; info bits 0
*** (2) WAITING FOR THIS LOCK TO BE GRANTED:

RECORD LOCKS space id 2151 page no 3 n bits 80 index `PRIMARY` of table `stamp_server_db`.`accountinfo` trx id 1331673 lock_mode X locks rec but not gap waiting
Record lock, heap no 10 PHYSICAL RECORD: n_fields 35; compact format; info bits 0

*** WE ROLL BACK TRANSACTION (2)

请各位大神,帮小弟看一下。怎么破

1个回答

Star_Sachem
Star_Sachem   2017.01.06 17:52

READ COMMITTED 允许幻想读、不可重复读,不允许脏读

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!