当前位置:网站首页>Two kinds of updates lost and Solutions
Two kinds of updates lost and Solutions
2022-07-07 21:59:00 【fastjson_】
Let's first create a user surface ,id Primary key
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT,
`money` int DEFAULT NULL,
`version` int DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
The first type of lost updates ( Rollback lost )
A When the transaction is cancelled , Put what has been submitted B The updated data of the transaction covers . This kind of mistake can cause serious problems , It can be seen from the following account :
Time | Withdrawals A | Transfer transaction B |
T1 | begin; |
|
T2 | begin; Start business | |
T3 | select money from user where id = 1; | |
T4 | select money from user where id = 1; | |
T5 | money = 1000 + 100; | |
T6 | commit; | |
T7 | money = 1000 - 100; | |
T8 | rollback; | |
T9 | Balance restored to 1000 element ( Lost update ) |
A When the transaction is rolled back ,“ Incaution ” take B The transaction has been erased from the account .
stay MySQL database , Any isolation level does not allow the first type of update to be lost
The second kind of lost updates ( Cover lost / Two update questions )
A Transaction coverage B Data that the transaction has committed , cause B The operation of the firm is lost :
Time | Transfer transaction A | Withdrawals B |
T1 | begin; | begin; |
T2 | select money from user where id = 1; | |
T3 | select money from user where id = 1; |
|
T4 | money = 1000 + 100; update user set money=1100 where id = 1; Remittance 100 element | |
T5 | commit; | |
T6 | select money from user where id = 1; |
|
T7 | money = 1000 - 100; update user set money=900 where id = 1; Take out 100 Change the balance to 900 element | |
T8 | commit; | |
T9 | Change the balance to 900 element ( Lost update ) |
In the above example, the deposit balance is updated because the withdrawal covers the transfer , The user lost in the end 100 element , On the contrary, if the withdrawal transaction is submitted first , Then the bank will lose 100 element .
The second reason for missing updates :MySQL By default, snapshot reads are used for repeatable reads . Read the snapshot There is no way to obtain the latest data . Therefore, snapshot reading is a major reason for the loss of the second type of updates .
solve Read the snapshot : Two basic ideas , One is pessimistic lock , The other is optimistic lock ;
Simply put, it is an assumption that such a problem is of high probability , It's best to lock it at the beginning , Lest the update always fails ; Another assumption is that such a problem is of small probability , The last step is to lock it when updating , In order to avoid locking for too long and affecting other people to do relevant operations .
Pessimistic locking
Lock when reading data , This can ensure that the data read is the latest data , And will lock the record , Other things will block if they want to get locks . So the overall business process becomes : Get the lock after opening things , Then different operations are carried out according to different business scenarios
| begin; Open transaction | begin; Open transaction |
| select money from user where id = 1 for update; Query account balance as 1000 element | |
| select money from user where id = 1 for update; Blocking ... | |
| money = 1000 - 100; update user set money=900 where id = 1; Take out 100 Change the balance to 900 element | Blocking ... |
| commit; | Blocking ... |
| Find the data :money = 900 | |
| Other operating .... |
Optimism lock
add to version Field , Record the updated version of each record , Add one to the version number after each update .
Get the version number of the line before each thing starts , Bring the version number to judge when updating .update money set money = money + 100 where id = 1 and version = The version number found
select version from user where id = 1; | |
select version from user where id = 1; | |
begin; | begin; |
money = 1000 - 100; | |
| The update is successful , The number of updates is 1 | money = 1000 + 100; update user set money=1100,version = version +1 where id = 1 and version = 0; Blocking ... |
| Blocking ... | |
| commit | |
| The update is successful , The number of updates is 0( Because another thing changed the version number, and now the version number is 1) |
summary :
Pessimistic locking b The way is through select..for update The way , This may cause other sessions to block , Optimistic lock b Method requires maintenance of one more version column .
Personal advice : Choose pessimistic locking method in the application system with few concurrent users and serious conflicts , In other cases, first of all, optimistic locking version listing .
边栏推荐
- How can big state-owned banks break the anti fraud dilemma?
- 海外代理推荐
- How to make agile digital transformation strategy for manufacturing enterprises
- NVR硬盤錄像機通過國標GB28181協議接入EasyCVR,設備通道信息不顯示是什麼原因?
- 建立自己的网站(18)
- How does win11 time display the day of the week? How does win11 display the day of the week today?
- Default constraint and zero fill constraint of MySQL constraint
- Jenkins user rights management
- Ad domain group policy management
- Ternary expressions, generative expressions, anonymous functions
猜你喜欢

Jenkins user rights management
![Jerry's about TWS channel configuration [chapter]](/img/94/fde5054fc412b786cd9864215e912c.png)
Jerry's about TWS channel configuration [chapter]

Virtual machine network configuration in VMWare
![Jerry's about TWS pairing mode configuration [chapter]](/img/fd/dd1e252617d30dd7147dbab25de5b4.png)
Jerry's about TWS pairing mode configuration [chapter]

The strongest installation of the twin tower model, Google is playing "antique" again?

The latest Android interview collection, Android video extraction audio

Automatic classification of defective photovoltaic module cells in electronic images

Win11时间怎么显示星期几?Win11怎么显示今天周几?

Ten thousand word summary data storage, three knowledge points

Validutil, "Rethinking the setting of semi supervised learning on graphs"
随机推荐
An in-depth understanding of fp/fn/precision/recall
为什么Win11不能显示秒数?Win11时间不显示秒怎么解决?
[uvalive 6663 count the regions] (DFS + discretization) [easy to understand]
Jerry's configuration of TWS cross pairing [article]
Feature generation
Win11如何解禁键盘?Win11解禁键盘的方法
How can big state-owned banks break the anti fraud dilemma?
OpenGL super classic learning notes (1) the first triangle "suggestions collection"
The maximum number of meetings you can attend [greedy + priority queue]
Why can't win11 display seconds? How to solve the problem that win11 time does not display seconds?
Use br to back up tidb cluster data to azure blob storage
Word inversion implements "suggestions collection"
Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
The latest Android interview collection, Android video extraction audio
Codeforces round 275 (Div. 2) C – diverse permutation (construction) [easy to understand]
Ad domain group policy management
Insufficient permissions
Jerry's key to initiate pairing [chapter]
Deadlock conditions and preventive treatment [easy to understand]
The function is really powerful!