The propagation mechanism of spring affairs

Source: Internet
Author: User
Tags savepoint

Spring Transaction propagation mechanism:
SPRING Specifies the type of transactional propagation behavior in the Transactiondefinition interface, which specifies how transactions are propagated when the transaction methods and transaction methods are nested.
That is, a rule that coordinates the transaction context (whether there is an independent transaction isolation level and a lock) between the methods in which the transaction is identified and when the call occurs.

Nested Concepts of transactions:
The so-called transaction nesting is the invocation of two transaction methods between each other. Spring transactions are opened, either interface-based or class-based proxies are created (note that if the agent
You cannot manually new an object, and this class (with or without interfaces) must be proxied ———— the beans in spring will be proxied only if they are included in IOC management).
So a method in the same class invokes another transactional method, and the transaction does not work.

Overview:
When we call a spring-based service interface method, such as Userservice#adduser (), it will run in a spring-managed transactional environment.
The service interface method may internally invoke other service interface methods to collectively complete a complete business operation, as it generates the Services interface method
In the case of nested calls, spring controls how the current transaction propagates to the target service interface method of the nested call through the transaction propagation behavior.

Transaction propagation is an important concept of spring for transaction management. In the following section, we will analyze the manifestations of different transactional propagation behaviors to understand the differences between them.

I. Types of business communication activities

SPRING Specifies 7 types of transactional propagation behavior in the Transactiondefinition interface, which specifies how transactions are propagated when the transaction methods and transaction methods are nested:

The following is the Org.springframework.transaction.TransactionDefinition interface.

 Packageorg.springframework.transaction; Public Interfacetransactiondefinition {intpropagation_required = 0; intPropagation_supports = 1; intPropagation_mandatory = 2; intPropagation_requires_new = 3; intpropagation_not_supported = 4; intPropagation_never = 5; intpropagation_nested = 6; intIsolation_default =-1; intisolation_read_uncommitted = 1; intisolation_read_committed = 2; intIsolation_repeatable_read = 4; intIsolation_serializable = 8; intTimeout_default =-1; intGetpropagationbehavior ();//The propagation behavior of a transaction    intGetisolationlevel ();//isolation level of a transaction    intGetTimeout ();//the expiration time of the transaction    BooleanIsReadOnly ();//Read and write characteristics of transactionsString getName ();//Get name}

Required: Is the default transaction propagation behavior for spring. The specified method must be executed within the transaction, and if there is no transaction, the transaction is automatically created and executed.

Support: There are no transactions to execute, there is a transaction executed in the transaction, otherwise it is executed directly.

Mandatory: If a transaction is currently present, the transaction is executed and an exception is thrown if no transaction is currently present.

Requires_new: Always creates a new transaction, if the current method has a transaction, suspends the current method transaction first (executes the newly created transaction, and then executes the transaction of the current pending method itself) until the newly created transaction has finished executing.

Not_supported: Can no longer be executed in a transaction, and the current method transaction is suspended if there is a transaction in the current method.

Never: cannot be executed in a transaction and throws an exception if there is a transaction in the current method.

Nested: Executes within a nested transaction if a transaction is currently present. If there is currently no transaction, perform a similar operation as require.

Attention:

Requires_new is the start of a new, "internal" transaction that does not depend on the environment.

This transaction will be completely commited or rollback without relying on external things, it has its own isolation range, its own locks and so on. When an internal transaction starts executing, the foreign transaction will be suspended, and the external thing will continue to execute when the internal transaction ends.

On the other hand, nested is the beginning of a "nested" transaction, which is a real child of a transaction that already exists. When a nested transaction starts executing, it gets a savepoint. If this transaction fails, we roll back to the secondary savepoint, the nested transaction is part of the external transaction, and only the nested transaction is committed after the external transaction ends. (a nested transaction cannot be committed, it must complete the commit action through the outer transaction , and the rollback of the outer transaction will also cause the internal transaction to be rolled back)

Thus, the biggest difference between requires_new and nested is:

Requires_new is completely a new transaction, while nested is a child of the external transaction, and if the external transaction commits, the nested transaction is also commit, and the same rule applies to rollback.

With an example, see https://www.cnblogs.com/softidea/p/5962612.html

The propagation mechanism of spring affairs

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.