MySQL base notes (16) - Transaction

Recommended learning

MySQL base notes (1) - in-depth understanding of the index

A Transaction

1. What is a transaction

A transaction is a set of operations, or are executed or not executed. The classic example is an example of transfers: A transfers to B, if A then B must be charged the money. However, if a system error occurs in the process, A is not charged, then B will not get the money, which is money debit transactional and processing operations of this group.

2. Transaction four properties (ACID)

Here Insert Picture Description

1.Atomic- atomicity

A transaction is the smallest unit, operating within a transaction either succeed, or else fail.

2.Consistency- consistency

Data before and after the implementation of the transaction are the same when read other things to.

3.Isolation- isolation

Performed simultaneously between a plurality of transactions do not interfere with each other, each independent transaction.

4.Durability- persistence

After transaction execution, change its data is persistent, rollback does not occur.

3. concurrent transactions brought four major problems

1. dirty read

A transaction reads dirty data uncommitted transaction B, A leads to operation after the transaction is wrong.
Here Insert Picture Description

2. A non-repeatable read

A transaction reads the data, B to perform an update transaction operations, resulting in A transaction can not be read to the original results.
Here Insert Picture Description

3. Magic Reading

After the transaction reads data A, B transaction to add new data, resulting in A transaction can not read the original results.
Here Insert Picture Description

4. Update loss

A refers to the possibility of update transaction rollback transaction B is revoked.

Q: non-repeatable read and phantom read What is the difference?

Refers to a non-repeatable read A transaction reads a period of data, B transaction changes the article data, leading to inconsistent results before and after the reading of A, to solve this problem just addRow-level lockingRead the A transaction open during certain pieces of data, other transactions can not make changes to the data for the article.

The phantom read A transaction refers to the period of reading a plurality of data, B data transaction added, leading to inconsistent results before and after the reading of A, to solve this problem need to addTable-level lockingSo A plurality of data during read things, other transactions can not change the data in the table.

II. Database lock mechanism

Lock according to different objects, it can generally be dividedTable lockwithLine LockThe former lock the entire table, which table a particular row lock. Concurrent transactions from locking relationship point of view, can be divided into share lock and exclusive lock. Shared lock will prevent exclusive lock, but allow other shared lock; and both exclusive lock to prevent other exclusive lock to prevent other shared lock.

1. The four kinds of transaction isolation level

Isolation Levels Dirty read Non-repeatable read Magic Reading
Uncommitted Read
Read Committed x
Repeatable read x x
Serialization x x x

Which can be read as a repeat of the default InnoDB transaction isolation level.

2.InnoDB storage engine lock algorithm

  1. Record Lock: lock on a single row
  2. Gap Lock: gap lock, a lock range but does not include its own
  3. Next-Key Lock: Record Lock + Gap Lock, a range that is locked, including itself.

Q: three kinds of lock algorithms when to use?

  1. Modification of a single row of data: Record Lock, lock a single row
  2. Modification of the range of the data: Next-Key Lock: locking the data range

3.MVCC- multi-version concurrency control

MVCC refers to a multi-version concurrency control techniques. InnoDB engine is the default isolation level RR (repeatable read), but they were able to solve the problem phantom read, the reason lies in using MVCC technology.

MVCC technology to achieve: in the range of transaction data read, InnoDB first assigned a version number (the default plus 1) for the transaction,Then in the course of the transaction query only query the data is less than the version number of the transaction, Then whether or not there are other things to change the data in this range are able to query the same result, because if another transaction changes the data, it will produce a larger version number, will not be queried.

Thanks

  1. https://segmentfault.com/a/1190000012650596
  2. https://www.jianshu.com/p/8d735db9c2c0
Published 309 original articles · won praise 205 · Views 300,000 +

Guess you like

Origin blog.csdn.net/pbrlovejava/article/details/103550499