ACID
Atomicity:
Consistency:
Isolation:
“Durability” :
- The execution result of a transaction is correct only if consistency is satisfied.
- In the absence of concurrency, transactions are executed sequentially and isolation must be satisfied. So long as atomicity can be satisfied, consistency can be satisfied.
- In the case of concurrency, multiple transactions are executed in parallel. Transactions must satisfy not only atomicity but also isolation to satisfy consistency.
- Transactions are persisted in the event of a database crash.
AUTOCOMMIT
Concurrency consistency problem
1. Lost Update:
Definition: When two or more transactions select the same row and then update the row based on the value originally selected, a lost update problem occurs because each transaction is unaware of the existence of the other transactions, and the last update overwrites updates made by the other transactions.
2. Dirty Reads
Definition: a transaction is making changes to a record whose data is in an inconsistent state before the transaction completes and commits. At this point, another transaction reads the same record, and if left unchecked, the second transaction reads the “dirty” data and performs further processing accordingly, resulting in uncommitted data dependencies. This phenomenon is aptly called “dirty reading”.
T1 modifies a piece of data, which T2 then reads. If T1 reverses the change, then T2 reads dirty data.
3. Non-repeatable Reads:
T2 reads a piece of data, and T1 modifies it. If T2 reads this data again, it will not read the same data as it did the first time.
4. Phantom Reads:
Definition: When a transaction re-reads previously retrieved data under the same query criteria, only to find that another transaction inserts new data that meets its query criteria, this phenomenon is called “phantom read”.
The difference between phantom and unrepeatable reads:
The difference between unrepeatable and dirty reads:
Isolation level
Level 1: Read Uncommitted
Level 2: Read Committed
A transaction can only read changes made by committed transactions. In other words, changes made by one transaction are not visible to other transactions until they are committed.
The problem with this isolation level is Nonrepeatable Read: Non-repeatable reads mean that we may see different results when executing exactly the same SELECT statement in the same transaction. The reasons for this may include:
There is a new commit for an crossed transaction, resulting in data changes;
Repeatable Read level 3: Repeatable Read
MVCC does not solve the phantom problem; next-Key Locks exist to solve it. At the REPEATABLE READ isolation level, the phantom problem is solved using MVCC + next-key Locks.
Level 4: Serializable
summary
The default isolation level for MySQL is repeatable reads. The default isolation level for MySQL is repeatable reads. The default isolation level for MySQL is repeatable reads. It uses MVCC+next-key LOCKS to avoid the phantom problem. I will introduce the principle in the next article.
Refer to the
zhuanlan.zhihu.com/p/29166694
Github.com/CyC2018/CS-…