mysql 插入導致的死鎖問題


 

1、問題現象,通過產生的日志可以看到,兩個insert事務產生死鎖,這個問題在oracle數據庫中沒有發生過,mysql是什么原因導致的此問題?

mysql tables in use 1, locked 1
LOCK WAIT 6 lock struct(s), heap size 1136, 3 row lock(s), undo log entries 3
MySQL thread id 3217010, OS thread handle 47641961760512, query id 649860665 10.xx.xx.xx
INSERT INTO table (columns) VALUES (values);               <------- Insert statement
[Note] InnoDB: *** (1) WAITING FOR THIS LOCK TO BE GRANTED:

RECORD LOCKS space id xxxx 170 page no xxxx n bits 168 index PRIMARY of table "database_name"."table_name" trx id xxxx lock_mode X insert intention waiting

2018-11-19T20:59:38.997530Z 3217009 [Note] InnoDB: *** (2) TRANSACTION:

TRANSACTION 184047644, ACTIVE 3 sec inserting
mysql tables in use 1, locked 1
7 lock struct(s), heap size 1136, 4 row lock(s), undo log entries 3
MySQL thread id 3217009, OS thread handle 47641962026752, query id 649860365 10.xx.xx.xx APP_USER
INSERT INTO table (columns) VALUES (values);               <---- Similar Insert statement as above and same table are used
2018-11-19T20:59:38.997556Z 3217009 [Note] InnoDB: *** (2) HOLDS THE LOCK(S):

RECORD LOCKS space id 17170 page no 1172 n bits 168 index PRIMARY of table "database_name"."table_name"  trx id 184047644 lock_mode X <---waiting on the primary key
Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
0: len 8; hex xxxxxxx; asc supremum;;

2018-11-19T20:59:38.997612Z 3217009 [Note] InnoDB: *** (2) WAITING FOR THIS LOCK TO BE GRANTED:

RECORD LOCKS space id 17170 page no 1172 n bits 168 index PRIMARY of table "database_name"."table_name" trx id 184047644 lock_mode X insert intention waiting <---- waiting on same primary key
Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
0: len 8; hex xxxxxxxxxxx; asc supremum;;

2018-11-19T20:59:38.997671Z 3217009 [Note] InnoDB: *** WE ROLL BACK TRANSACTION (1)

2018-11-19T21:07:50.532127Z 3217290 [Note] InnoDB: Transactions deadlock detected, dumping detailed information.
2018-11-19T21:07:50.532157Z 3217290 [Note] InnoDB:

  

2、通過查閱官方文檔,可以看到是由於以下原因導致的,但是文檔並沒有給出過多的解釋:

CHANGES
 Transaction isolation level is REPEATABLE-READ.


  

3、先來了解下在這兩種事務隔離方式的區別:

RR:即repeatable-read,

RC:即repeatable-commited,此事務模式是oracle默認的事務隔離級別模式。

 

 

4、為什么在mysql中高並發的插入會導致deadlock,而gap lock又是什么?

CAUSE
When Transaction isolation level is REPEATABLE-READ, Innodb will use Gap lock to prevent other transactions from inserting to the gap.

  UPDATE和DELETE時(注意這里沒有insert,經過對insert測試,沒有出現此類情況。),除了對唯一索引的唯一搜索外都會獲取gap鎖或next-key鎖即鎖住其掃描的范圍。

5、如何避免此類問題?

 

SOLUTION
Change tx_isolation = READ COMMITTED. With READ COMMITTED, it will reduce the gap locks and may help to your deadlocks here.

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM