How to ensure transaction isolation

First, what is a transaction

  Transaction: The transaction is a database operation or multiple SQL statements an integral work units.

  Features: all operations in a transaction which are normally completed, the entire transaction was only submitted into the database, if an action is not completed, then the entire transaction is rolled back.

Second, the four characteristics of the transaction (ACID)

  A: Atomicity:

    The same transaction may be multiple sql statements either all succeed, either directly rollback

  C: Consistency:

    Data and operating status change transaction is consistent result that is written must be in full compliance with the preset rules, the system will not lead to accidents and other reasons inconsistent state.

  I: Isolation:

    A firm operating data prior to submission, not visible to other transactions

  D: Persistence:

    Firms do modifications will be saved permanently and will not lead to data loss due to accidental system.

Third, the transaction isolation

  1, isolation, solve three problems

    Dirty read - not yet submitted data is read out

    Not reread - before and after reading the contents of the database is inconsistent

    Read the database inconsistencies total - Magic Reading

  2, isolation level

    RU-- Read Uncommitted : there will be: dirty read, can not re-read, phantom read

    RC-- Read Committed : prevent dirty reads, there will not be re-read and phantom read

    RR-- repeatable read : prevent dirty reads, non-repeatable read, may be a phantom read (default level)

    SR-- serialization : what can prevent (multiple synchronization session window, rather than concurrently, poor performance)

Fourth, how to ensure the isolation ?

  1, multi-version concurrency control (mvcc) (prevent non-duplication)

    ReadView - ReadView is actually a snapshot creates a snapshot of each transaction for the first time after starting the query is executed

  2, the lock (to solve the problem of phantom read

    Gap Locks (gap lock) - between the index record locking, or locking the first index before recording, or locked after the last index record

    Next-Key Locks (next key lock) - lock on the index records, and locked in the gap before the index record

 

Guess you like

Origin www.cnblogs.com/youhongliang/p/12177805.html