mysql transaction isolation mechanism

https://segmentfault.com/blog/lant
the ACID properties of transactions 1.
{
Atomicity Atomicity
Consistency Consistency: database is always a transition from state to state to another consistent consistency;
Isolation Isolation: Generally , a firm changes made prior to the final submission of other transactions is not visible;
Durability Rev persistence: here's permanent `` should be understood as modified by the transaction data is actually stored in the table, rather than store in a place such as a temporary table
}
2. isolation transaction isolation level
{
    lowest isolation level rEAD UNCOMMITTED, visible to other matters and causing problems arise dirty read
    transaction there are four isolation levels (from low to high: 
    rEAD UNCOMMITTED, generally not used, no operations are to lock
    the rEAD COMMITTED, 
    to REPEATABLE the rEAD, solved: the problem is not dirty reads repeatable read problem
    SERIALIZABLE)
See isolation level of
the SELECT @@ SESSION.tx_isolation;
}

3. Question
{
3.1 READ COMMITED dirty read isolation level or above
{
a Transaction A reads write data in parallel to another transaction B is not final submissions read that this transaction is A dirty read (read because the transaction A the 'dirty' data 'non-persistent apos)

Solution: Use of isolation - READ COMMITED or more isolation level
** `READ COMMITED` level guarantees, as long as the data has been submitted before the current statement is executed is visible **. `REPEATABLE READ` attention and level area !!!

Solve the problem of dirty read, just to ensure that you read each transaction are persistent data only !!!!
}

3.2 Non-repeatable read the READ REPEATABLE
{
if reading the same data multiple times in a single transaction, just read between the two, another transaction, it has been completed and submitted to modify the data, then the question came: It may appear multiple times to read the results of inconsistencies.

REPEATABLE READ level to ensure that, as long as the data has been submitted before the execution of the current transaction are visible. READ COMMITED level of attention and area !!!

Ensure that the same transaction in the middle of the same data repeatedly read consistent results    
}


3.3 Magic Reading / gap locks InnoDB the default engine RR level has been automatically help us solve by MVCC
{
main point is that once read many records within a range of (direct access to all records, including the results or do statistical aggregation), found inconsistent results (standard Archives generally refers to record an increase, decrease record should also be considered phantom read).

MySQL's InnoDB engine default RR level has been automatically help by MVCC us to solve, so this level, you can also simulate not phantom read the scene; 
return to the RC isolation level, you and easy to phantom reads and non-repeatable read out confusion
}

3.4 Consistency non-blocking reads
{
database snapshot of the state of affairs applies to SELECT statements, but not necessarily for all DML statements. 
If you insert or modify some of the lines, and then commit the transaction, DELETE, or UPDATE statement from another REPEATABLE READ concurrent transactions issued may affect those lines just submitted, even if the transaction can not query them. 
If a transaction to update or delete rows submitted by different transactions, those changes become visible to the current transaction.    
}

3.5 MVCC concurrency control a lot of information in the read operation can be divided into two categories: a snapshot reading (snapshot read) and current reading (current read).
{
Snapshot read 
    read specific snapshot (for RC, Snapshot (ReadView) will be created in each statement. For RR, snapshots are created when a transaction starts)
    `` `
    simple operation to select (do not need to lock such as: select ... Lock in the MODE report this content share, select ... for Update)
    `` `
    for the operation also select
the current read
    to read the latest version of the record, not snapshots. In InnoDB, the current reading will not create any snapshot.    
    `` `
    The SELECT ... Lock the MODE report this content share in
    the SELECT ... Update for
    ` ``
    For the following operations, make the following operations obstruction:    
    `` `
    INSERT
    Update
    the Delete
    ` ``
    - at RR level, 
    the snapshot is read by MVVC (multi-version control) and undo log achieved, 
    the current reading is added manually record lock (lock records) and gap lock (lock gap) to achieve.
    So from the point of view above shows, if you need to display real-time data, still need to be achieved by locking. This time will be used next-key technology.
        
    }

3.6 lost update
{
The first lost update: when A withdrawal transaction, the updated transaction data B has been submitted covers.
    Such lost update problem will not happen, because InnoDB storage engine isolation levels are used exclusive lock, even MVCC is not a pure MVCC, also used the exclusive lock! So transaction A unfinished when other matters It is unable to make changes to the data related to the transaction a and submission.

The second category is missing Update: A transaction data covering B transaction has committed, resulting in the loss of B firm to do the operation.
    Such lost update problem, you can not rely on the first three isolation levels to solve, with the highest level of isolation can only Serializable or manually using optimistic locking, pessimistic locking to solve. (Highest level of isolation Serializable not be employed in a practical application scenario)
}

3.n highest isolation level SERIALIZABLE isolation can also solve phantom read, but the isolation level is rarely used in practice!
    
}

4.MVCC multi-version concurrency control
{
MySQL's most transactional storage engines do not actually realize a simple row-level locking. Based on lifting consider concurrent performance, they are generally at the same time to achieve a multi-version concurrency control (MVCC). Not only is MySQL, including Oracle, PostgreSQL and other database systems have achieved MVCC, but their implementation mechanism is different, because there is no single MVCC implementation of standards.

MVCC implementation of a variety of typical optimistic (optimistic) and pessimistic concurrency control (pessimistic) concurrency control.

MVCC only work in READ COMMITTED and REPEATABLE READ isolation level two.
The other two isolation levels and MVCC is not compatible enough, because the READ UNCOMMITTED always read the latest data line, rather than in line with the current transaction version of the row. The SERIALIZABLE will lock on all rows read.

MVCC is supported Mysql in transactional storage engine InnoDB;
cope with high concurrent transactions, MVCC than simply locking more efficient;
MVCC only work in READ COMMITTED and REPEATABLE READ isolation level two;
MVCC can use optimistic (optimistic) lock and pessimism (pessimistic) lock is achieved;
each database MVCC implementation is not uniform
InnoDB is MVCC is achieved by saving two hidden columns in each row behind the record

    
Current read and snapshot read
MySQL's InnoDB storage engine the default transaction isolation level is RR (repeatable read), through the "row exclusive lock + MVCC" achieve together, not only to ensure repeatable read, also partially prevent phantom read, and incomplete prevent;

3. Snapshot read (snapshot read)
simple select operations (excluding of course select ... lock in share mode, select ... for update)

4. The current reading (the Read Current)
the SELECT ... Lock the MODE report this content share in
the SELECT ... Update for
INSERT
Update
the Delete
, the current reading in the RR level, the snapshot is read by MVVC (multi-version control) and undo log is to achieve by adding record lock (lock records) and gap lock (lock gap) to achieve.
innodb in the case of a snapshot reading does not really prevent phantom read, but to avoid non-repeatable read and phantom read !!! In the current reading

}
It is also because MVCC InnoDB used in conjunction with the exclusive lock, not pure MVCC, so the first class lost update is not there, the general said second category refers to lost updates are lost updates.


5. locks and transaction isolation level
{
deadlocks, lock conflicts, row locks, table locks, read locks, write locks, optimistic locking, pessimistic locking

Dirty reads, non-repeatable read, phantom read can directly use transaction isolation level to avoid
core transaction isolation level is to lock each isolation level uses a different locking strategy, when analyzing several high concurrent transactions before the problem of isolation level (lock) as a pre-knowledge of nature is not the point, but the ultimate solution to the problem!    

InnoDB approach is: read does not affect write, write does not affect the reading.
    Reading does not affect write: When data is being read, the write operation of other matters will not go on waiting for the release of current affairs line S lock, but will go to read a line of snapshot data.
    Write does not affect read: When the data write operation is being performed, read other transactions will not go on waiting for the release of the current transaction row X lock, but will go to read a line of snapshot data.
}

Guess you like

Origin blog.csdn.net/Edu_enth/article/details/93849118