MySQL transactions and isolation levels

https://blog.csdn.net/weixin_43844810/article/details/86666007

Transaction has the following four characteristics:

(1) Atomicity: all transaction operations as an atomic unit, which can only be fully committed or rolled back completely to the data modification operation performed by the firm like.

(2) Consistency: When the transaction is completed, all data must be changed from one consistent state to another consistent state, all changes must be applied to modify the transaction to ensure data integrity.

(3) Isolation: modify a transaction manipulation statements made had to do with other firms revise isolated. When you view the data in which the state data during a transaction, either state before being changed by another concurrent transaction, either state after being modified by another concurrent transaction, that the current transaction will not see another concurrent transaction is modifying data this characteristic is achieved by locking mechanism.

(4) Persistence: After the transaction is complete, the impact of the changes made to data is permanent, even if the system reboot or system failure occurs data can still be recovered.

Transaction isolation level:

Can be isolated by the following statement to set the level of the transaction:

(1) uncommitted read

SET    GLOBAL   TRANSACTION    ISOLATION    LEVEL    READ    UNCOMMITTED;

(2) submit read

SET    GLOBAL   TRANSACTION    ISOLATION    LEVEL    READ    COMMITTED;

(3) repeatable read

SET    GLOBAL   TRANSACTION    ISOLATION    LEVEL    REPEATABLE     READ;

(4) serializable

SET    GLOBAL   TRANSACTION    ISOLATION    LEVEL    SERIALIZABLE;

 

Uncommitted read (READ UNCOMMITTED)

In this isolation level, all transactions can see other results did not commit the transaction, he will lead to dirty read

Dirty read: A transaction reads the contents of the transaction uncommitted B

At the same time open the transaction A and B, B before the transaction · updated but uncommitted, A transaction reads to the updated data, but the B roll back the transaction, the transaction appears A dirty read.

 

Read Committed (COMMITTED)

         From start to a transaction before submitting the changes made are not visible, the transaction can only see change firms have submitted done, this isolation level also supports the so-called non-repeatable read, because other forces of the same thing in the the new data may be submitted during the process leading to instances of data changes, so the same query might return different results.

Non-repeatable read: A transaction at the same time open and transaction B, after the B update and submit the transaction, A transaction reads to the updated data, this time in the same query A transaction emergence of different results, that is, non-repeatable read phenomenon .

 

Repeatable Read (REPEATABLE READ)  

       This is the default transaction isolation level mysql, and can ensure that multiple instances of the same thing when concurrent reads the data, you will see the same data line, in theory, lead to another problem, phantom reads. For example, a first transaction data in the table has been modified, such modifications are all the data in the table row design. Meanwhile, the second transaction also modifies the data in this table, this modification is to insert a new row to the table, the user operates the first transaction occurs after a discovery table has not modified data row.

 

Serializable (SERIALIZABLE)

This is the highest level of isolation, forced to sort through the transaction, making it impossible to conflict, so as to solve the problem phantom read, plus a shared lock is implemented on each data line read, at this level, could lead to a lot of overtime phenomenon and lock contention, generally do not agree to use

 

The type of lock

(1) a shared lock (S)

Shared lock or a lock granularity row is a tuple (a plurality of rows). After a transaction acquires a shared lock, you can lock the data read operation is performed on the range.

(2) exclusive lock (X)

The particle size of the same row share lock locks, as well as rows or tuples, a transaction acquires exclusive lock, data can be written to the lock range.

If two transactions A and B, if A transaction acquires a shared lock tuples, transaction B can immediately obtain a shared lock this tuple, but can not obtain an exclusive lock immediately tuple, you must wait until the transaction A release after a shared lock.

If the transaction A gained a tuple exclusive lock, the transaction B can not immediately acquire a shared lock this tuple, can not immediately acquire an exclusive lock this tuple, A must wait until the release of the exclusive lock.

(3) intent lock

Intent lock is a table lock, the lock is the entire table, do not lock into intent exclusive (IX) and intent shared lock (IS) categories, intent shared lock indicates that a transaction intends to share the data lock or exclusive lock. "Intentionally" refers to a transaction but has not really want to perform the implementation, between the lock and the lock is either compatible or mutually exclusive.

Compatibility: When operating a set of data, if the transaction obtaining the lock 1 a, 2 can also obtain other transaction lock b.

Mutex: a set of operation data, if a transaction acquire a lock, where a transaction can not acquire a lock 2 is released before the lock b 1 a transaction.

 

Lock granularity:

Lock granularity is divided into tables and row locks.

Minimum table lock lock management overhead while allowing concurrent amount is the smallest lock mechanism. MyISAM storage engine uses the lock mechanism. When data to be written, record the entire table is locked, then the other read / write operation always waiting, while certain actions, such as when using the ALTER TABLE lock table is performed.

Row lock supports a maximum concurrency. InnoDB storage engine uses the lock mechanism, if you want to support concurrent / read-write, InnoDB storage engine is recommended, because the use of row-level locking, you can get more performance.

Guess you like

Origin www.cnblogs.com/ldddd/p/11260263.html