当前位置:网站首页>Database transactions (often asked)
Database transactions (often asked)
2022-07-25 03:15:00 【C'z x】
One 、 Database transaction concept
Database transactions , It refers to some column operations performed as a single logical unit of work , Or both , Either not
A simple example : Xiao Ming turns to Xiao Hei 100 Yuan , Can be divided into Xiao Ming minus 100 element , Black plus 100 element . But you can't just perform one of these steps , Or both , Either not . This is the business .
Two 、 The nature of transactions (ACID)【 a key 】
Atomicity (Atomicity): A business (transaction) All operations in , Or complete it all , Or not at all , It doesn't end in the middle . An error occurred during the execution of the transaction , Will be rolled back (Rollback) Status to the beginning of the transaction , It's like this transaction has never been executed . namely , The business is indivisible 、 Irreducible .
Uniformity (Consistency): Before and after transaction start , The integrity of the database is not compromised . This means that the data to be written must fully comply with all default constraints 、 trigger 、 Cascade rollback, etc . such as : Before we transfer money, we add up to 200 element , After that, we always want to 100 element .
Isolation, (Isolation): The ability of a database to allow multiple concurrent transactions to read, write, and modify its data simultaneously , Isolation can prevent data inconsistency caused by cross execution when multiple transactions are executed concurrently . Transaction isolation is divided into different levels , Include uncommitted reads (Read uncommitted)、 Submit to read (read committed)、 Repeatable (repeatable read) And serialization (Serializable).
persistence (Durability): After transaction ends , Changes to data are permanent , Even if the system fails, it will not be lost .
3、 ... and 、 What are the problems of concurrent transactions ?( For example )
In a typical application , Multiple transactions run concurrently , Often operate the same data to complete their own tasks ( Multiple users operate on the same data ). Concurrency is necessary , But it may cause the following problems .
Dirty reading (Dirty read): When a transaction is accessing data and modifying the data , This modification has not yet been committed to the database , At this time, another transaction also accesses the data , And then I used this data . Because this data is not submitted yet , So the data read by another transaction is “ Dirty data ”, basis “ Dirty data ” The operation may not be correct .
Example : Business 1 Inquire about a data a , Business 2 Yes a Modify the data a‘ , Business 1 Query again a —>a’, Business 2 Roll back a’--->a, Business 1 Next, all operations are wrong
Missing changes (Lost to modify): When a transaction reads a data , Another transaction also accesses the data , After modifying the data in the first transaction , The second transaction also modifies the data . In this way, the modification result in the first transaction will be lost , So it's called lost modification . for example : Business 1 Read data from a table A=20, Business 2 Also read A=20, Business 1 modify A=A-1, Business 2 Also modify A=A-1, final result A=19, Business 1 The modification of is lost .
It can't be read repeatedly (Unrepeatableread): Reading the same data multiple times in a transaction . Before the end of the business , Another transaction also accesses the data . that , Between two reads in the first transaction , Due to the modification of the second transaction, the data read by the first transaction twice may be different . This happens when the data read twice in a transaction is different , So it's called unrepeatable reading .
Example : Business 1 Inquire about a data a , Business 2 Yes a Modify the data to a‘ Submit , Business 1 Inquire about a data —>a’, Business 1 The two queries are different
Fantasy reading (Phantom read): Unreal reading is similar to nonrepeatable reading . It happens in a transaction (T1) Read a few lines of data , Then another concurrent transaction (T2) When some data is inserted . In the subsequent query , The first thing (T1) There will be more records that don't exist , It's like an illusion , So it's called Unreal reading .
Example : Business 1 Inquire about surface a Number of all data pieces n strip , Business 2 Yes a Add table Submit , Business 1 Query again n+1 strip , The number of data pieces before and after is inconsistent
The difference between nonrepeatable reading and unreal reading :
The key point of non repeatable reading is to modify. For example, read a record many times and find that the values of some columns have been modified , The key point of magic reading is to add or delete a record. For example, read a record many times and find that the number of records increases or decreases .
Four 、 What are the transaction isolation levels ?MySQL The default isolation level for is ?
SQL The standard defines four isolation levels : Default REPEATABLE-READ( Repeatable )
READ-UNCOMMITTED( Read uncommitted ): Lowest isolation level , Allow read of uncommitted data changes , Can cause dirty reading 、 Phantom or unrepeatable reading .
READ-COMMITTED( Read committed ): Allow to read data submitted by concurrent transactions , Can prevent dirty reading , But phantom or unrepeatable reads can still occur .
REPEATABLE-READ( Repeatable ): Multiple reads of the same field are consistent , Unless the data is modified by the transaction itself , Can prevent dirty and unrepeatable read , But phantom reading can still happen .
SERIALIZABLE( Serializable ): Highest isolation level , Completely obey ACID Isolation level . All transactions are executed one by one , In this way, there is no interference between transactions , in other words , This level prevents dirty reads 、 Unrepeatable reading and phantom reading .

边栏推荐
- [jailhouse article] certify the uncertified rewards assessment of virtualization for mixed criticality
- Resolve the error: org.apache.ibatis.binding.bindingexception
- FLASH read / write problem of stm32cubemx
- Dynamic planning of force buckle punch in summary
- Eslint error
- Solve ''_ Xsrf 'argument missing from post
- Win10 -- open the hosts file as an administrator
- Algorithmic interview questions
- Merge sort / quick sort
- A. Subtle Substring Subtraction
猜你喜欢

Operator explanation - C language

Resolve the error: org.apache.ibatis.binding.bindingexception
![[stm32f103rct6] motor PWM drive module idea and code](/img/a5/3010acff73c8913e967ff3a024877e.png)
[stm32f103rct6] motor PWM drive module idea and code

Riotboard development board series notes (VIII) -- building desktop system
![[Kali's sshd service is enabled]](/img/1b/180534d51049177254e30c4b783eba.png)
[Kali's sshd service is enabled]

05 - MVVM model

Tensorflow's study notes (I)

JS written test questions -- random numbers, array de duplication

Unity refers to a variable in another class (its own instance)

Stm32cubemx quadrature encoder
随机推荐
JS method encapsulation summary
Arduino + si5351 square wave generator
Mark down learning
Take a note: Oracle conditional statement
Resolved (the latest version of selenium reported an error) attributeerror: module 'selenium webdriver‘ has no attribute ‘PhantomJS‘
kettle_ Configure database connection_ report errors
Backtracking to solve combinatorial problems
Use of stm32cubemonitor Part II - historical data storage and network access
Sum of "n" numbers of force deduction summary
mysql_ User table_ Field meaning
Download the jar package of jsqlparser and PageHelper
Publish the project online and don't want to open a port
Beginners must see the markdown User Guide
Bgy development small example
Dynamic programming -- Digital DP
Riotboard development board series notes (VII) -- the use of framebuffer
JS foundation -- task queue and event loop
Win10 -- open the hosts file as an administrator
Solve ''_ Xsrf 'argument missing from post
Concurrent programming day01