search
HomeDatabaseMysql TutorialMySQL's four transaction isolation levels
MySQL's four transaction isolation levelsDec 15, 2020 am 10:24 AM
mysqlisolation level

mysql tutorialThe column introduces four transaction isolation levels

MySQL's four transaction isolation levels

Recommended (free): mysql tutorial

The test environment for this article’s experiment: Windows 10 cmd MySQL5.6.36 InnoDB

1. Basic elements of transactions (ACID)

 1. Atomicity: After the transaction starts, all operations are either completed or not. When doing something, it is impossible to stagnate in the middle. If an error occurs during transaction execution, it will be rolled back to the state before the transaction started, and all operations will be as if they did not happen. That is to say, affairs are an indivisible whole, just like atoms learned in chemistry, which are the basic units of matter.

 2. Consistency: Before and after the transaction starts and ends, the integrity constraints of the database are not destroyed. For example, if A transfers money to B, it is impossible for A to deduct the money but B not to receive it.

3. Isolation: At the same time, only one transaction is allowed to request the same data, and there is no interference between different transactions. For example, A is withdrawing money from a bank card. B cannot transfer money to this card before A's withdrawal process is completed.

4. Durability: After the transaction is completed, all updates to the database by the transaction will be saved to the database and cannot be rolled back.

2. Transaction concurrency issues

1. Dirty read: Transaction A reads the data updated by transaction B, and then B rolls back operation, then the data read by A is dirty data

2. Non-repeatable reading: transaction A reads the same data multiple times, and transaction B reads the same data multiple times in transaction A , the data was updated and submitted, resulting in inconsistent results when transaction A read the same data multiple times.

3. Phantom reading: System administrator A changed the scores of all students in the database from specific scores to ABCDE grades, but system administrator B inserted a specific score at this time record. When system administrator A completed the modification, he found that there was still one record that had not been modified. It was as if he had hallucinated. This is called phantom reading.

Summary: Non-repeatable reading and phantom reading are easily confused. Non-repeatable reading focuses on modification, while phantom reading focuses on adding or deleting. To solve the problem of non-repeatable reading, you only need to lock the rows that meet the conditions. To solve the problem of phantom reading, you need to lock the table

3. MySQL transaction isolation level

##Read-uncommittedYesYesYesNon-repeatable read (read-committed)NoYesYesRepeatable-readNoNoYesSerializableNoNoNo

The default transaction isolation level of mysql is repeatable-read

4. Use examples to illustrate each isolation level

1. Read uncommitted:

  (1) Open a client A, and set the current transaction mode to read uncommitted (uncommitted read), query the initial value of the table account:

    (2) Before client A's transaction is committed, open another client B and update the table account:

                       The transaction has not yet been submitted, but client A can query the updated data of B:

  (4) Once client B's transaction is rolled back for some reason , all operations will be revoked, and the data queried by client A is actually dirty data:

                                       luck rid us] The data queried by client A is actually dirty data: set balance = balance - 50 where id =1, lilei's balance did not become 350, but actually 400. Isn't it strange? The data is inconsistent. If you think so, you are too naive. In the application, we will use 400 -50=350, I don’t know that other sessions have been rolled back. To solve this problem, you can use the read-committed isolation level

 2. Read-committed

  (1) Open a client A, and set the current transaction mode to read committed (read committed), query all records of the account table:

   ( 2) Before the transaction of client A is submitted, open another client B and update the table account:

  (3) At this time, the transaction of client B has not yet been completed Submit, client A cannot query the updated data of B, solving the dirty read problem:

  (4) Client B’s transaction submission

 (5) Client A executes the same query as the previous step, but the result is inconsistent with the previous step, which causes a non-repeatable read problem

3. Repeatable read

​ (1) Open a client A, set the current transaction mode to repeatable read, and query all records of the account table

                      All records of the account are consistent with the query results in step (1), and there is no non-repeatable read problem

##  (4) On client A, then execute update balance = balance - 50 where id = 1, the balance does not become 400-50=350, lilei's balance value is calculated using the 350 in step (2), so it is 300, and the consistency of the data is not destroyed. The MVCC mechanism is used under the repeatable read isolation level. The select operation does not update the version number, but is a snapshot read (historical version); insert, update, and delete will update the version number and is a current read (current version).

(5) Re-open client B, insert a new data and submit

(6) In the client End A queries all the records in the account table, and no new data is found, so there is no phantom reading

## 4. Serialization

 (1) Open a client A, set the current transaction mode to serializable, and query the initial value of table account:

 (2) Open a client B and set the current transaction mode to serializable. When inserting a record, an error is reported. The table is locked and the insertion fails. When the transaction isolation level in mysql is serializable, the table will be locked, so phantom reads will not occur. In this case, this isolation level has extremely low concurrency and is rarely used in development.

 Supplement:

 1. When the transaction isolation level is read-commit, writing data will only lock the corresponding row

 2. When the transaction isolation level is repeatable read, if the search condition has an index (including the primary key index), the default locking method is next-key lock; if the search condition There is no index, and the entire table will be locked when updating data. A gap is locked by a transaction, and other transactions cannot insert records in this gap, thus preventing phantom reads.

 3. When the transaction isolation level is serialization, reading and writing data will lock the entire table

 4. The higher the isolation level, the more complete and consistent the data can be guaranteed, but the greater the impact on concurrency performance.

5. MYSQL MVCC implementation mechanism reference link: https://blog.csdn.net/whoamiyang/article/details/51901888

6. Regarding the next-key lock, please refer to the link: https://blog.csdn.net/bigtree_3721/article/details/73731377

Transaction isolation level Dirty read Non-repeatable read Phantom read

The above is the detailed content of MySQL's four transaction isolation levels. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
图文详解mysql架构原理图文详解mysql架构原理May 17, 2022 pm 05:54 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于架构原理的相关内容,MySQL Server架构自顶向下大致可以分网络连接层、服务层、存储引擎层和系统文件层,下面一起来看一下,希望对大家有帮助。

mysql的msi与zip版本有什么区别mysql的msi与zip版本有什么区别May 16, 2022 pm 04:33 PM

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

mysql怎么去掉第一个字符mysql怎么去掉第一个字符May 19, 2022 am 10:21 AM

方法:1、利用right函数,语法为“update 表名 set 指定字段 = right(指定字段, length(指定字段)-1)...”;2、利用substring函数,语法为“select substring(指定字段,2)..”。

mysql怎么替换换行符mysql怎么替换换行符Apr 18, 2022 pm 03:14 PM

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

mysql怎么将varchar转换为int类型mysql怎么将varchar转换为int类型May 12, 2022 pm 04:51 PM

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

MySQL复制技术之异步复制和半同步复制MySQL复制技术之异步复制和半同步复制Apr 25, 2022 pm 07:21 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

mysql怎么判断是否是数字类型mysql怎么判断是否是数字类型May 16, 2022 am 10:09 AM

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。

带你把MySQL索引吃透了带你把MySQL索引吃透了Apr 22, 2022 am 11:48 AM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function