当前位置:网站首页>What is the transaction of MySQL? What is dirty reading and what is unreal reading? Not repeatable?
What is the transaction of MySQL? What is dirty reading and what is unreal reading? Not repeatable?
2022-07-06 14:38:00 【Floating~】
Tips : Although the role of affairs in actual work is not great , But the written examination at school , The interview is the key examination topic of the database !!! Therefore, Baozi should keep up their spirit to read this article !!!
Catalog
Two 、 The isolation level of the transaction
3、 ... and 、 Methods and steps of implementing transactions
Four 、 The isolation level of the transaction
Read Uncommited, Read uncommitted :
Read Commited, Read operations :
Repeatable Read, Repeated reading :
Preface :
Mysql Relational databases are transaction enabled by default , This article will use MySQL Database and Navicat Explain things for everyone . This article will introduce in detail what is a transaction , The four characteristics of affairs , The isolation level of the transaction , Let's start today's study !
One 、 What is business
Let's first look at the official definition of affairs :
Business , Is to put a group SQL Statements are executed in the same batch , If one SQL Statement error , Then all of the SQl Will be cancelled .
Be careful :MySQL Only innoDB and BDB Database tables of type support transactions !
If you don't understand the official concept , Don't panic ! Let's take a look at the following example :
Say 500 years ago m78 There is a man named Sophie in the nebula, although he is the captain of the universe guard , But he always gets beaten when he comes out , So I want to buy armor to make myself more resistant to beating , So ran went to the earth and found a Gamay armor ...
Brother , Your bubble dragon and clothes look good , Give me ten stacks .
B: yes, you can ! Brother has eyes , I'll pack it for you !!!
But Sophie is old and poor , I want to run with my armor w(゚Д゚)w
Let's think about it , Is it OK for Sophie not to pay for her clothes ! It's not going to work !
So Dega said :" You must pay and deliver with one hand , This is a complete deal !"
The embodiment of affairs : Payment and delivery must be completed , Business is a success , Any activity fails , The whole transaction failed !
Let's take a look at two understandings of affairs :
Understand one :
Business can be seen as a big activity , It's made up of different little activities , These activities are either all successful , All or nothing .
Understanding two :
Transaction can be regarded as a big operation , It consists of a series of operations , These operations are either all successful , All or nothing .
Two 、 The isolation level of the transaction
1、 Atomicity (Atomicity)
It means that all operations contained in the transaction are either successful , All or nothing , Therefore, if the transaction operation succeeds, it must be fully applied to the database , If the operation fails, there will be no impact on the database .
Let's think about it , Although zophe didn't have a lot of money , But he still has a little private money , So he said to Dega , Can you pay in installments , But Dega said , Big brother , Little brother, this is also a small business , Must be paid in a lump sum , No credit !!!
The transaction reflects : Atomicity is indivisibility , Money can't be paid in installments .
2、 Uniformity (Consistency)
It means that the transaction must change the database from one consistency state to another consistency state , That is to say, a transaction must be in a consistent state before and after execution .
Sophie really can't , You can only borrow money from his third brother Saiwen , Sophie said :“ Look, big brother lent you 91 Give it back to you 1 Block row no , Give me a change ”
Saiwen was immediately surprised :“ Although you are my big one , But you can't bully your little brother like this , Pay back as much as you have to borrow !!!”
The transaction reflects : In the process of borrowing money , How much you borrow must be consistent with how much you repay later .
3、 Isolation, (Isolation)
It refers to when multiple users access the database concurrently , For example, when operating the same table , A transaction opened by a database for each user , It cannot be interfered by the operation of other transactions , Multiple concurrent transactions should be isolated from each other .
Ask Saiwen to borrow , Sophie suddenly felt that she was suddenly rich , So I want to buy better armor , But the money is not enough , So he asked his second younger brother to borrow money :“ Second brother ! You see, brother is a little short of money recently , Why don't you borrow some, brother money, At that time, if you return the money to the third brother, you will pay back the money to the second brother ”
The first generation listen , I was at a loss :“ Big brother ! The third brother is the third brother , The money you borrowed from us is irrelevant ! It's time to pay back !!!”
The transaction reflects : Does the money that Sophie borrowed from Saiwen and chudai have anything to do with , Obviously, it doesn't matter , And borrowing money from the early generation or repaying money does not affect borrowing money from Saiwen to repay money . This process is not affected by other parts .
4、 persistence (Durability)
Once a transaction is committed , So the change to the data in the database is permanent , Even in the case of database system failure, the transaction commit operation will not be lost .
Zuo Fei finally took the money and bought the armor he was thinking about , So I can't wait to find the little monster to fight alone , As a result, although he played 18 kinds of Martial Arts , But unexpectedly, the armor was smashed by a palm , At this time, even if Zuo Fei is sad , The armor has broken , Unable to recover .
The transaction reflects : Once the transaction is committed , Changes to database data are permanent !!!
3、 ... and 、 Methods and steps of implementing transactions
Four 、 The isolation level of the transaction
Read Uncommited, Read uncommitted :
That is, one transaction can read the data of another uncommitted transaction ; Concurrent operations can lead to dirty reads
Read Commited, Read operations :
That is, a transaction cannot read data until another transaction is committed ; Solve the dirty reading problem ; Concurrent operations can result in non repeatable reads
Repeatable Read, Repeated reading :
Start reading data ( The transaction open ) when , Modification operations are no longer allowed ; Solve the problem of non repeatable reading ; Concurrent operations can cause unreal reads ( Corresponding insert operation )
Serializable, Serializable :
Highest transaction isolation level , At this level , Sequential execution of transactions ; Avoid dirty reading 、 No repetition or unreal reading ; But this level is inefficient , Compare consumption of database performance , Generally do not use .
Let's take a look at the concurrency problems that occur in several isolation situations :
1. Dirty reading
One transaction reads another uncommitted data .
2. It can't be read repeatedly
Within the scope of a transaction , Two identical queries return different data .
3. Fantasy reading
Two identical queries within a transaction scope return different data , The corresponding is the insert operation .
For the convenience of memory , I summarize the isolation level of transactions and concurrency problems in the following table :
features || problem | Dirty reading | It can't be read repeatedly | Fantasy reading |
Read uncommitted | √ | √ | √ |
Read the submission | √ | √ | × |
Repeatable | √ | × | × |
Serializable | × | × | × |
Finally, I wish you all treasure children will get something after reading ^_^!!!
also ! also ! Do you still believe in light ???
边栏推荐
- servlet中 servlet context与 session与 request三个对象的常用方法和存放数据的作用域。
- Keil5-MDK的格式化代码工具及添加快捷方式
- 我的第一篇博客
- Pointer -- eliminate all numbers in the string
- [pointer] use the insertion sorting method to arrange n numbers from small to large
- C language learning summary (I) (under update)
- [pointer] delete all spaces in the string s
- Record an edu, SQL injection practice
- 函数:求两个正数的最大公约数和最小公倍
- Intranet information collection of Intranet penetration (5)
猜你喜欢
随机推荐
JDBC事务、批处理以及连接池(超详细)
An unhandled exception occurred when C connected to SQL Server: system Argumentexception: "keyword not supported:" integrated
Proceedingjoinpoint API use
MySQL中什么是索引?常用的索引有哪些种类?索引在什么情况下会失效?
The difference between layer 3 switch and router
循环队列(C语言)
Feature extraction and detection 14 plane object recognition
MySQL learning notes (stage 1)
使用 flask_whooshalchemyplus jieba实现flask的全局搜索
Harmonyos application development -- address book management system telmanagesys based on listcontainer [phonebook][api v6]
JDBC 的四种连接方式 直接上代码
Function: string storage in reverse order
《统计学》第八版贾俊平第一章课后习题及答案总结
Attack and defense world misc practice area (simplerar, base64stego, no matter how high your Kung Fu is, you are afraid of kitchen knives)
Interview Essentials: what is the mysterious framework asking?
Chain team implementation (C language)
Hcip -- MPLS experiment
Sqqyw (indifferent dot icon system) vulnerability recurrence and 74cms vulnerability recurrence
Attack and defense world misc practice area (GIF lift table ext3)
图书管理系统