The characteristics of the transaction and the isolation level of the transaction


Four characteristics of transactions

1. What is a transaction

A transaction refers to a series of rigorous logical operations in a program, and all operations must be completed successfully, otherwise all changes made in each operation will be undone.

2. Four characteristics of transaction (ACID)

  • Atomicity : When operating these instructions, either all of them are executed successfully, or none of them are executed. As long as one of the instructions fails to execute, all instructions fail to execute, and the data is rolled back to return to the data state before the instruction was executed.
  • Consistency (Consistency) : The execution of the transaction causes the data to transition from one state to another, but the integrity of the entire data remains stable.
  • Isolation : When multiple users access the database concurrently, such as when operating the same table, the transaction opened by the database for each user cannot be interfered by the operation of other transactions. Multiple concurrent transactions must Isolated from each other.
  • Durability : When the transaction is completed correctly, the change to the data is permanent.

Problems caused by concurrent transactions

  • Dirty read
    Dirty read refers to the data in another uncommitted transaction that is read during a transaction process.
    Insert picture description here
  • Non-repeatable read
    A transaction reads the same row of data twice, and the result is different states. The data is updated by another transaction in the middle. The two results are different and cannot be trusted.
    Insert picture description here
  • Phantom read
    a transaction to perform two queries, the second result set contains data that did not or some rows have been deleted in the first time, resulting in inconsistent results, but another transaction inserted or deleted in the middle of the two queries Data. Phantom reading is a phenomenon that occurs when transactions are not independently executed.
    Insert picture description here

Isolation level of database transactions

  • Read uncommitted (READ UNCOMMITTED)
    in the read uncommitted isolation level, transaction A can read transaction B modified but uncommitted data.
    Dirty reads, non-repeatable reads, and phantom reads may occur when reads are not committed. Generally, this isolation level is rarely used.
  • Read committed (READ COMMITTED)
    In the read committed isolation level, transaction B can only read the data modified by transaction B after transaction A has been modified and committed.
    The read submitted isolation level solves the problem of dirty reads, but non-repeatable reads and phantom reads may occur. Generally, this isolation level is rarely used.
  • Repeatable read (REPEATABLE READ)
    In the repeatable read isolation level, transaction B can only read the data modified by transaction B after transaction A has modified the data and submitted it, and after submitting the transaction itself.
    The repeatable read isolation level solves the problems of dirty reads and non-repeatable reads, but phantom reads may occur.
  • Serializable (SERIALIZABLE) the
    highest transaction isolation level. Under this level, transactions are executed serially in order to avoid dirty reads, non-repeatable reads, and phantom reads. However, this transaction isolation level is inefficient and consumes database performance, so it is generally not used.
    Insert picture description here

note

  • The default transaction isolation level of most databases is Read committed, such as Sql Server and Oracle. Mysql's default isolation level is Repeatable read.
  • The isolation level setting is only valid for the current link. For the use of the MySQL command window, a window is equivalent to a link, and the isolation level set by the current window is only valid for the transaction in the current window; for the JDBC operating database, a Connection object is equivalent to a link, and for the Connection object The set isolation level is only valid for this Connection object, and has nothing to do with other linked Connection objects.
  • The isolation level of the database must be set before the transaction is started.

Guess you like

Origin blog.csdn.net/qq_42647711/article/details/108387048