[Interview] must have a quick understanding of database transaction isolation level

Pictures show abnormalities can go https://juejin.im/post/5d5575dde51d4561a705badd View

Introduction to transactions

I believe used MySQL friends know affairs, we often through this example to explain the role of transaction: A transfer to B, where the database can be divided into two steps, reducing the account balance A, B increased account balances. However, if the sudden appearance of a database is down, etc. A decrease in account balances time, it is not there will be fewer A's balance, but the balance has not increased B situation? the answer is negative. The reason is that the database supports transactions (the commonly used relational data, such as MySQL, Oracle, etc.).

A transaction is a series of strict application of the operation, all operations must be completed successfully, otherwise made in each operation, all changes are undone. That is, the transaction has atomicity, a series of operations in a transaction either all succeed, or none do.

There are two end of the transaction, when the transaction so successfully execute all the steps, the transaction commits. If a step fails, the rollback operation will occur, so that before the undo operation to undo the transaction began.

ACID properties of a transaction

This is a commonplace problem, the interview also often ask: What are the ACID properties of a transaction? ACID here represent four words: atomicity (Atomicity), consistency (Consistency), isolation (Isolation) and persistent (Durability).

1, atomicity. A transaction is a logical unit of work database, all operations in the transaction included either do or do not do

2, consistency. The results of the implementation of the database transaction must be changed from one consistent state to another consistent state. Therefore, when the database contains only the result of a successful transaction commit, say the database in a consistent state. If a failure database system is running, some were forced to interrupt the transaction has not been completed, these transactions did not complete the modifications made to the database has been written to the physical part of the database, then the database will be in a bad state, or is inconsistent state.

3, isolation. Execution of a transaction not subject to interference by other transactions. I.e., operation and use of the data inside a transaction other concurrent transactions are isolated and can not interfere with each other between the respective transaction executed concurrently.

4, persistent. Also known as permanent, it means that once a transaction commits, changing the data in the database is that it should be permanent. The next operation or other faults should not have any impact on the results.

Transaction isolation level

He said that before the transaction isolation level, take a look at the following questions:

1, dirty reads (Dirty Read)

The so-called dirty read refers to a transaction to access data other uncommitted transactions. A transaction of such a modified data, but did not submit the transaction, then B A read transaction modifies the data transaction but not committed, which is dirty reads.

2, non-repeatable read (Non-repeatable read)

It refers to a transaction, reading the same data multiple times. When this transaction is not over, another transaction also access the same data. So, between the two read data in the first transaction, due to the modification of the second transaction, then the first two transactions read data may be different. This occurs in a transaction the two read data are not the same, so called non-repeatable read.

3, phantom read (Phantom Read)

Refers to the so-called phantom read within the same transaction multiple query returns a result set is not the same (such as an increase or decrease of rows). For example, within the same transaction A first query when there are n records, but the inquiry but n + 1 records under the same conditions a second time, it's like hallucinating, why not the same result twice that. In fact, and as non-repeatable read, phantom read occurs because of a transaction is another add or delete or modify the result set inside the first transaction data. Except that the content of non-repeatable data are read the same record is modified, phantom read data rows becomes more or less

After understanding the above several issues, let us look at four kinds of transaction isolation level.

1, Read Uncommitted (Uncommitted read the content)

In this isolation level, all transactions can see the results of other uncommitted transactions. This isolation level is rarely used in practice, because its performance is not much better than the other levels. Uncommitted read data, also called a dirty read.

2, Read Committed (read submission)

This is the default isolation level for most database systems (MySQL, but not the default). It meets the simple definition of isolation: a transaction can only see the change has been submitted firms do. This is called non-repeatable read (Non-repeatable read).

3, Repeatable Read (can be re-read)

This is the default MySQL transaction isolation level, it ensures that the same transaction multiple concurrent instances when data is read, it will see the same data row. But in theory, it will lead to another thorny issue: Magic Reading (Phantom Read). Simply put, phantom read means that when a user reads a range of data row, another transaction and insert a new row within the range, when the user re-read the range of data rows, you will find a new " Phantom "line. InnoDB and Falcon storage engine through a multi-version concurrency control ( MVCC , Multiversion Concurrency Control) mechanism to solve the problem.

4, Serializable (serialization)

This is the highest level of isolation, it is forced to sort through the transaction, making it impossible to conflict with each other, so as to solve the problem phantom read. In short, it is to add a shared lock on each row of data read. At this level, it could lead to a lot of timeouts and lock contention.

The four isolation levels problems that may arise summary

In addition to the worst performance of the Serializable (serialization) isolation level does not appear dirty reads, non-repeatable read, outside the phantom read, other isolation level there is one or more problems. But MySQL's InnoDB engine solves the phantom read problem Repeatable Read by MVCC way.

Transaction isolation level

View isolation level

# 查看当前连接的隔离级别
SELECT @@session.tx_isolation;
# 查看全局的事务隔离级别
SELECT @@global.tx_isolation;

Set the transaction isolation level

1, the global modification

On the mysql configuration file changes, such as the windows of mysql.ini, Linux is my.cnf.

#可选参数有:READ-UNCOMMITTED, READ-COMMITTED, REPEATABLE-READ, SERIALIZABLE.
[mysqld]
transaction-isolation = REPEATABLE-READ

2, the connection setting command current

# 可选参数为:read uncommitted、read committed、repeatable read、serializable
set session transaction isolation level xxx

No information leading public concern

Search public Happyjava number [], [Reply] and [video] e-books, e-books you can get a lot of quality and big data, kafka, nginx, MySQL and other videos
Access to information concern

Guess you like

Origin www.cnblogs.com/happy4java/p/11361290.html