The four attributes, isolation levels and three operating modes of MySQL transactions

Affairs

  A transaction is a unit of concurrency control, a sequence of operations defined by the user.

Four attributes of transactions (ACID)

  1. Atomicity: After the transaction starts, all operations are either done or not done. It is impossible to stop in the middle link. If an error occurs during the execution of the transaction, it will roll back to the state before the start of the transaction, and all operations will appear to have not happened. In other words, affairs are an indivisible whole, just like atoms learned in chemistry, they are the basic unit of matter.
  2. Consistency: Before and after the transaction, the integrity constraints of the database are not destroyed. For example, if A transfers money to B, it is impossible for A to deduct the money, but B does not receive it.
  3. Isolation: At the same time, only one transaction is allowed to request the same data, and there is no interference between different transactions. For example, A is withdrawing money from a bank card, and B cannot transfer money to this card before the process of A withdrawing money is finished.
  4. Durability: After the transaction is completed, all updates made by the transaction to the database will be saved to the database and cannot be rolled back.

Transaction concurrency

  1. Dirty read: Transaction A reads the data updated by transaction B, and then B rolls back the operation, then the data read by A is dirty data.
  2. Non-repeatable read: Transaction A reads the same data multiple times, and transaction B updates and commits the data during multiple reads of transaction A, which results in inconsistent results when transaction A reads the same data multiple times.
  3. Phantom reading: System administrator A changed the scores of all students in the database from specific scores to ABCDE grades, but system administrator B inserted a record of specific scores at this time. When system administrator A finished the change, he found that there was still If a record is not changed, it is as if a hallucination has occurred. This is called a phantom reading.

  Non-repeatable reading focuses on modification, while phantom reading focuses on adding or deleting. To solve the problem of non-repeatable reads, you only need to lock the rows that meet the conditions, and to solve the phantom read, you need to lock the table.

MySQL transaction isolation level

Transaction isolation level Dirty read Non-repeatable Phantom reading
Read uncommitted (read-uncommitted) Yes Yes Yes
Non-repeatable read (read-committed) no Yes Yes
Repeatable-read no no Yes
Serializable no no no

Three modes of MySQL transaction operation

1. Automatically commit the transaction: each individual statement is a transaction, and each statement implies a commit.
2. Explicit transaction: start with begin transaction and end with commit or rollback.
3. Implicit transaction: when the previous transaction is completed, the new transaction is started implicitly, but each transaction still ends with commit or rollback display

Guess you like

Origin blog.csdn.net/weixin_43283397/article/details/109551181