Transaction isolation Mysql learning

Let's talk about today's transaction, I believe we all know that the transaction ACID (Atomicity, Consistency, Isolation, Durability, namely atomicity, consistency, isolation, durability).

Atomicity: a transaction can not be represented in the division, and the operations in a transaction either succeed together or fail together;

Consistency: that the integrity of the data before and after the transaction to be consistent;

Persistence: that once the transaction to commit, then change the data will be stored permanently, and database downtime will not have any effect.

The first three are well understood, then we say that the transaction isolation, isolation transaction is that it would not interfere with each other and transactional matters . But the case of multiple affairs, or is prone to dirty reads, non-repeatable read and phantom read problems. We simply explain that the words,

Dirty read: when a transaction database A is a modified data but has not yet committed or rolled back, another transaction B reads the contents of the modified and used, transaction A then presented, this time caused by the dirty read .
Isolation level of uncommitted read: This situation only occurs.

 

Non-repeatable read: a plurality of operations in a transaction data A, (not eventual submission) during operation of the transaction, B has only to do the transaction process, and this value is changed, this time will result in the operations in a transaction A We found the data and for the first time is not the same. Is non-repeatable read.
This situation will only occur when: read uncommitted isolation level is read committed.

 

Magic Reading: A transaction re-reads data retrieval seen before in the same query, but found other transactions from inserting new data to meet their query, a phenomenon called phantom reads. Magic Reading refers to a phenomenon that occurs when the transaction is not performed independently, for example, a first transaction data in the table has been modified, such modification involves this table, "all the data lines." At the same time, also the second transaction to modify the data in this table, this modification is to insert "new row" to the table. Then, the user operates the first transaction will occur after the discovery of the table there is no modification of the data lines, as if the same happened hallucinations general solution is to increase the range of phantom read lock RangeS, narrow search range is read-only , thus avoiding the phantom reads.
This situation will occur in the back: read uncommitted, read committed, repeatable read isolation level.

 

Having the above problems, we say that a particular transaction isolation level, the lower the transaction isolation level higher efficiency. Transaction isolation level roughly divided into the following,

Uncommitted Read (read uncommited), read-committed ((read commited), Repeatable read (repeatable read) and serialization (serializable). Then I explain these types of isolation levels,

Uncommitted Read: Indicates that a transaction is not committed, other transactions will be able to read what it modified;

Read committed: Indicates a transaction must be committed, other transactions can be read into what it changes;

Repeatable read: Indicates a transaction from beginning to end to see the execution of the data you see when you open the data with this transaction is consistent;

Serialized: that a transaction regardless of read and write operations are locked, read and add read lock, write and write-lock, if multiple transactions occur when reading and writing conflicts, etc. must be executed before a transaction is completed, in order to executing the next transaction.

Code can query your current database transaction isolation level below

mysql> show variables like 'transaction_isolation';
Empty set, 1 warning (0.13 sec)

mysql default is empty. You can set the start value of the parameter transaction-isolation to RE AD-COMMITTED, where I also suggest that you put the transaction isolation set to "read committed." Of course, specific settings to what level will have to be based on business.

The principle of the transaction isolation

It creates a view in the database, when accessing the logical result of an attempt to prevail.

In the "repeatable read" isolation level down, the view is created when the transaction is started, and from start to finish with this in view, the reason is "the transaction from beginning to end to see the implementation of open data and transaction when you see the data are consistent ";

Under "read committed" isolation level, this view is created at the time of the beginning of each sql statement executed;

It should be noted that the "read uncommitted" isolation level down, there is no concept of view, because it is made directly to get the latest value;

"Serialized" Isolation levels but did not get the concept of view, because it is directly locked, so avoid other matters have concurrent access.

Services across the start-up mode

Mysql affairs start in the following ways,

1, explicit start a transaction, or begin using start transation, then there begin or submit start commit transaction begin or start rollback transaction rollback;

2, can also be set to automatically submit, set autocommit = 0 indicate to the transaction off the automatic submission, if an express start automatically submitted, of course, actually commit the premise must be to open a transaction, so here multi-step operation, begin open transaction. If you think this is very troublesome, you can also perform commit work and chain, means the automatic submission, and automatically start the next transaction.

 

Well, stop here today, if you have any questions, welcome to study and discuss together.

Guess you like

Origin www.cnblogs.com/gusluo/p/11299169.html