About the transaction, the transaction isolation levels and dirty reads, non-repeatable read, phantom read to understand

What is a transaction?

  Services that database transactions. It is a logical unit of execution of a database management system, a configuration of a finite sequence of database operations .

  In general, the proper execution of the transaction will make the database from one state to another state .

Characteristics of the transaction (ACID principles)

  •  Atomicity (atomicity) that is the indivisibility of the transaction either all executed or not executed whole.
  •  Perform consistency (Consistency) so that the transaction database correct conversion from one state to another state correctly.
  •  Isolation (isolation) correctly submitted before the transaction, the transaction is not allowed to change to the data available to any other business.
  •  After persistence (durability) transactions submitted correct, the results will always be stored in the database.

 

Problem of concurrent transactions will produce a state of

  Concurrent state interpreted as when the transaction A and transaction B operate on the same resource, you may encounter a lot of problems.

Read dirty (uncommitted data for)

  That transaction A B read data transaction has not been submitted. A If the transaction data has been updated, but the transaction A does not commit, but transaction B this time to see the update transaction A does not commit. When a transaction A has been rolled back, just to see the data transaction B is dirty. That is dirty reads.

  example:

  A turn to B 100 million, but has not yet submitted A, B check their accounts at this time, more than 100 million. A wrong turn and found the man, rolled back the things. Then B 100 Wan gone. In this process, found in the B data (the extra one million), which is a dirty read uncommitted.

Non-repeatable read (in a transaction which is read twice a data inconsistency read out data for modification operations)

  That same transaction during the execution of a transaction on the same data has been read many times, but each time the results of the data read is not the same. The reason is read twice in the interval, the data do not modify the others, resulting in a unified transaction twice read inconsistent results.

  example:

  A check bank balances 1,000,000, B this time took away 500 000, then the balance becomes 500,000 A check balances once again, become 50 million. Inconsistent results is not repeated reading of A in terms twice.

Magic Reading (data has not been found to operate in a single transaction operation for additions and deletions operation)

  A process i.e. transaction data set read multiple times, the transaction data B to the new operation or a delete operation, resulting in multiple reads of data sets A transaction is inconsistent.

  example:

  A modification of the current employee information all the time, B to insert a new employee, when A submission this time he did not found a modified information staff, in terms of A is like hallucinating.

 

Transaction isolation level

  In response to the issues arising under the above concurrency, transaction isolation level is produced. The higher the transaction isolation level when the above issues will be less, but the performance will be greater consumption. Therefore, in the actual production process, to determine the isolation level according to demand.

The four isolation levels

READ_UNCOMMITTED

  Uncommitted Read, i.e., capable of reading the data is not submitted, so it is clear that this level can not be solved in isolation mechanism dirty reads, non-repeatable read, read in any of a phantom.

READ_COMMITED

  Has been submitted, that can read data that has been submitted, it is possible to prevent the dirty read, but can not solve the problem of non-repeatable read and phantom read.

REPEATABLE_READ

  Read repeatedly that the lock after the data read out, similar to the "select * from XXX for update", clear data read out is to use the update, so to add a lock to prevent others modify it. REPEATABLE_READ meaning is similar, a data read, this is not the end of the transaction, other transactions will not be able to change this record, this would resolve the dirty read, non-repeatable read problem, but the phantom read problem is not solved.

SERLALIZABLE

  Serialization, the highest transaction isolation level, no matter how many transactions, one by one run to completion of a transaction of all sub-transactions before you can perform all the sub-transaction inside another transaction, this would resolve the dirty read, non-repeatable read and phantom read problem.

Guess you like

Origin www.cnblogs.com/jyroy/p/11106609.html