MySQL four isolation levels, you know what?

1. What is a transaction

   A transaction is a series of strict application of the operation, all operations must be completed successfully, otherwise made in each operation, all changes are undone. That is, the transaction has atomicity, a series of operations in a transaction either all succeed, or none do. There are two end of the transaction, when the transaction so successfully execute all the steps, the transaction commits. If a step fails, the rollback operation will occur, so that before the undo operation to undo the transaction began.

2, the ACID transaction

      Transaction has four features: Atomicity (Atomicity), consistency (Consistency), isolation (Isolation) and persistent (Durability). The four properties, referred to as the ACID properties.

  • Atomicity. A transaction is a logical unit of work database, all operations in the transaction included either do or do not do
  • consistency. The results of the implementation of the database transaction must be changed from one consistent state to another consistent state. Therefore, when the database contains only the result of a successful transaction commit, say the database in a consistent state. If a failure database system is running, some were forced to interrupt the transaction has not been completed, these transactions did not complete the modifications made to the database has been written to the physical part of the database, then the database will be in a bad state, or is inconsistent state.
  • Isolation. Execution of a transaction can not interfere with other transactions. I.e., operation and use of the data inside a transaction other concurrent transactions are isolated and can not interfere with each other between the respective transaction executed concurrently.
  • Persistent. Also known as permanent, it means that once a transaction commits, changing the data in the database is that it should be permanent. The next operation or other faults should not have any impact on the results.

3, Mysql four isolation levels

    SQL standard defines four types of isolation levels, including a number of specific rules, which defines changes to internal and external transaction is visible, which is not visible. Low levels of isolation stages generally support higher concurrent processing, and have lower overhead.

(1) Read Uncommitted (Uncommitted read the content)

    In this isolation level, all transactions can see the results of other uncommitted transactions. This isolation level is rarely used in practice, because its performance is not much better than the other levels. Uncommitted read data, also called a dirty read (Dirty Read).

(2) Read Committed (read submission)

    This is the default isolation level for most database systems (MySQL, but not the default). It meets the simple definition of isolation: a transaction can only see the change has been submitted firms do. This isolation level also supports so-called non-repeatable read (Nonrepeatable Read), because other instances of the same transaction in the example of the process during which there may be a new commit, so select the same may return different results.

(3) Repeatable Read (can be re-read)

       This is the default MySQL transaction isolation level, it ensures that the same transaction multiple concurrent instances when data is read, it will see the same data row. But in theory, it will lead to another thorny issue: Magic Reading (Phantom Read). Simply put, phantom read means that when a user reads a range of data row, another transaction and insert a new row within the range, when the user re-read the range of data rows, you will find a new " Phantom "line. InnoDB and Falcon storage engine through a multi-version concurrency control (MVCC, Multiversion Concurrency Control) mechanism to solve the problem.

(4) Serializable (serializable)

     This is the highest level of isolation, it is forced to sort through the transaction, making it impossible to conflict with each other, so as to solve the problem phantom read. In short, it is to add a shared lock on each row of data read. At this level, it could lead to a lot of timeouts and lock contention. The four isolation levels to take a different type of lock is achieved, if the same data is read, then it is prone to problems. E.g:

  • Dirty read (Drity Read): a transaction has been updated copy of the data, another transaction at this time to read the same data, for some reason, before a RollBack operation, the firm after a data read on It would be incorrect.
  • Non-repeatable read (Non-repeatable read): In two inquiries into a transaction data is inconsistent, which may be inserted into the middle of the query process twice the original data in a transaction update.
  • Magic Reading (Phantom Read): Two query data items in a transaction not, for example, there is a transaction query the columns (Row) data, while another transaction but at this time insert new columns of data, previously transaction in the next query, there are several columns of data it is not out of the inquiry, if at this time and insert another transaction inserts data will error.

In MySQL, the realization of these four isolation levels, are likely to cause problems as follows:

 

  Recommended blog: (1)  MySQL transaction isolation level four

                    (2) Details of the four isolation levels (sec understand)


 

Guess you like

Origin www.cnblogs.com/2019wxw/p/11674819.html