Spring manages the transaction propagation mechanism of hibernate affairs

Source: Internet
Author: User

Spring manages the transaction propagation mechanism of hibernate affairs

Public enum Propagation {REQUIRED (transactiondefinition.propagation_required), SUPPORTS ( Transactiondefinition.propagation_supports), MANDATORY (transactiondefinition.propagation_mandatory), NOT_   Supported (transactiondefinition.propagation_not_supported), never (Transactiondefinition.propagation_never),   NESTED (transactiondefinition.propagation_nested);   private final int value;      Propagation (int value) {this.value = value;} public int value () {return this.value;}}

The above enumeration class is the defined spring transaction propagation mechanism.

1:propagation_required (the default transaction propagation mechanism)

Join the transaction that is currently being executed is not in another transaction, then a new transaction for example, Serviceb.methodb's transaction level is defined as propagation_required, So since the execution of Servicea.methoda, Servicea.methoda has already started the transaction, this time calls Serviceb.methodb, Serviceb.methodb sees that he is running inside a Servicea.methoda transaction, no new transactions are made. And if Servicea.methoda is running and finds himself not in a transaction, he assigns himself a transaction. in this way, the transaction will be rolled back if an exception occurs in the Servicea.methoda or anywhere within the SERVICEB.METHODB. Even if the SERVICEB.METHODB transaction has been committed, but Servicea.methoda will be rolled back in the next fail, SERVICEB.METHODB will roll back

If an exception occurs in Serviceb.methodb, the try catch in Servicea.methoda will still cause

Transaction rolled back because it had been marked as Rollback-only exception.


2:propagation_supports

If you are currently running in a transaction, that is, as a transaction, if you are not currently in a transaction, run as a non-transactional


3:propagation_mandatory

Must run in a transaction. In other words, he can only be called by a parent transaction. Otherwise, he's going to throw an exception.


4:propagation_requires_new

That's a bit of a detour. For example, we design Servicea.methoda with a transaction level of Propagation_required,serviceb.methodb of Propagation_requires_new, Then when the execution to the SERVICEB.METHODB, Servicea.methoda the transaction will be suspended, Serviceb.methodb will start a new transaction, waiting for the completion of the SERVICEB.METHODB transaction, he continues to execute. The difference between his affairs and Propagation_required is the degree of rollback of the transaction. Because Serviceb.methodb is a new transaction, there are two different transactions. If the SERVICEB.METHODB has been committed, then Servicea.methoda fails to rollback, SERVICEB.METHODB is not rolled back. If Serviceb.methodb fails to roll back, if the exception he throws is captured by Servicea.methoda, the Servicea.methoda transaction may still be committed.


5:propagation_not_supported

Transactions are not currently supported. For example, Servicea.methoda's transaction level is propagation_required, and Serviceb.methodb's transaction level is propagation_not_supported,

Then when execution to Serviceb.methodb, Servicea.methoda's transaction hangs, and he runs out of non-transactional state, and then continues the Servicea.methoda transaction.


6:propagation_never

Cannot run in a transaction. Suppose the transaction level of Servicea.methoda is propagation_required, and Serviceb.methodb's transaction level is Propagation_never,

Then Serviceb.methodb will throw an exception.


7:propagation_nested

The key to understanding nested is savepoint. The difference between him and propagation_requires_new is that propagation_requires_new another transaction, which will be independent of his father's affairs,

and nested's affairs are dependent on his father's affairs, and his submission is to be submitted as a piece of his father's business. In other words, if the parent transaction is finally rolled back, he will also be rolled back. The advantage of nested affairs is that he has a savepoint.

/** * Transaction property configured to Propagation_required */class servicea {public void MethodA () { try {//savepoint serviceb.methodb ();//propagation_nested level} catch (Someexception E        Xception) {//Perform other business, such as SERVICEC.METHODC (); }}}******************************************** that is SERVICEB.METHODB failure rollback, then Servicea.methoda will also roll back to SavePoint point, ServiceA.m Ethoda can choose another branch, such as SERVICEC.METHODC, to continue execution to try to complete its own transaction. However, this transaction is not defined in the EJB standard.

================================end================================

Spring manages the transaction propagation mechanism of hibernate 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.