The problem of concurrent transactions and isolation levels

ACID properties of a transaction

Atomicity (Atomicity): the transaction is the smallest unit of execution, segmentation is not allowed. Atomicity of transactions to ensure that actions are either completed or totally ineffective.
Consistency (Consistency): Before and after the transaction, the data is consistent, the same results for a plurality of transaction data read is the same.
Isolation (Isolation): concurrent access to the database, a user transaction not interfere with the other firms, database between concurrent transactions are independent.
Persistent (Durability): After a transaction is committed. It changed data in the database is persistent, even if the database fails nor should it have any impact.

The problem of concurrent transactions

  • Lost modify (Lost to modify): Transaction A, after B simultaneously access data, modify this data A, B also modified, modify the result of the A's lost.
  • Dirty read (Dirty read): A transaction data has been modified, no modification submitted, transaction B "dirty data" is uncommitted.
  • Non-repeatable read (Unrepeatableread): the same data is read twice during the transaction A, B modifies the transaction data, the two read data A are not the same.
  • Magic Reading (Phantom read): A read data during a transaction period, transaction B is inserted or deleted several rows. In subsequent queries, A records found much fire, as if, like hallucinations occurred, so called phantom reads.

Non-repeatable read and phantom read difference:
there are different inconsistent grained, non-repeatable read A second reading is a record found value of some columns is modified, A phantom read focus is found to increase or decrease a few rows .

Isolation Levels

In the database transaction is defined in order to ensure the correctness of concurrent read and write data proposed, it is not a proprietary concept MySQL, but from SQL-92 standard ANSI / ISO developed.

Each relational databases provide their own characteristics to achieve the isolation level, the most common MySQL InnoDB engine, for example, it is MVCC (Multi-Versioning Concurrency Control) and lock complex to achieve, in accordance with the degree of isolation from low to high based, MySQL transaction isolation level is divided into four different levels:

  • Uncommitted Read (Read uncommitted): a transaction can see the changes other matters not yet submitted the lowest level of isolation, may appear dirty reads, non-repeatable reads or phantom reads . .
  • Read Committed (Read committed): guaranteed not to see the middle of the state before the uncommitted, is not guaranteed to get the same data is read again, can prevent dirty reads, but phantom reads or non-repeatable read may still occur .
  • Repeatable read (Repeatable reads): to ensure that the same transaction multiple times to read the results on the same field is the same, is the default isolation level MySQL InnoDB engine, can prevent dirty reads and non-repeatable reads, but phantom reads still occur .
  • Serialize (Serializable), all transactions executed one by one in turn, is the highest level of isolation, completely subordinate ACID, can prevent dirty reads, non-repeatable reads and phantom reads . To read means to acquire a shared read lock, update to acquire an exclusive write locks, if using SQL WHERE clause, will acquire the lock interval (MySQL GAP implemented in the form of lock, Repeatable Read will use the default, so the default isolation MySQ level can also prevent phantom read).

Optimistic and pessimistic locking lock

General use pessimistic locking like this SELECT ... FOR UPDATE statement to the data lock to prevent other transactions from modifying data. Optimistic locking it with Java and contracting in AtomicFieldUpdater similar, but also the use of CAS mechanism, will not have data locked, but by the time stamp or version number of comparative data, to achieve the required version optimistic locking judge. MVCC nature can be seen as optimistic locking mechanism, and exclusive write locks, dual-phase lock is achieved pessimistic lock.

reference

"Java core technology say 36"
"MySQL will know will be"

Guess you like

Origin www.cnblogs.com/ChengzhiYang/p/12512576.html