The basic features of ACID transactions
- Atomic (atomicity) : all operations in the transaction is an integral unit, this unit operation either all succeed or all fail, not partial failure, partially successful scenario;
- Consistency (Consistency) : Upon completion of the transaction, it is necessary to make all the data in a consistent state (constraint a + b = 10, after the end of the transaction a + b = 10 is still valid);
- Isolation (isolation) : each transaction to a certain extent feel the presence of other transactions (reflected in the different isolation levels);
- Durability (persistence) : After the transaction commits, all data will be permanently written to disk.
Transaction isolation level
Database standards proposed four categories isolation levels, in varying degrees, pressing lost updates.
- Uncommitted Read
- Read Committed
- Repeatable read
- Serialization
Uncommitted Read
Uncommitted Read (read uncommitted) is the lowest level of isolation allows a transaction to read another transaction did not submit the data. For data consistency for the scene is not required. It exists dirty read phenomenon in the following table:
time | Services 1 | Services 2 | Explanation |
---|---|---|---|
T1 | Read inventory of 2 | Inventory of 2 | |
T2 | Stock --1 | Inventory 1 | |
T3 | Stock --1 | Inventory is 0 (read uncommitted data transaction 1) | |
T4 | Commit the transaction | Save stocks 0 | |
T5 | Rollback | Inventory is 0 (first class has overcome the loss of update) |
The first update is missing : a transaction rollback, another transaction commit, rollback covering the data submitted. The current database to overcome the loss of first class update.
Read Committed
Read Committed (read committed) refers to a transaction can only read data from another transaction has been submitted, the data can not be read uncommitted.
time | Services 1 | Services 2 | Explanation |
---|---|---|---|
T1 | Read inventory of 2 | Inventory of 2 | |
T2 | Stock --1 | Transaction 1 of 1 in stock | |
T3 | Stock --1 | 2 stock transaction is 1 (uncommitted transactions 1) | |
T4 | submit | Maintenance of a stock of 1 | |
T5 | Roll back the transaction | Inventory is 1 (first class has overcome the loss of update) |
Operation result on the final table correctly. But reading has been submitted will produce non-repeatable read :
time | Services 1 | Services 2 | Explanation |
---|---|---|---|
T1 | Read inventory 1 | Inventory 1 | |
T2 | Stock --1 | 1 in stock transactions 0 | |
T3 | Read inventory 1 | 2 transactions considered deductible (uncommitted transactions 1) | |
T4 | submit | Save stocks 0 | |
T5 | Stock --1 | Failure, this time inventory 0 |
Here Transaction 2 1 submitted before the transaction can be considered deductible, but later found the transaction 0 2 deductions stock has not deductible, such a phenomenon is called non-repeatable read, this is a lack of reading has been submitted.
Repeatable read
Repeatable read (read repeatable) objective is to overcome the phenomenon of non-repeatable reads read submitted that appear.
time | Services 1 | Services 2 | Explanation |
---|---|---|---|
T1 | Read inventory 1 | Inventory 1 | |
T2 | Stock --1 | 1 in stock transactions 0 | |
T3 | Read inventory | Services 2 can not be read, waiting for the transaction to submit 1 | |
T4 | submit | Save stocks 0 | |
T5 | Read inventory | Inventory is 0, you can not deduct |
When the read transaction data transactions 2 1 read in advance, will be blocked until after the transaction commits the transaction 1 2 can read, read committed appearing in non-repeatable read phenomenon disappeared. But repeatable read occurs phantom reads:
time | Services 1 | Services 2 | Explanation |
---|---|---|---|
T1 | Search Stock 100 | Stock 100,10 orders | |
T2 | Check Order 10 | ||
T3 | Stock --1 | ||
T4 | Insertion orders | ||
T5 | submit | Stock 99,11 orders | |
T6 | Print order, 11 single | 2, more than a transaction record, is inconsistent with the previous query |
On the table is the emergence of the phenomenon of phantom reads, phantom reads are not recorded for the purposes of a database, but multiple records , the order is on the table a number of records out, it will generate phantom reads.
Serialization
Serialization (serializable) is a database of isolation highest level, all transactions are executed sequentially. It can overcome various problems that appear in front of the isolation level, and fully ensure data consistency.
to sum up
Isolation Levels | Dirty read | Non-repeatable read | Magic Reading |
---|---|---|---|
Uncommitted Read | √ | √ | √ |
Read Committed | × | √ | √ |
Repeatable read | × | × | √ |
Serialization | × | × | × |
Different isolation levels can be suppressed in varying degrees, lost updates, use a higher isolation level to better ensure data consistency, but also to pay for performance. The higher the isolation level, the more performance decline linearly.