MySQLTransactionRollbackException: Deadlock found when trying to get lock;
<p>I have a table trade_order:</p>

<pre><code> CREATE TABLE `trade_order` IF NOT EXISTS `buyer_order` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `order_no` varchar(30) NOT NULL DEFAULT '',
 `name` int(11) unsigned NOT NULL DEFAULT '0',
 PRIMARY KEY (`id`),
 UNIQUE KEY `order_no` (`order_no`),
 )
</code></pre>

<p>Now I want to insert data into table trade_order. I do it like this:</p>

<pre><code>transaction begin.
===
result = select * from order_table where order_no = #{orderNo} for update 
if(result is null){
    insert into trade_order (order_no,name) values (orderNo,name)
}
else{
    update trade_order set name = #{name} where order_no = #{orderNo}
}
===
transaction end.
</code></pre>

<p>But I got deadLock exeption:</p>

<pre><code> org.springframework.dao.DeadlockLoserDataAccessException: 
 ### Error updating database.  Cause:      com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock      found when trying to get lock; try restarting transaction
 ### The error may involve      com.youzan.trade.process.datasync.dal.dao.OrderDAO.insert-Inline
 ### The error occurred while setting parameters
 ### SQL: insert into trade_order ( order_no, name )  values ( ?,         ?,  )
 ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction
 ; SQL []; Deadlock found when trying to get lock; try restarting transaction; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction
</code></pre>

<p>Maybe the same two data would be deal with by two threads at the same time,and the two threads both do insert action.</p>

<p>But will it be result in deadLock?I wonder why.</p>

<p>(The mysql transaction isolate level is RR, InnoDB engine)</p>