A deep understanding of Spring transaction principles and a deep understanding of spring

Source: Internet
Author: User

A deep understanding of Spring transaction principles and a deep understanding of spring
I. Basic principles of transactions

The essence of Spring transactions is the database's support for transactions. Without the database's support for transactions, spring cannot provide transaction functions. To use transactions to operate databases only using JDBC, follow these steps:

With the Spring transaction management function, we can not write the code in steps 2 and 4, but automatically complete it by Spirng. Then, how does Spring start and close transactions before and after CRUD we write? To solve this problem, we can understand the implementation principle of Spring transaction management as a whole. The following is an example of the annotation method.

Ii. propagation attributes of Spring transactions

The propagation attribute of spring transactions defines how spring handles these transactions when multiple transactions exist at the same time. These attributes are defined in TransactionDefinition. The specific constants are described in the following table:

Constant name Constant Interpretation
PROPAGATION_REQUIRED Supports the current transaction. If no transaction exists, a new transaction is created. This is the most common choice and the propagation of Spring default transactions.
PROPAGATION_REQUIRES_NEW Create a new transaction. If a transaction exists, the current transaction is suspended. The new transaction will have nothing to do with the pending transaction. It is two independent transactions. After the outer transaction fails to be rolled back, it cannot roll back the results of the execution of the internal transaction, an exception is thrown when an internal transaction fails. The outer transaction is captured or cannot be rolled back.
PROPAGATION_SUPPORTS Supports the current transaction. If no transaction exists, it is executed in non-transaction mode.
PROPAGATION_MANDATORY Supports the current transaction. If no transaction exists, an exception is thrown.
PROPAGATION_NOT_SUPPORTED The operation is performed in non-transaction mode. If a transaction exists, the current transaction is suspended.
PROPAGATION_NEVER It is executed in non-transaction mode. If a transaction exists, an exception is thrown.
PROPAGATION_NESTED

If an active transaction exists, it runs in a nested transaction. If no active transaction exists, it is executed according to the REQUIRED attribute. It uses a separate transaction, which has multiple storage points that can be rolled back. Rollback of internal transactions does not affect external transactions. It is valid only for the DataSourceTransactionManager Transaction Manager.

Iii. Database isolation level
Isolation level Isolation level value Problems
Read-Uncommitted 0 Dirty read
Read-Committed 1 Avoid dirty reads and allow repeated and Phantom reads.
Repeatable-Read 2 Avoid dirty reads and avoid repeated reads. Allow Phantom reads.
Serializable 3 Serialized read, transactions can only be executed one by one, avoiding dirty reads, repeated reads, and Phantom reads. Execution efficiency is slow, so use with caution

Dirty read: A transaction adds, deletes, modifies, and does not commit data. Another transaction can read uncommitted data. If the first transaction is rolled back at this time, the second transaction reads dirty data.

Unrepeatable read: Two read operations are performed in a transaction. The data is modified between the first read operation and the second operation, at this time, the data read twice is inconsistent.

Phantom read: the first transaction batch modifies data within a certain range, and the second transaction adds a piece of data in this range. At this time, the first transaction will lose the modification to the new data.

Summary:

The higher the isolation level, the more data integrity and consistency can be guaranteed, but the greater the impact on concurrency performance.

The default isolation level of most databases is Read Commited, such as SqlServer and Oracle.

The default isolation level of a few databases is Repeatable Read, for example, MySQL InnoDB.

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.