mysql The transaction isolation level

A transaction is a logical set of operations, or are executed or not executed.

The most classic of the transaction, for example, often come up with is the transfer. If small white flowers give transfer $ 1000, this transfer will involve two key operation is: will the balance -1000 flowers, the white balance of +1000. But what if between these two operations suddenly an error, such as the banking system sudden power outages, downtime or sudden collapse, may result in the balance after the flowers -1000 +1000 white balance was not so flowers and white on not happy. Affairs is to ensure that these two key operations are either succeed or fail should be a mechanism, we have also successfully completed the transfer, have failed and will not cause loss of flowers.

Characteristics of the transaction

There are four transaction properties (the ACID), namely atomicity, consistency, isolation and durability.

Atomicity (Atomity):  the transaction is the smallest unit of execution, segmentation is not allowed. Atomicity of transactions to ensure that actions are either completed or totally ineffective.

Consistency (Consistency):  before and after the execution of the transaction, the data is consistent.

Isolation (Isolation):  concurrent access to the database, a user's transaction can not interfere with the other firms, each is independent of concurrent transactions for the database.

Persistent (Durable): a transaction is committed after. It changed data in the database is persistent, even if the database fails nor should it have any impact.

Problems caused by concurrent transactions

In a typical application, if multiple transactions are running concurrently, often exhibit the same operational data of multiple transactions to complete their tasks (multiple users to operate a unified data) scene.

Although concurrent is necessary, but it may cause the following problems.

1. dirty read (Dirty Read)

When a transaction is accessing data, and the data has been modified, and this modification has not been submitted to the database, then another transaction also access this data and then use this data. This is because the data has not yet submitted data, and this data may not be final and submitted to the database, then another transaction read this data is [dirty], [based on] what you did the dirty data may It is not correct.

2. Modify the loss (Lost to Modify)

After a transaction at the time of a data read, another transaction can access the data, modify the data in the first transaction, a second transaction can modify this data. This could lead to a modification results in the first transaction is lost, because the amendment actually ends up in force is the second transaction to make changes, this modification is lost. For example, a transaction reads a data table A = 21, the read transaction is also 2 A = 21, when the transaction a modified A-A = 1, 2 also modify the transaction A = A-1, but the end result is a = 20, 1 to modify the transaction is lost.

3. Non-repeatable read (Unrepeatableread)

Non-repeatable read means is read within a transaction many times the same data, this data is read twice before and has inconsistencies. Because when this transaction is not over, there may be another transaction also access the data, it may cause data between the reads in the first transaction, second transaction because the modification results in the first two transactions data is not the same problem once read. Within the same transaction twice read data is not the same situation, it is referred to as non-repeatable read.

4. Magic Reading (Phantom Read)

Magic Reading and unrepeatable reads the like. It occurs in a transaction (T1) is read several lines of data, followed by another concurrent transaction (T2) is inserted into some of the data. In the following query, the first transaction (T1) will find more than a few original records do not exist, as if the same happened hallucinations, so called phantom reads.

Non-repeatability and distinction of the phantom read

Non-repeatable read focus is modified to emphasize that state records, such as records of some of the properties; phantom reads focus is to add or delete, stressed that the number of records, such as more or less a few few records recording.

Examples of non-repeatable reads (the same condition, you had to read the data, read out again discovered the value is not the same): 1 Mr A transaction reads their operating balance of 1000 is not yet complete, the transaction 2 Mr. a, Mr. B would modify the balance of 2000, resulting in Mr. a reading his balance again becomes a balance of 2000, which is non-repeatable read.

Examples of the phantom read (same conditions, read out of the 1st and 2nd recording different number): if the payroll table salary greater than 1W of 24 people, transaction 1 reads all the salary greater than 1W person, total found 24 records, and then record the transaction 2 has inserted a salary greater than 1W of, found in the transaction record 1 read again becomes a 25, thus leading to phantom reads.

Transaction isolation level

In the SQL standard defines four levels of isolation, are uncommitted read, read committed, and serializable can be re-read.

Read uncommitted (READ-UNCOMMITTED): the lowest level of isolation, changes have not been allowed to read the data submitted, may cause dirty reads, non-repeatable reads or phantom reads.

Read Committed (READ-COMMITTED): allow concurrent transactions to read data already submitted, can prevent dirty reads, but phantom reads or non-repeatable read may still occur.

Can re-read (REPEATABLE-READ): multiple reads the results of the same field are the same, unless the data is modified their affairs themselves, you can prevent dirty reads and non-repeatable reads, but phantom reads still occur.

Serialization (SERIALIZABLE): the highest level of isolation, four full compliance ACID properties. In this isolation level, all transactions are executed sequentially one by one, to ensure strict impossible interference between transactions. This means that this level can effectively prevent dirty reads, non-repeatable reads and phantom reads.

Default isolation level in MySQL InnoDB storage engine is REPEATABLE-READ (can be re-read).

SELECT @@tx_isolation;

You can check out the current MySQL isolation level used by the above command.

It should be noted, MySQL realize the isolation level and SQL different standards in place that InnoDB storage engine REPEATABLE-READ (can be re-read) the next transaction isolation level is Next-Key Lock Lock algorithm used, thus avoiding to generate magic of reading, this is with other database systems (such as SQL Server) different. This also means that the default isolation level REPEATABLE-READ InnoDB storage engine (can be re-read) already fully guarantee transaction isolation requirements, namely to achieve the SQL standard SERIALIZABLE (serialization) isolation level.

We know that isolation level of the database is usually implemented using locks. The lower the isolation level, transaction requests lock the less, the performance loss is also lower, database response sooner, so most of the database system isolation level is READ-COMMITTED (read submitted content) . But you have to know is, MySQL's InnoDB storage engine used by default REPEATABLE-READ (can be re-read) does not have any performance loss (Really), because MySQL has done some corresponding optimization. In addition, InnoDB storage engine in the case of a distributed transaction will generally be used SERIALIZABLE (serialization) isolation level, which is particularity scene.

Transaction-related commands

In the default configuration of MySQL command line, the transaction is automatically committed after that is to execute SQL statements COMMIT operation will be executed immediately.

We can set the isolation level by the following command.

SET [SESSION|GLOBAL] TRANSACTION ISOLATION LEVEL [READ UNCOMMITTED|READ COMMITTED|REPEATABLE READ|SERIALIZABLE]

There are also some concurrency control statements, it is often used in development to.

TARNSACTION the START | BEGIN  - explicitly open a transaction 
COMMIT  - commit the transaction, making all changes made to the database to become permanent 
ROLLBACK  - all uncommitted changes in end user transaction rollback, and cancel ongoing.

 

"I do not remember the way home, I just remember I'm afraid you Lin Zhao rain."

Guess you like

Origin www.cnblogs.com/yanggb/p/11365160.html