How does MySQL implement the four major characteristics of transactions?

Analysis & Answers

If you don’t know about transactions, let alone the four major characteristics, please take a look first: What is a transaction?

atomicity

Either all statements are executed or none are executed, which is the core feature of a transaction. The transaction itself is defined by atomicity, and the implementation is mainly based on undo log

The undo log (rollback log) is the log provided by the InnoDB engine. The function of the undo log is to roll back the data. When a transaction modifies the database, the InnoDB engine will not only record the redo log, but also generate the corresponding undo log. If the transaction execution fails or rollback is called, causing the transaction to be rolled back, you can use the information in the undo log to roll back the data to the way it was before modification.

persistence

It ensures that after the transaction is submitted, data will not be lost due to downtime and other reasons. It is mainly implemented based on redo log .

The redo log uses WAL (Write-ahead logging, write-ahead log ). All modifications are first written to the log and then updated to the Buffer Pool, ensuring that the data will not be lost due to MySQL downtime, thus meeting the durability requirements.

Isolation

Ensure that the execution of transactions is isolated from each other, and the execution of transactions will not be affected by other transactions. The default database isolation level of the InnoDB storage engine is RR (repeatable read). RR is mainly based on the lock mechanism, hidden columns of data, undo log class and next-key lock mechanism.

The discussion of isolation can be mainly divided into two aspects:

consistency

Consistency means that after the transaction is executed, the integrity constraints of the database are not destroyed, and the data state is legal before and after the transaction is executed. Consistency is the ultimate goal pursued by transactions. Atomicity, durability, and isolation actually exist to ensure the consistency of the database state. The realization of transaction consistency requires guarantees at both the database level and the application level.

In other words, the AIDs in ACID are all characteristics of the database, that is, they depend on the specific implementation of the database. And only this C, in fact, it depends on the application layer, that is, on the developer. Consistency here refers to the migration of the system from one correct state to another correct state. What is the correct state? That is, the current state satisfies the predetermined constraints, which is called the correct state. And the transaction has the characteristics of C in ACID, which means that our consistency is guaranteed through the AID of the transaction.

Meow Interview Assistant: A one-stop solution to interview questions. You can search the WeChat applet [Meow Interview Assistant]  or follow [Meow Interview Assistant] -> Interview Assistant to  answer questions for free. If you have any good interview knowledge or skills, I look forward to sharing them with you!

Guess you like

Origin blog.csdn.net/jjclove/article/details/127392098