MySql InnoDB Repeatable Read Unexpected behavior of locks
NickName:Gleb Egunov Ask DateTime:2017-03-31T21:47:04

MySql InnoDB Repeatable Read Unexpected behavior of locks

As I know, innoDB use mechanism of consistent non blocking read, so every transaction works with its own snapshot.

it is told also in official documentation

A consistent read does not set any locks on the tables it accesses, and therefore other sessions are free to modify those tables at the same time a consistent read is being performed on the table.

But I unexpectedly faced with behavior when classic 'read/update' deadlock appears:

  1. Isolation level REPEATABLE READ (also is reproduced with READ COMMITTED)

  2. Transaction 1 reads row (NOT lock in share mode).

  3. Transaction 2 reads the same row ( ALSO NOT lock in share mode). then

  4. Transaction 1 tries to update this row.

  5. Transaction 2 also tries to update this row.

After last step, innoDB detects deadlock (there is LATEST DETECTED DEADLOCK below): ---------------- 2017-03-31 16:07:03 0x1f58 *** (1) TRANSACTION: TRANSACTION 413412, ACTIVE 20 sec starting index read mysql tables in use 1, locked 1 LOCK WAIT 9 lock struct(s), heap size 1136, 6 row lock(s), undo log entries 3 MySQL thread id 33, OS thread handle 8148, query id 102005 localhost 127.0.0.1 root updating

/* update Order */ update `Order` set ... <fields to update>

*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 2151 page no 709 n bits 88 index PRIMARY of table `ooapp2`.`order` trx id 413412 lock_mode X locks rec but not gap waiting
Record lock, heap no 3 PHYSICAL RECORD: n_fields 54; compact format; info bits 0

*** (2) TRANSACTION:
TRANSACTION 413413, ACTIVE 11 sec starting index read
mysql tables in use 1, locked 1
9 lock struct(s), heap size 1136, 6 row lock(s), undo log entries 3
MySQL thread id 28, OS thread handle 8024, query id 102008 localhost 127.0.0.1 root updating

/* update Order */ update `Order` set ...<fields to update>

*** (2) **HOLDS THE LOCK(S):**
RECORD LOCKS space id 2151 page no 709 n bits 88 index PRIMARY of table `ooapp2`.`order` trx id 413413 lock mode S locks rec but not gap
Record lock, heap no 3 PHYSICAL RECORD: n_fields 54; compact format; info bits 0

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 2151 page no 709 n bits 88 index PRIMARY of table `ooapp2`.`order` trx id 413413 lock_mode X locks rec but not gap waiting
Record lock, heap no 3 PHYSICAL RECORD: n_fields 54; compact format; info bits 0

 *** WE ROLL BACK TRANSACTION (2)

I can't understand, what happens, why Transaction 2

HOLDS THE LOCK(S)

so if innoDB nevertheless not use Consistent Read with snapshot and sets S-locks this does not correspond to the fact that is written in official manual.

Copyright Notice:Content Author:「Gleb Egunov」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/43142338/mysql-innodb-repeatable-read-unexpected-behavior-of-locks

More about “MySql InnoDB Repeatable Read Unexpected behavior of locks” related questions

MySql InnoDB Repeatable Read Unexpected behavior of locks

As I know, innoDB use mechanism of consistent non blocking read, so every transaction works with its own snapshot. it is told also in official documentation A consistent read does not set any...

Show Detail

What's the actual difference between MySQL InnoDB implementations of Repeatable Read and Serializable

According to the SQL Standard, Repeatable Read should prevent fuzzy reads and dirty reads, while Serializable should also prevent phantom reads. According to the MySQL documentation: By default,

Show Detail

MySQL Repeatable Read and dirty reads

According to this wikipedia entry, the repeatable read isolation level holds read and write locks when selecting data. My understanding is that this can prevent the age old banking example: Start a

Show Detail

Why MySQL InnoDB also acquire Gap locks for update/delete operation?

As far as I know the gap lock is used to prevent phantom read, and I found gap lock is set by locking read in most articles via Google search. A gap lock is a lock on a gap between index records...

Show Detail

InnoDB MySQL Select Query Locking

I have an isolation level of Repeatable Read and I am making a: Select * From examplequery. I read in https://dev.mysql.com/doc/refman/5.7/en/innodb-locks-set.html that select...from queries use

Show Detail

MySQL InnoDB gap locks

my table as follow: Create Table: CREATE TABLE `t` ( `a` int(11) DEFAULT NULL, KEY `idx_a` (`a`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 all datas as follow: +------+ | a | +------

Show Detail

What are MySQL InnoDB intention locks used for?

I have read the MySQL mannual about intention lock: http://dev.mysql.com/doc/refman/5.5/en/innodb-locking.html#innodb-intention-locks It says that "To make locking at multiple granularity levels

Show Detail

Does MySQL InnoDB locks multiple rows with READ_COMMITTED isolation level?

I am in trouble while resolving LOCK WAIT TIMEOUT EXCEED error with MySQL InnoDB. I have gone through this article and it says if we use isolation level READ_COMMITTED then my update query should ...

Show Detail

How does mysql INNODB implement READ-UNCOMMITTED?

I read mysql INNODB uses MVCC (optimistic) to solve READ_COMMITTED and REPEATABLE_READ isolation levels (and) 2PL (pessimistic) to solve SERIALIZABLE. But no where it's mentioned how it solves

Show Detail

Innodb executing "select for update" and "select (consistent read) in repeatable read isolation" parallelly in two different sessions/transactions

I have two parallel sessions/transactions, where one reads with "for update" and other with "consistent read(default)" . I want to know whether select query in session-2 blocks until session-1 is

Show Detail