当前位置:网站首页>Interview question MySQL transaction (TCL) isolation (four characteristics)
Interview question MySQL transaction (TCL) isolation (four characteristics)
2022-07-04 12:10:00 【Peng_ zhj】
Technology provided by database ( Business )(TCL classification )
In actual business development , Some business operations need to access the database multiple times . One service needs to send multiple messages SQL Statement to the database . You need to
The operation of accessing the database is performed as a whole , Or all SQL All statements are executed successfully .
If one of them SQL Statement failure , Roll back the transaction , be-all SQL All statements failed to execute .
Such as transfers :
1 tom 1000
2 rose 1000
To complete the transfer business requires operations 2 Secondary database
1. from tom Subtract 500
update account set money = money - 500 where id = 1 ;
2. take 500 Add to rose Account number
update account set money = money + 500 where id = 2 ;
How to control 2 strip sql Not all statements are successful , Or all of them are failures ?
We have to use the transaction technology provided by the database to help us achieve our goal
The role of Transaction Technology :
You can control multiple steps of a complete thing
As long as one step goes wrong, the whole thing will fail , As long as all steps are successful, it is successful to control the current thing
demand : Create account table , Complete the control of transfer through transaction technology
The transaction operations
mysql By default, each item sql Statements are submitted automatically
But we have to make every one sql Statements have to be submitted manually , So turn off automatic submission
Do transaction operations :
start transaction; Start a transaction ( Turn off auto submit Manual submission is enabled )
update
update
commit; Commit the transaction and close the current manual commit
rollback; Rollback the transaction and close the current manual commit
Conclusion : We can put a business that needs to operate multiple databases into one transaction ,
It is convenient for us to manage every impact of this business on database data
When a business is completed, it needs to operate multiple sql At the time of statement You have to use transactions …
Interview questions : Four characteristics of transactions (ACID)
Atomicity (Atomicity)
A transaction is an indivisible unit , Either the operations in the transaction occur , Or none at all .
Uniformity (Consistency)
The integrity of the data before and after the transaction must be consistent
Isolation, (Isolation)
When multiple users access the database at the same time , The transaction of one user cannot be interfered by the transactions of other users , Data between multiple concurrent transactions should be isolated from each other , Don't influence each other .
persistence (Durability)
Once a transaction is committed , It changes the data in the database permanently
Isolation of transactions
problem : If the transaction does not have isolation , There will be reading problems when reading database data
1 Dirty reading : Read uncommitted data ( It must be avoided )
One transaction reads uncommitted data from another transaction
2 It can't be read repeatedly ( in the light of update Modified )
The data contents read twice in a transaction are inconsistent
3 Virtual reading / Fantasy reading ( in the light of insert Inserted )
The amount of data read twice in a transaction is inconsistent
solve
Isolation level of database
read uncommitted Read uncommitted The above three problems will happen
read committed(oracle Default ) Read submitted Avoid dirty reading
repeatable read(mysql Default ) Repeated reading Avoid dirty reading and non repeatable reading
serializable Serialization Avoid all problems
Inquire about mysql Isolation level
show variables like "%isolation%";
Set the isolation level of the transaction
set global transaction isolation level Level string ;
1. Read uncommitted ( The lowest level , None of the above can be guaranteed )
read uncommitted:
2. Read submitted ( It can avoid dirty reading .)
read committed
3. Repeatable ( Avoid dirty reading 、 Occurrence of non repeatable reading . mysql The default isolation level of )
repeatable read
4. Serialization ( Avoid dirty reading 、 It can't be read repeatedly 、 Fantasy reading ( Virtual reading ) What happened )
Use serializable Isolation level , A transaction is not completed , Other business SQL It can't be carried out
Security :serializable>repeatable read>read committed>read uncommitted:
efficiency :read uncommitted>read committed>repeatable read>serializable
In enterprise development , What must be avoided is dirty reading
mysql Data default level :repeatable read
oracle Data default level :read committed
边栏推荐
- Common built-in modules
- How to disable debug messages on sockjs stomp - how to disable debug messages on sockjs Stomp
- DVC use case (VI): Data Registry
- About the use of URL, href, SRC attributes
- Sys module
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 7
- Summary of collection: (to be updated)
- os. Path built-in module
- queue
- Login operation (for user name and password)
猜你喜欢
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 13
Login operation (for user name and password)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 5
Ultimate bug finding method - two points
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16
Exness: positive I win, negative you lose
Lecture 9
2021-08-09
Star leap plan | new projects are continuously being recruited! MSR Asia MSR Redmond joint research program invites you to apply!
[the way of programmer training] - 2 Perfect number calculation
随机推荐
Dos and path
C language compilation process
Xiaobing · beauty appraisal
MySQL advanced review
Properties and methods of OS Library
Anti clockwise rotation method of event arrangement -- PHP implementation
Login operation (for user name and password)
VPS installation virtualmin panel
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 14
Simple understanding of generics
QQ set group information
World document to picture
2021 annual summary - it seems that I have done everything except studying hard
Haproxy cluster
Btrace tells you how to debug online without restarting the JVM
Serialization oriented - pickle library, JSON Library
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 18
[solve the error of this pointing in the applet] SetData of undefined
Usage of case when then else end statement
How to disable debug messages on sockjs stomp - how to disable debug messages on sockjs Stomp