MySql transaction isolation level

foreword

Databases are transactional, and different levels of transactions correspond to different levels of security for data operations. This article takes MySql as an example to introduce the transaction isolation level of the database.

 

Transaction Definition

A transaction is the change of data from one state to another.

The transaction isolation level is the degree to which a transaction sees data modified by other transactions.

 

transaction visibility

Dirty read: A transaction adds, deletes, or modifies data, but it is not committed, and another transaction can read the uncommitted data. If the first transaction rolls back at this time, then the second transaction has read the dirty data.

Non-repeatable read: Two read operations occur in a transaction. Between the first read operation and the second operation, another transaction modifies the data. At this time, the two read data are inconsistent.

Phantom read: The first transaction modifies a certain range of data in batches, and the second transaction adds a piece of data to this range. At this time, the first transaction will lose the modification of the newly added data.

 

Fantasy reads are repeatable reads, the difference is: the data that has been read is unchanged, just more data is seen.

 

transaction isolation level

isolation level The value of the isolation level cause problems
Read-Uncommitted 0 cause dirty reads
Read-Committed 1 Avoid dirty reads, allow non-repeatable reads and phantom reads
Repeatable-Read 2 Avoid dirty reads and non-repeatable reads, allow phantom reads
Serializable 3 Serialized read, transactions can only be executed one by one, avoiding dirty reads, non-repeatable reads, and phantom reads. The execution efficiency is slow, use it with caution

 MySql's default isolation level is repeatable read.

 

Summarize:

The higher the isolation level, the better the data integrity and consistency can be guaranteed, but the greater the impact on concurrent performance.

The default isolation level of most databases is Read Commited, such as SqlServer, Oracle, Postgresql

The default isolation level of a few databases is: Repeatable Read For example: MySQL InnoDB

 

Use Tips:

In general, because the cost of using the database transaction isolation level is too high, transaction control is generally performed at the application layer, such as using optimistic locking and distributed transactions, to eliminate the pressure of using transactions at the database layer.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326564635&siteId=291194637