当前位置:网站首页>Demonstration and solution of dirty reading, unrepeatable reading and unreal reading
Demonstration and solution of dirty reading, unrepeatable reading and unreal reading
2022-07-29 08:37:00 【Soup key.tj】
Catalog
Dirty reading
Question presentation
- First set the isolation level to read uncommitted
- Then open two windows
- Operate on a table at the same time
- Both open transactions
- Such as : Li Si wants to borrow Zhang San 500 element


-- Set the transaction isolation level to read uncommitted SET GLOBAL TRANSACTION ISOLATION LEVEL READ uncommitted; -- Open transaction START TRANSACTION; -- Transfer accounts UPDATE account SET money=money-500 WHERE name=' Zhang San '; UPDATE account SET money=money+500 WHERE name=' Li Si '; -- Inquire about account surface SELECT * FROM account; -- Roll back ROLLBACK;- Problem analysis
- At this time, Zhang San turns to Li Si 500 了 , But not submitted
- And ask Li Si to inquire
- Li Si checked again that the money had indeed arrived
- Then Zhang San said he needed Li Si to fight 500 Your IOU
- Li Si gave him an IOU , This has already taken effect in the legal sense
- After Zhang San got the IOU , Rolled back
- After the rollback is completed, the data is restored again
- After a while , Li Si goes to get the money 1500 element , Insufficient balance can be found
- At this time, Li Si went to check the balance and returned to 1000 element
- In this way, Li Si not only didn't receive the money , I also wrote a note to Zhang San 500 Your IOU
- This is a problem caused by dirty reading :
- Data in another uncommitted transaction was read during one transaction
solve
- Modify the transaction isolation level to read committed

-- Set the transaction isolation level to read committed SET GLOBAL TRANSACTION ISOLATION LEVEL read committed; -- Open transaction START TRANSACTION; -- Transfer accounts UPDATE account SET money=money-500 WHERE name=' Zhang San '; UPDATE account SET money=money+500 WHERE name=' Li Si '; -- Inquire about account surface SELECT * FROM account; -- Roll back ROLLBACK;It can't be read repeatedly
Question presentation
- First set the isolation level to read committed
- Then open two windows
- Operate on a table at the same time
- Both open transactions
- Such as : Li Si wants to borrow Zhang San 500 element

- Problem analysis
- For example, the boss of the company asked Xiao Wang to inquire about the revenue of the company in that year
- What Xiao Wang found in the morning was 100 ten thousand
- It's time for the meeting , Will take this 100 Wan told the boss
- But it changed again in the afternoon , Turned into 80 ten thousand
- What Xiao Wang told his boss was 100 ten thousand , The data queried by the boss is 80 ten thousand
- This shows that the data queried many times is different
- : Read the data modified and submitted in another thing during the processing of one thing
solve
- Modify the transaction isolation level to repeatable read

-- Set the transaction isolation level to repeatable read SET GLOBAL TRANSACTION ISOLATION LEVEL REPEATABLE read; -- Open transaction START TRANSACTION; -- Transfer accounts UPDATE account SET money=money-500 WHERE name=' Zhang San '; UPDATE account SET money=money+500 WHERE name=' Li Si '; -- Inquire about account surface SELECT * FROM account; -- Commit transaction COMMIT;- If you want to see the modified data in window 2
- Window 2 is also required commit, Again select see
Fantasy reading
Question presentation
- First set the isolation level to repeatable read
- Then open two windows
- Operate on a table at the same time
- Both open transactions
- window 1 First, only add records without submitting
- And then to the window 2 Check it out
- Discovery window 2 There are no newly added records
- So in the window 2 Add a new record again
- But there is no way to add , Will be stuck
- Back to the window 1 After the commit transaction
- window 2 Immediately report the wrong , Unable to add
- Then in the window 2 Do commit transactions
- At the window 2 Query again and find that
solve
- Set the isolation level to serializable
- After modification, the window 2 When you inquire, you will get stuck
- Then in the window 1 Do commit transactions , window 2 You can immediately query successfully
- So you don't have to be in the window 2 Stuck when adding
- There is no need to think that there is no such operation as adding
边栏推荐
猜你喜欢
随机推荐
GBase 8s数据库有哪些备份恢复方式
Collation of ml.net related resources
Basic crawler actual combat case: obtaining game product data
MySQL中的时间函数
SAP sm30 brings out description or custom logical relationship
Pnpm install appears: err_ PNPM_ PEER_ DEP_ ISSUES Unmet peer dependencies
01背包关于从二维优化到一维
[from_bilibili_dr_can][[advanced control theory] 9_ State observer design] [learning record]
Google browser cross domain configuration free
正则表达式校验版本号
C language calculates the length of string
Simulation of four way responder based on 51 single chip microcomputer
Chrony time synchronization
leetcode hot 100(刷题篇9)(301/45/517/407/offer62/MST08.14/)
Deep learning (1): prediction of bank customer loss
Cloud security daily 220712: the IBM integration bus integration solution has found a vulnerability in the execution of arbitrary code, which needs to be upgraded as soon as possible
Hc-sr04 use method and routine of ultrasonic ranging module (STM32)
What if official account does not support markdown format file preparation?
Common query optimization technology of data Lake - "deepnova developer community"
New energy shared charging pile management and operation platform














