Article Directory
- Recommended learning
- MySQL base notes (1) - in-depth understanding of the index
- A Transaction
- 1. What is a transaction
- 2. Transaction four properties (ACID)
- 3. concurrent transactions brought four major problems
- Q: non-repeatable read and phantom read What is the difference?
- II. Database lock mechanism
- 1. The four kinds of transaction isolation level
- 2.InnoDB storage engine lock algorithm
- Q: three kinds of lock algorithms when to use?
- 3.MVCC- multi-version concurrency control
- Thanks
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)
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.
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.
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.
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 Lock,The 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
- Record Lock: lock on a single row
- Gap Lock: gap lock, a lock range but does not include its own
- Next-Key Lock: Record Lock + Gap Lock, a range that is locked, including itself.
Q: three kinds of lock algorithms when to use?
- Modification of a single row of data: Record Lock, lock a single row
- 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
- https://segmentfault.com/a/1190000012650596
- https://www.jianshu.com/p/8d735db9c2c0