What is transaction, transaction feature, transaction isolation level, spring transaction propagation feature

1. What is a transaction:

A transaction is a series of rigorous operations in a program. All operations must be completed successfully, otherwise the changes made in each operation will be undone. This is also the atomicity of transactions (either success or failure).

2. Transaction features:

Transaction features are divided into four: Atomicity, Consistency, Isolation, Durability, or ACID for short.

  1. Atomicity: A transaction is a logical unit of work in the database, and the operations contained in the transaction either execute successfully or fail.
  2. Consistency: The result of transaction execution must be to change database data from one consistent state to another. When the transaction is executed successfully, the database is said to be in a consistent state. If an error occurs during execution, and some of the modifications made to the database by these outstanding transactions have been written to the physical database, the database is in an inconsistent state.
  3. Isolation: The execution of one transaction cannot affect the execution of other transactions, that is, the operations and data used within a transaction are isolated from other transactions, and there is no interference between concurrently executing transactions.
  4. Durability: Once a transaction is committed, its changes to database data are permanent. Other operations after that should not have any effect on the result of its execution.

3. Transaction isolation level:

The isolation levels of transactions are also divided into four types, from low to high: read uncommited, read committed, read repeatable, and serializable. Dirty reads, non-repeatable reads, and phantom reads can be solved one by one.

  1. read uncommited: is the lowest transaction isolation level, which allows another transaction to see the uncommitted data of this transaction.
  2. read committed: Ensure that a transaction can only be read by another transaction after it is committed. Another transaction cannot read the transaction's uncommitted data.
  3. Repeatable read: This transaction isolation level prevents dirty reads and non-repeatable reads. But phantom reads may occur. In addition to ensuring that a transaction cannot read uncommitted data by another transaction, it also avoids the following situations (non-repeatable reads).
  4. serializable: This is the most expensive but most reliable transaction isolation level. Transactions are processed for sequential execution. In addition to preventing dirty reads, non-repeatable reads, it also avoids phantom reads.
  5. Dirty read, non-repeatable read, phantom read concept description:
    • Dirty read: Refers to when a transaction is accessing data and modifying the data, but the data has not been submitted to the database, at this time, another transaction also accesses the data, and then uses the data. Because the data has not been submitted, the data read by another transaction is called dirty data. Actions based on dirty data can be incorrect.
    • Non-repeatable read: Refers to reading the same data multiple times within a transaction. Before this transaction is executed, another transaction also accesses the same data, then between the two reads of data in the first transaction, due to the modification of the second transaction, the data read twice by the first transaction It may be different, so it happens that the data read twice consecutively in one thing is not the same, this situation is called non-repeatable read.
    • Phantom read: A transaction reads a range of records successively, but the number of records read twice is different, we call it a phantom read (two executions of the same select statement will produce different results, and the second read will increase by one. data row, it doesn't say that the two executions are in the same transaction)

Spring transaction propagation features:

Transaction propagation behavior is how transactions are propagated among multiple transaction methods when they call each other. Spring supports seven transaction propagation behaviors:

    • propagation_requierd: If there is no current transaction, create a new transaction. If there is an existing transaction, join the transaction. This is the most common choice.
    • propagation_supports: supports the current transaction, if there is no current transaction, it is executed in a non-transactional method.
    • propagation_mandatory: use the current transaction, throw an exception if there is no current transaction.
    • propagation_required_new: Create a new transaction. If there is a current transaction, suspend the current transaction.
    • propagation_not_supported: Execute the operation in a non-transactional manner, and suspend the current transaction if there is a current transaction.
    • propagation_never: Perform the operation in a non-transactional manner, throwing an exception if the current transaction exists.
    • propagation_nested:如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与propagation_required类似的操作

Spring 默认的事务传播行为是 PROPAGATION_REQUIRED,它适合于绝大多数的情况。假设 ServiveX#methodX() 都工作在事务环境下(即都被 Spring 事务增强了),假设程序中存在如下的调用链:Service1#method1()->Service2#method2()->Service3#method3(),那么这 3 个服务类的 3 个方法通过 Spring 的事务传播机制都工作在同一个事务中。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324517643&siteId=291194637