Deadlock detection and rollback
...............................
InnoDB automatically detects a deadlock of transactions and rolls back
the transaction whose lock request was the last one to build a
deadlock, that is, a cycle in the waits-for graph of transactions.
InnoDB cannot detect deadlocks where a lock set by a MySQL `LOCK
TABLES' statement is involved, or if a lock set in another table
handler than InnoDB is involved. You have to resolve these situations
using `innodb_lock_wait_timeout' set in `my.cnf'.
When InnoDB performs a complete rollback of a transaction, all the
locks of the transaction are released. However, if just a single SQL
statement is rolled back as a result of an error, some of the locks set
by the SQL statement may be preserved. This is because InnoDB stores
row locks in a format where it cannot afterwards know which was set by
which SQL statement.