title: How would this deadlock happens?
body: <p>Recently I am handing a SQL issue, I have two transaction, the transaction A first got the Next-Key Locks, and the transaction B tried to get the same lock, so it was waiting, then the transaction A tried to get Insert Intention Locks, so the deadlock happened. But I am confuse that why would this happens?</p>
  
  <h1>Here is my table structure:</h1>
  
  <pre><code>CREATE TABLE `changeset` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT',    
    `userId` int(10) NOT NULL COMMENT,    
    `documentId` varchar(20) NOT NULL,    
    `memberId` bigint(13) NOT NULL,    
    `createTime` bigint(13) NOT NULL,    
    `version` bigint(13) NOT NULL COMMENT,    
    `changesets` mediumtext,    
    PRIMARY KEY (`id`),    
    UNIQUE KEY `uniq_documentId_version` (`documentId`,`version`) USING BTREE    
  ) ENGINE=InnoDB AUTO_INCREMENT=85771623 DEFAULT CHARSET=utf8
  </code></pre>
  
  <h1>And here is my deadlock log:</h1>
  
  <blockquote>
    <p>(1) TRANSACTION:</p>
    
    <p>TRANSACTION 22640, ACTIVE 66 sec starting index read</p>
    
    <p>mysql tables in use 1, locked 1</p>
    
    <p>LOCK WAIT 2 lock struct(s), heap size 1136, 1 row lock(s)</p>
    
    <p>MySQL thread id 209, OS thread handle 123145559986176, query id 6204
    localhost root Sending data</p>
    
    <p><code>select * from changeset where documentId = '7oO5C_v' and version &gt;=
    13 for update</code></p>
    
    <p>(1) WAITING FOR THIS LOCK TO BE GRANTED:</p>
    
    <p>RECORD LOCKS space id 107 page no 15 n bits 704 index 
    uniq_documentId_version of table <code>test</code>.<code>changeset</code> trx id 22640
    lock_mode X waiting</p>
    
    <p>Record lock, heap no 2 PHYSICAL RECORD: n_fields 3; compact format;
    info bits 0</p>
    
    <p>0: len 7; hex 3976735431644a; asc 9vsT1dJ;;</p>
    
    <p>1: len 8; hex 8000000000000000; asc         ;;</p>
    
    <p>2: len 4; hex 051cbef7; asc     ;;</p>
    
    <p>(2) TRANSACTION:</p>
    
    <p>TRANSACTION 22639, ACTIVE 95 sec inserting</p>
    
    <p>mysql tables in use 1, locked 1</p>
    
    <p>6 lock struct(s), heap size 1136, 4 row lock(s), undo log entries 1</p>
    
    <p>MySQL thread id 212, OS thread handle 123145561657344, query id 6210
    localhost root update <code>insert into changeset values (0, 9, '7oO5C_v',
    814, 1, 13, 'x')</code></p>
    
    <p>(2) HOLDS THE LOCK(S):</p>
    
    <p>RECORD LOCKS space id 107 page no 15 n bits 704 index 
    uniq_documentId_version of table <code>test</code>.<code>changeset</code> trx id 22639
    lock_mode X</p>
    
    <p>Record lock, heap no 2 PHYSICAL RECORD: n_fields 3; compact format;
    info bits 0</p>
    
    <p>0: len 7; hex 3976735431644a; asc 9vsT1dJ;;</p>
    
    <p>1: len 8; hex 8000000000000000; asc         ;;</p>
    
    <p>2: len 4; hex 051cbef7; asc     ;;</p>
    
    <p>(2) WAITING FOR THIS LOCK TO BE GRANTED:</p>
    
    <p>RECORD LOCKS space id 107 page no 15 n bits 704 index
    uniq_documentId_version of table <code>test</code>.<code>changeset</code> trx id 22639
    lock_mode X locks gap before rec insert intention waiting</p>
    
    <p>Record lock, heap no 2 PHYSICAL RECORD: n_fields 3; compact format;
    info bits 0</p>
    
    <p>0: len 7; hex 3976735431644a; asc 9vsT1dJ;;</p>
    
    <p>1: len 8; hex 8000000000000000; asc         ;;</p>
    
    <p>2: len 4; hex 051cbef7; asc     ;;</p>
    
    <p>WE ROLL BACK TRANSACTION (1)</p>
  </blockquote>