Spring Transaction propagation behavior (12)

Source: Internet
Author: User
Tags savepoint

Transactional propagation behavior

Specifies that a transaction method in spring calls another transaction method. The behavior of the processing

How to use:
@Transactional (propagation=propagation.required)

In the process of using the transaction, the most propagating behavior is require, in most MIS system, the whole business layer can be cut to a require transaction to meet the needs. But spring provides more than that, for complex business, spring also provides the corresponding transaction propagation behavior to meet the business needs.

The propagation behavior in spring is as follows:

Require: Support the current transaction, if there is no transaction, build a new one, which is the most common;

Supports: Supports the current transaction and, if there is no transaction, executes it in a non-transactional manner;

Mandatory: Supports the current transaction and throws an exception if there is no current transaction;

RequiresNew: Creates a new transaction, suspends the current transaction if a transaction is currently present;

NotSupported: Executes the operation in a non-transactional manner and suspends the transaction if there is a current transaction;

Never: Executes in a non-transactional manner and throws an exception if a transaction is currently present.

Nested: Creates a new transaction and suspends the current transaction if a transaction is currently present. The difference from requirenew is related to the parent transaction, and there is a savepoint.

Among them, Require, Supports, notsupported, never two see the text also can understand, not much said. And mandatory is to require that all operations must be in one transaction, and more stringent on the transaction requirements than require.

Requirenew: When a require method A calls Requirenew method B, the B method will be new to a transaction, and the transaction is not related to a transaction, that is, the B method has an exception, does not cause a rollback, and B has been committed, a and then an exception, B is not going to roll back.

Nested: The difference between this and requirenew is that the transaction of the B method and the transaction of a method are relevant. The B transaction is committed only when a transaction is committed. That is, when an exception occurs, A and B transactions are rolled back, and when B has an exception, B rolls back, and A rolls back to SavePoint, as shown in the following code:

1PublicvoidA () {2//Action 13//Action 24//Action 35Try{6//savepoint 7 B (); // a nested method  8} catch{ 9 / / An exception occurred, the B method rolled back, a method rolled back to 10 //< Span style= "COLOR: #008000" >savepoint, which means that operations 1, 2, 3 are still 11  C (); 12} finally{13 14 }15" Span style= "COLOR: #008080" >16}            

Spring Transaction propagation behavior (12)

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.