Spring transaction isolation and transaction Propagation

Source: Internet
Author: User

Spring transaction isolation and transaction Propagation

Detailed description and comparison of Spring transaction isolation and transaction Propagation

Spring is the administrator of SSH and is responsible for managing other frameworks and coordinating the work of various parts. Let's take a look at Spring transaction management today. Spring transaction management is divided into declarative and programming. Declarative configuration is performed in the Spring configuration file; programmatic configuration is written into the code by annotation.

The transaction configuration in the Spring configuration file is always composed of three parts: DataSource, TransactionManager, and proxy mechanism. In any configuration method, the agent mechanism is changed. DataSource and TransactionManager only change according to the data access method. For example, when Hibernate is used for data access, DataSource is actually SessionFactory, and the implementation of TransactionManager is HibernateTransactionManager.

Transaction isolation level of spring

  • ISOLATION_DEFAULT: use the default database isolation level.
  • ISOLATION_READ_UNCOMMITTED: allows reading changed uncommitted data, which may lead to dirty reads, non-repeated reads, and Phantom reads.
  • ISOLATION_READ COMMITTED: Allows concurrent transactions to be read after being COMMITTED, which can avoid dirty reads and may lead to repeated and Phantom reads.
  • ISOLATION_REPEATABLE_READ: The read results of the same field are consistent multiple times, which can lead to phantom read.
  • ISOLATION_SERIALIZABLE: fully complies with ACID principles to ensure that dirty reads, non-repeated reads, and Phantom reads are not performed.

You can adopt an appropriate isolation level based on your system's requirements for data, because isolation involves locking records in the database, the stricter the requirements on positive data, and the worse the concurrency performance.

Spring transaction Propagation Behavior

  • The Propagation Behavior of spring transactions refers to how transactions operate when one method calls another method.
  • PROPAGATION_MANDATORY: This method must run in a transaction. If the current transaction does not exist, an exception is thrown.
  • PROPAGATION_NESTED: if a transaction exists, the method runs in a nested transaction. Nested transactions can be committed and rolled back independently from the current transaction. If no transaction exists, a new transaction starts. Each Vendor's support for such promotional activities is uneven.
  • PROPAGATION_NEVER: the current method should not run in a transaction. If a transaction exists, an exception is thrown.
  • PROPAGATION_NOT_SUPPORTED: the current method should not run in a transaction. If a transaction is running, it will be suspended during the running of the method.
  • PROPAGATION_REQUIRED: This method must run in a transaction. If a transaction is running, this method runs in this transaction. Otherwise, a new transaction is started.
  • PROPAGATION_REQUIRES_NEW: The method must run in its own transaction. It starts a new transaction. If an existing transaction is running, it will be suspended during the running of this method.
  • PROPAGATION_SUPPORTS: the current method does not need a transaction processing environment, but if a transaction is already running, this method can also be run in this transaction.

The above is an example of Spring transaction isolation and transaction propagation. If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article and hope to help you. Thank you for your support for this site!

Related Article

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.