transactions, transaction characteristics, transaction isolation levels, spring transaction propagation characteristics

Source: Internet
Author: User

1. What is a transaction:

A transaction is a rigorous set of operations in a program, and all operations must be completed successfully, or changes made in each operation will be undone, which is the atomicity of the transaction (either success or failure).

2. Transaction characteristics:

Transaction characteristics are divided into four: atomicity (atomicity), consistency (consistency), isolation (isolation), persistence (durability), or acid.

    1. Atomicity (atomicity): A transaction is a logical unit of work for a database, and the operations contained in a transaction either perform successfully or fail.
    2. Consistency (consistency): The result of transactional execution must be to change the database data from one consistent state to another. When the transaction executes successfully, it says that the database is in a consistent state. If an error occurs during execution, some of the modifications made to the database by these outstanding transactions have been written to the physical database, which is the database is in an inconsistent state.
    3. Isolation (Isolation): The execution of a transaction cannot affect the execution of other transactions, that is, the operation within one transaction and the data used are isolated to other transactions, and there is no interference between the concurrent execution of the transactions.
    4. Persistence (Durability): Once a transaction is committed, its changes to the database data are permanent. Subsequent operations should not have any effect on their execution results.

3. Isolation level of the transaction:

The isolation level of the transaction is also divided into four categories, from low to high, namely: Read uncommited (reading uncommitted), read commited (read commit), read repeatable (read repeat), Serializable (serialization), These four levels can solve the problems of dirty reading, non-repeatable reading, and Phantom reading one after the other.

    1. Read uncommited: Is the lowest transaction isolation level, which allows another transaction to see uncommitted data for this transaction.
    2. Read commited: Ensure that one thing is committed before it can be read by another transaction. Another transaction cannot read data that is not committed by the object.
    3. REPEATABLE READ: This level of transaction isolation prevents dirty reads and cannot be read repeatedly. But phantom reads may occur. In addition to ensuring that one transaction cannot read uncommitted data from another transaction, the following conditions are avoided (non-repeatable read).
    4. Serializable: This is the most cost-effective, but most reliable, transaction isolation level. Transactions are processed for sequential execution. In addition to preventing dirty reading, not repeatable read, but also avoid phantom reading.
    5. Dirty read, non-repeatable read, Phantom Read concept description:
      • Dirty reads: When a transaction has a positive access to the data, and the data has been modified, and the data has not yet been submitted to the database, another transaction accesses the data and then uses that data. Because this data has not yet been submitted, then another transaction reads this data that we call dirty data. It is not correct to do the work according to the dirty data.
      • Non-repeatable read: Refers to reading the same data multiple times within a transaction. In the event that the transaction has not been completed and another transaction accesses the same data, the data that is read from the first transaction to two reads may not be the same as the second transaction is modified, and the data in the first two reads can be different, so that two consecutive reads in a thing are not the same, This condition is known as non-repeatable reading.
      • Phantom read: A transaction successively reads a range of records, but the number of records two reads is different, we call it Phantom Read (two executions of the same SELECT statement will result in different results, the second read will add a data row, does not say that two executions are in the same transaction)

Spring Transaction Propagation Characteristics:

Transaction propagation behavior is how transactions are propagated between these methods when multiple transactional methods are called each other. Spring supports 7 types of transactional propagation behavior:

      • Propagation_requierd: If there is currently no transaction, create a new transaction, which is the most common choice if there is already a transaction in which to join the transaction.
      • Propagation_supports: Supports the current transaction and is executed as a non-transactional method if there is no current transaction.
      • Propagation_mandatory: Uses the current transaction and throws an exception if there is no current transaction.
      • Propagation_required_new: Creates a new transaction and suspends the current transaction if a transaction is currently present.
      • Propagation_not_supported: Executes the operation in a non-transactional manner, suspending the current transaction if a transaction is currently present.
      • Propagation_never: Executes the operation in a non-transactional manner and throws an exception if the current transaction exists.
      • Propagation_nested: Executes within a nested transaction if a transaction is currently present. If there is no current transaction, perform a similar operation as propagation_required

Spring's default transactional propagation behavior is propagation_required, which is suitable for most situations. Assuming that Servivex#methodx () is working in a transactional environment (that is, it is enhanced by Spring transactions), assume that the following call chain exists in the program: Service1#method1 ()->service2#method2 () SERVICE3#METHOD3 (), then the 3 methods of these 3 service classes work in the same transaction through Spring's transaction propagation mechanism.

transactions, transaction characteristics, transaction isolation levels, spring transaction propagation characteristics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.