MySQL database --- transaction interview focus

1. What does a transaction do (what problem does it solve)?

Let's take an example here
. Xiaoming's account has 1,000 yuan, and Xiaohong's account has 500 yuan.
Xiaoming initiates a transfer to Xiaohong, and transfers 500 to Xiaohong.

Specific operation:

  1. Xiao Ming's account is -500 yuan
  2. Xiaohong's account is +500 yuan

However, due to some other reasons (such as network crash), after performing operation 1, it crashes, then the money in Xiaoming's account is deducted, but the money in Xiaohong's account is not added.
Once operation 1 is performed, the second step is wrong, There is a problem with the data at this time.
Transactions are used to solve such a problem.

2. The concept of transaction

A transaction is to tie the above two steps together, and they either succeed or fail together.

3. Characteristics of Transactions

The characteristics of MySQL transactions are divided into four points (ACID):

Atomicity:

It means that several operations in the transaction are either all executed or not executed at all . (Not executed here does not mean that these operations are not executed, but that if a certain step in the middle is wrong, it will be rolled back from this step. rollback)

For example: the above 1 operation is successfully executed, 2 operation is not executed, then let Xiao Ming's account + 500 yuan

Consistency:

Before and after the transaction is executed, the integrity of the database will not be destroyed by these operations, that is, before and after the transaction is executed, the data is always in a legal state.

For example: Xiaoming originally only had 1,000 yuan. It is illegal to transfer 1,500 yuan to Xiaohong.

Durability:

After the transaction is completed, the modification of the data by its operation will continue.

For example: The transaction completes the modification of the data and uploads it to the disk (after the submission is successful). At this time, the data in the database will not be destroyed if a failure occurs.

isolation:

The database allows multiple transactions to concurrently perform modification and read operations at the same time. Isolation can be used to prevent data inconsistencies when concurrent modification and deletion are performed.

5. Dirty reads

Dirty read means that a transaction A is performing an operation to modify data. At this time, transaction A has not completed the modification operation. At this time, another transaction B has read the content at this time. At this time, the read operation of transaction B is a dirty read,
because At this moment, transaction A has not completed the modification operation, and the data just read by transaction B may be modified again at any time.

example:

For example, the transfer example of Xiaoming to Xiaohong.
Originally, the balance of Xiaoming's account is 1000. The balance of Xiaohong's account is 500.
At this time, transaction A executes the operation: Xiaoming transfers 500 yuan to Xiaohong.
At this time, transaction B executes the operation: read Xiaoming's account balance .
When the transaction When A performed the operation of Xiaoming's account -500, before the operation of Xiaohong's account +500 was performed, transaction B read the balance of Xiaoming's account of 500.
However, transaction A had an error at this time and a rollback occurred. Xiao Ming's account has become 1000 again, but transaction B reads 500.

Graphical timeline:

insert image description here

Solution:

Lock the write operation . When a transaction is performing the write operation, other transactions cannot perform the read operation. When the transaction completes the write operation and releases the lock, other transactions can read.

6. Non-repeatable read

Non-repeatable read means that during the execution of transaction A, the data read multiple times is not the same, which is called non-repeatable read.

example:

Xiao Ming wants to check his own grades and see others' grades, the teacher uploads the grades and checks the grades.

Transaction A is executed: read Xiaoming's grades -> look at other people's grades -> check your own grades again
Transaction B is executed: upload grades -> find wrong grades and modify grades
When transaction B performs a write operation, transaction A wants to read It is found that the write operation is locked, and there is no way to read, only waiting for the completion of the write operation. When the write operation of transaction B is completed, transaction A reads the operation for the first time and finds that Xiaoming scored 80 points in the test. After the first read, the entire transaction A still It is not over, transaction B has carried out a modification operation, and modified Xiaoming's score to 60 points. At this time, when transaction A went to read again, it was found that Xiaoming only had 60 points, and the data before and after was inconsistent, that is, it cannot be read repeatedly.

Graphical timeline:

insert image description here

Solution:

The read operation is also locked . When transaction A is reading, transaction B cannot modify the operation. Transaction B can only operate after transaction A has finished executing and releasing the lock.

7. Phantom reading

Phantom read means that during the execution of transaction A, the result sets read multiple times are different (the total amount of data is different).
Although both read operations and write operations are locked, they can be added.

example:

Xiaoming checks the amount of summer homework, and the teacher uploads the summer homework.
Transaction A Xiaoming checks the summer homework -> Xiaoming performs other operations -> Xiaoming checks the summer homework again
. Transaction B The teacher adds summer homework.
When task A is executed, The first time I read the operation, I found that the summer homework was only 10 pages. Then Xiaoming performed other operations. At this time, transaction B added an operation and added 90 pages.
When transaction B checked again, it was found that the number of pages of the homework became 100 pages.

Graphical timeline:

insert image description here

Solution:

Complete serialization , that is, when transaction A is executing, transaction B can do nothing but hang up and wait.

8. MySQL isolation level

1. read uncommitted

Allow reading of uncommitted data

Note: The isolation is the lowest, the concurrency is the highest, and there is a problem of dirty reading

2. read committed

Only read committed data is allowed, which is equivalent to locking the write operation.

Note: Isolation is improved, concurrency is reduced, dirty reads are solved, but there are non-repeatable reads

3. repeatable read

The read is also locked, which is the default isolation level of MySQL

Note: The isolation has been improved, the concurrency is lower, and the non-repeatable read has been solved, but there are phantom reads.

4. serializable

Strictly serialized execution

Note: The isolation is the highest, the concurrency is the lowest, and the problem of phantom reading is solved.

Guess you like

Origin blog.csdn.net/wwzzzzzzzzzzzzz/article/details/123746109