MySQL transaction isolation level

One, four isolation levels

The SQL standard defines four types of isolation levels, including some specific rules to limit which changes inside and outside the transaction are visible and which are invisible. Low-level isolation levels generally support higher concurrent processing and have lower system overhead.

Read Uncommitted

At this isolation level, all transactions can see the execution results of other uncommitted transactions. This isolation level is rarely used in practical applications because its performance is not much better than other levels. Reading uncommitted data is also known as Dirty Read.

 

Read Committed

This is the default isolation level for most database systems (but not the MySQL default). It satisfies the simple definition of isolation: a transaction can only see changes made by committed transactions. This isolation level also supports the so-called non-repeatable read (Nonrepeatable Read), because other instances of the same transaction may have new commits during the processing of the instance, so the same select may return different results.

 

Repeatable Read

This is MySQL's default transaction isolation level, which ensures that multiple instances of the same transaction will see the same rows of data when reading data concurrently. In theory, though, this could lead to another thorny problem: Phantom Read. Simply put, phantom read means that when a user reads a range of data rows, another transaction inserts a new row in the range. Phantom" line. InnoDB and the Falcon storage engine solve this problem through the Multiversion Concurrency Control (MVCC, Multiversion Concurrency Control) mechanism.

 

Serializable (serializable)

This is the highest isolation level, and it solves the problem of phantom reads by forcing transaction ordering so that they cannot conflict with each other. In short, it adds a shared lock on each read data row. At this level, a lot of timeouts and lock contention can result.

 

2. Isolation level and consistency

These four isolation levels are implemented by different lock types. If the same data is read, problems are prone to occur. E.g:

Drity Read: A transaction has updated a piece of data, and another transaction has read the same piece of data at this time. For some reason, the previous RollBack operation is performed, and the data read by the latter transaction is would be incorrect.

Non-repeatable read (Non-repeatable read): The data is inconsistent between two queries of a transaction, which may be the original data updated by a transaction inserted between the two queries.

Phantom Read: The number of data is inconsistent in the two queries of a transaction. For example, one transaction queried several columns of (Row) data, while another transaction inserted new columns of data at this time. In the next query, the transaction will find that there are several columns of data that it did not have before.

In MySQL, these four isolation levels are implemented, which may cause problems as follows:

isolation level dirty read non-repeatable read hallucinations
Read Uncommitted yes yes yes
Read Committed no yes yes
Repeatable Read no no yes
Serializable no no no

 

3. Set the current isolation level

-- cancel autocommit

set autocommit=0

show variables like "%autocommit%";

 

-- View isolation level

SELECT @@global.tx_isolation;

SELECT @@session.tx_isolation;

SELECT @@tx_isolation;

 

show variables like '%iso%';

+---------------+-----------------+

| Variable_name | Value           |

+---------------+-----------------+

| tx_isolation  | REPEATABLE-READ |

+---------------+-----------------+

 

show global variables like '%iso%';

+---------------+-----------------+

| Variable_name | Value           |

+---------------+-----------------+

| tx_isolation  | REPEATABLE-READ |

+---------------+-----------------+

 

-- set the isolation level

SET SESSION TRANSACTION ISOLATION LEVEL read uncommitted;

SET SESSION TRANSACTION ISOLATION LEVEL read committed;

SET SESSION TRANSACTION ISOLATION LEVEL repeatable read;

SET SESSION TRANSACTION ISOLATION LEVEL serializable;

 

-- transaction operation

start transaction;

SELECT * FROM text.tx;

commit;

 

start transaction;

SELECT * FROM text.tx;

update text.tx set num =10 where id = 1;

insert into text.tx(id,num) values(9,9);

rollback;

commit;

 

Fourth, my.cnf settings

# MySQL supports 4 transaction isolation levels, they are:

# READ-UNCOMMITTED, READ-COMMITTED, REPEATABLE-READ, SERIALIZABLE.

# If not specified, MySQL defaults to REPEATABLE-READ, ORACLE defaults to READ-COMMITTED

transaction_isolation = REPEATABLE-READ

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326442235&siteId=291194637