MySQL transaction characteristics and transaction isolation level

The concept of affairs

Database transaction (abbreviation: transaction) is a logical unit in the execution of the database management system, which consists of a limited sequence of database operations.

Transaction characteristics (ACID)

Transactions have four important characteristics:

  • Atomicity (Atomicity)
    all operations after the start of the transaction, either all completed or all failed. If an error occurs during the execution of the transaction, it will roll back to the state before the transaction started.

  • Consistency
    refers to the transformation of a database from one state to another consistent state by a transaction. Before and after the start of the transaction, the integrity of the database has not been destroyed.

  • Isolation
    requires that the objects of each read and write transaction can be separated from the operation objects of other transactions. That is, the transaction is not visible to other transactions before committing.
    Note: MySQL guarantees transaction isolation through a lock mechanism.


  • Once a Durability transaction is committed, the result is permanent.
    Note: MySQL uses redo log to ensure the permanence of transactions.

Dirty read

A transaction reads data modified by another uncommitted transaction. For example, transaction A reads a record (id=1, name="zhangsan"), and then changes "zhangsan" to "lisi", but transaction A has not yet committed, at this time another transaction B arrives and also reads This record is taken, and the data read is (id=1, name="lisi"). If transaction A rolls back, the record read by transaction B will be a non-existent data. This phenomenon is called dirty read.

Non-repeatable

When the same record is read twice within a transaction, the results obtained are inconsistent. For example, transaction A reads a record (id=1, name="zhangsan"), before transaction A ends, another transaction B also reads the same record (id=1, name="zhangsan") , And then changed "zhangsan" to "lisi" and submitted the modification. When transaction A reads this record again (id=1, name="lisi"), it found that the records read two times before and after are inconsistent. This phenomenon is called non-repeatable reading.

Phantom reading

When querying some records with the same query condition twice in a transaction, the number of records obtained is inconsistent. For example, transaction A finds 4 records under certain query conditions. Before transaction A ends, transaction B inserts (or deletes) a record that meets the query conditions of transaction A, and then transaction A queries the same conditions again, The number of records obtained is inconsistent.

Transaction isolation level

  • READ UNCOMMITED (read uncommitted)
  • READ COMMITED (read has been submitted)
  • REPEATABLE READ (repeatable read)
  • SERIALIZABLE (serializable)
Isolation level Dirty read Non-repeatable Phantom reading
READ UNCOMMITED (read uncommitted)
READ COMMITED (read has been submitted) ×
REPEATABLE READ (repeatable read) × ×
SERIALIZABLE (serializable) × × ×

Note: √: will happen ×: will not happen

Recommend related articles:

https://developer.ibm.com/zh/technologies/databases/articles/os-mysql-transaction-isolation-levels-and-locks/

https://juejin.im/post/6844903827611582471

Guess you like

Origin blog.csdn.net/qq_47768542/article/details/109317183