mysql Transaction processing

  Key words: consistency, isolation, phantom read,

  Preface:

    Assuming that the Transaction A and Transaction B simultaneously.


First, the definition && characteristics of the transaction:

1. Definition:

  A set of sequence of operations performed on the database, while the set operation sequence must satisfy four properties ACID.

2, the transaction characteristics:

  ① atomic (Atomic): refers to the database either all of the operations performed together, or all not performed;
  ② consistency (Consistency): refers to the transaction must ensure that the system transition from one consistent state to another consistent state. ;
  ③ isolation (Isolation): during the execution of concurrent transactions in a mutual isolation characteristic between different transaction performed;
  ④ persistent (Durability): After the transaction commits, the impact on the system is permanent. 

  See: https://blog.csdn.net/chosen0ne/article/details/10036775

Second, consistency and isolation of transactional analysis:

1. Consistency:

(1) Definition:

  Transaction systems to ensure the transition from one consistent state to another consistent state.

(2) Analysis:

  Trabecular old beams to transfer, assuming that the size of the beam before transfer money add up to a total of 10,000, then after transfer to the trabecular old beams, no matter how turn these two accounts, money the old beams and trabecular money add up the total must still 10,000 this is the consistency of the transaction.

2, isolation:

(1) Definition:

  During the execution of concurrent transactions, performed by a characteristic isolation between different transactions.

(2) isolation level:

  ① Uncommitted Read (Read Uncommitted): A, during the execution of a transaction, a read transaction may B uncommitted data;
  ② reading submitted (Read Committed): during the execution of a transaction A, B can only read transaction submitted the latest content;
  ③ Repeatable read (Repeated Read): A transaction in process execution, can not read data transaction B updates (either committed or uncommitted), only when the transaction A is finished to be able to read transaction B updates;
  ④ serialization (Serialization): A transaction in the course of execution, the transaction B can not do anything, can only wait for transaction A complete re-implementation of the enforcement branch B.

  Dielectric strength transaction : Read Uncommitted <Read Committed <Repeated Read <Serialization

  See: https://baijiahao.baidu.com/s?id=1629344395894429251&wfr=spider&for=pc  

(3) three questions during the execution of concurrent transactions will produce: 

  ① Dirty read: refers to the implementation of the transaction A, B read data transaction uncommitted, but after that transaction B was rolled back operation, resulting in data just read A transaction is invalid. At this time, read the "dirty" data , so called dirty read.
  ② not repeatable read: refers to the implementation of the transaction A, B if the transaction continues to commit, then every transaction A can read the latest transaction B data submitted. This leads to the transaction A twice read the data results will be inconsistent , which means that each read data is not duplicated. So called not repeat read.
  ③ phantom read: refers to perform transactions A, only the contents of the first A transaction commit transaction B can be read; if, during execution of a transaction A, B and commit the transaction modification, it will not affect the transaction A . But when executing the transaction A, that is, when it commit, you will find the original data has actually changed by the transaction B . Like this gave rise to the phenomenon of phantom reads.

(4) " a detailed analysis of isolation levels" and "problems," the:

① problem will produce different isolation levels:

  • Read Uncommitted Level: dirty reads, non-repeatable read, phantom read
  • Read Committed level: not repeatable read, phantom read
  • Repeated Read Level: Magic Reading
  • Serialization Level: will not produce
②Repeatable Read && Magic Reading level:
  Achieve four isolation level of the database in different databases are different. In the mysql innodb engine, when implementing the Repeatable Read isolation level, we have solved the problem of the phantom reads. (Although the SQL standard does not require solving phantom read, but innodb gap by using MVCC + lock to solve the problem of phantom read.)

InnoDB Repeatable Serialization difference between the Read and under:

a, Repeatable Read level:
  • A write transaction will add X lock, then transaction B certainly can not do anything;
  • A read transaction, by controlling MVCC like CRUD operations, while the operation of the current recording interval plus gap lock (lock equivalent to S). To ensure that the transaction B can only read data but can not perform other operations.
b, Serialization Level:
  • Whether it is read or write, it is serialized. A transaction at the time of the operation, transaction B can only wait.
 
 

reference:

Guess you like

Origin www.cnblogs.com/axing-articles/p/11413893.html