MySQL-- affairs, isolation, and isolation levels

 

Affairs

The transaction is a set of database operations, either all executed successfully, or all fail to perform, in MySQL, is to rely on a transaction storage engine layer implementation.

 

ACID(Atomicity,Consistency,Isolation,Durability)

Atomicity means that a transaction can not be divided, the smallest unit of work.

Consistency means that the integrity of the data must be consistent.

Isolation refers to a plurality of users concurrent access to the database, a transaction must be turned on for each user, each transaction between unaffected isolated from each other.

Persistence means that once a transaction is committed, it changed data in the database is permanent, then even if the database fails nor should it have any impact.

 

Isolation Levels

First need to be clear is that the more tightly isolation, the efficiency will be lower, so many times is to find a balance both times.

Four isolation levels include the following:

Uncommitted Read (READ UNCOMMITTED): refers to when a transaction is not committed, changes can be done other things to see.

Read committed (READ COMMITTED): refers to after a transaction is committed, changes can be made to see other transactions.

Repeatable read (REAPEATABLE READ): refers to the data during the execution of a transaction to see, and always see when the transaction starts data are consistent, and uncommitted changes to other transactions is not visible.

Serialization (SERIALIZABLE): refers to the same line for recording, it will write-write locks, read and extending its read lock, write lock when there is a conflict, the transaction must wait after the visit of a transaction to complete before continue. This is the highest level of isolation level.

 

A cook chestnuts

Assume that the data is only one table T, which row is 1, then for the different levels of isolation, the values ​​in the table will be V1V2V3 different.

 

 

 

1, for read uncommitted, although it did not submit but B B changes made can be seen A, so V1 is found out that the revised 2, then naturally V2 is 2, V3 is 2;

2, submitted for reading, then B is not submitted before the changes made by A is not visible, it is still 1 V1, V2 B but before the investigation submitted, so V2 is equal to 2, V3 is also equal to 2;

3, for repeatable read, means that the value seen during the execution of a transaction are the same, when you start to query the value is 1, before the A uncommitted see is 1, so V1V2 are 1 , V3 is 2;

4, for the serialized, because the query to A before B 1, B so that when modified, and can not continue down, but must wait for the A execution ends, it is a V1V2, V3 is 2;

 

Query isolation level own database:

SHOW VARIABLES LIKE 'transaction_isolation';

 

Transaction isolation implementation:

 

 

 In MySQL, virtually every record in the update is going to be rolled back while recording a record, the record date value, by rolling back to find value in the previous state, at different times

Start transaction will have a different read-view, the same record may be many versions of the system. :( cook a chestnut picture MySQL45 from Dinc speaking)

From left to right, is a modified sequence 2,3,4, rollback journal can be seen in this FIG recording,

 In view A, B, C inside, this record is the value of 2,4. At this time, even if another transaction is then changed to 4 5, the transaction with the read-viewA, B, C corresponding to the transaction will not conflict.

Details of the principle I have not conducted in-depth understanding, if later learned supplemented on.

 

Affairs Start:

Explicit start: start transaction / begin, submit supporting language is commit, rollback rollback

That will shut down automatically submit set autocommit = 0, means that if you execute a select statement, the transaction is started, and does not automatically submitted until you manually commit or rollback,

Or disconnected. This naturally is not good, because if the client has a long connection with MySQL, then it leads to long transaction, the transaction will result in long-system there are many old transaction view.

These transactions can view and access any data in the database, all of the records prior to the rollback of the transaction commits will be retained and take up a lot of storage space. In addition, the transaction also take long

Lock resources, have a significant impact on the performance of the library.

Guess you like

Origin www.cnblogs.com/Yintianhao/p/12293616.html