FAQ: MySQL / transaction isolation

Parallel database issues arising

  1. A revocation of a transaction when the transaction changes the data B revocation.
  2. When the transaction commits A, B will cover peer transaction data changes.
  3. Dirty read: A read transaction data B uncommitted transactions.
  4. Non-repeatable read: A transaction with the query is not idempotent, read the updated data.
  5. Magic Reading: A query with things not idempotent, read the newly inserted data.

Database transaction isolation level

  1. Serialize (Serializable): during the execution of a transaction can not see other things, but if other transactions in operation, can only stop and so on; can avoid the above five questions.
  2. Repeatable read (Repeated Read, RR): a transaction can see the newly inserted record other committed transactions, but can not see the other transactions to update records on the existing line; allows phantom reads.
  3. Read Committed data (Read Committed, RC): a transaction record can see other committed transactions, including insert and update; phantom allows the reading and non-repeatable read.
  4. Uncommitted Read: a transaction from other transactions can see uncommitted; only A transaction retraction to avoid influence B transaction.

InnoDB transaction implementation of

MySQL engine is innoDB, use the default isolation level is repeatable read (RR grade); most of the database using the default isolation level is Read Committed (grade RC).

When implementing RC-level transactions, only need to use the line lock. InnoDB row lock in the form of realization of the transaction optimistic locking, InnoDB added two field after each row of data: Create a version number, delete the version number, when the transaction is the default level of RR and RC level, InnoDB transaction isolation to achieve the following:

  • select to read the version number <= current transaction version number, delete the version number is empty or greater than the version number of the current transaction.
  • insert version number to save the current transaction to create the version number of rows.
  • Save before delete delete file transactional version number is the version number of the line.
  • update insert a new record, save the current version number for the transaction to create the version number of lines, while preserving the current transaction to the original version of the line.

It should be noted that, based on the above principles, mysql transaction level is repeatable read, the result of select operations do not have a magic reading record, in fact, this is because the mysql select a "snapshot read" rather than "current read". Therefore, when the transaction was RR and RC-class level, mysql select the lock is not required, but when it comes to insert / update / delete, you need to read the current, we need to lock.

MySQL transaction using the Next-Key Lock, Next-Key lock is a row lock merger clearance and lock. Row lock is responsible for operating the line lock, in the RC stage, row locks are sufficient. Lock the gap, based on data as a B + tree index structure, where the data of the adjacent block with the block lock, and therefore may be unnecessary lock section, without using an index, the table will give full lock.

Note that the level is serial, then even select operation is also locked.

references

The relationship Innodb transaction isolation level and lock the
database transaction isolation level

Guess you like

Origin www.cnblogs.com/cielosun/p/11502539.html