Example of spring's transaction propagation mechanism (iii)

Source: Internet
Author: User
Tags throwable

In this final article, we discuss the three most commonly used transaction propagation mechanisms in development:REQUIRED,requires_new, and NESTED

5. REQUIRED

Required is our most commonly used communication mechanism. If there is a transaction that currently exists, the transaction is joined, and if not, a new transaction is opened.

Modify the configuration file first:

<tx:attributes> <tx:method name= "insertsupertable" propagation= "REQUIRED"/> <tx:method name= "Inser Tsubtable "propagation=" REQUIRED "/> </tx:attributes>

Parent Transaction class:

@Componentpublic  class TransactionSuper {     @Autowired      TransactionSub transactionSub;    String insertSuperTable1 =  " insert into super_table values  (1,  ' super1 ') ";    string  insertsupertable2 =  "insert into super_table values  (2,  ' super2 ')";         public void insertsupertable (AbstractApplicationContext &NBSP;CTX)  throws exception{    system.out.println ("========insertSuperTable  start======== ");    jdbctemplate jt =  (JdbcTemplate) Ctx.getBean (" JdbcTemplate ");     jt.execute (InsertSuperTable1);     Transactionsub.insertsubtable (CTX);     jt.execute (InsertSuperTable2);     system.out.println ("========insertsupertable end======== ");     }} 

Child transaction class:

@Componentpublic  class transactionsub {    string insertsubtable1  =  "insert into sub_table values  (1,  ' sub1 ')";     string insertsubtable2 =  "insert into sub_table values  (2,  ' sub2 ')";     public void insertsubtable (ABSTRACTAPPLICATIONCONTEXT&NBSP;CTX)  throws  exception {        system.out.println ("======== insertsubtable start======== ");        jdbctemplate jt  =  (JdbcTemplate)  ctx.getbean ("JdbcTemplate");         Jt.execute (InsertSubTable1);         jt.execute (InsertSubTable2);         system.out.println ("========insertsubtable end========");     }}

Then run the test method

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7E/C8/wKiom1cIaC3AlOl9AAEunIP7Uao075.png "style=" float: none; "title=" 1.PNG "alt=" Wkiom1ciac3alol9aaeunip7uao075.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7E/C4/wKioL1cIaN2AXPrRAAAJBEkhP1w981.png "style=" float: none; "title=" 2.PNG "alt=" Wkiol1cian2axprraaajbekhp1w981.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7E/C8/wKiom1cIaC7C3WqWAAAIUXKTxKs213.png "style=" float: none; "title=" 3.PNG "alt=" Wkiom1ciac7c3wqwaaaiuxktxks213.png "/>

If there is a transaction that exists, as with the mandatory effect, it joins the transaction and shares a connection, so the parent-child method modifies the database to be mutually visible. There is no business situation here is no longer demonstrated, that is, the Insertsubtable method to open a new business.

Let's take a look at the rollback when the exception occurred, in fact, just like mandatory, only the sub-method can open their own transactions, and not necessarily to join other transactions .

①runtimeexception

If there are existing transactions, like mandatory, theREQUIRED annotation method is to join the parent transaction directly as part of the parent transaction, and they share a connection. So both the child transaction and the parent transaction are rolled back when the runtimeexception is thrown. If there is no transaction, only the action in the newly opened transaction of the child method is rolled back.

②throwable and exception

like mandatory , if the Rollback-for property is not configured, throwing throwable and exception does not cause a parent-child transaction to be rolled back, but rather where the exception is committed, There is a possibility that some of the submissions will occur.

We can use the Rollback-for property to let the Throwable and exception be rolled back as well . Because the REQUIRED annotation method is to join the parent transaction directly, the child class occurs throwable or exception, and the parent and child are rolled back together.



6. Requires_new

the method marked by Requires_new creates a new transaction, suspends the current transaction if a transaction is currently present, waits for the new transaction to complete, and resumes execution of the pending transaction.

Modify the configuration file first:

<tx:attributes> <tx:method name= "insertsupertable" propagation= "REQUIRED"/> <tx:method name= "Inser Tsubtable "propagation=" requires_new "/> </tx:attributes>

Execute the test method directly:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7E/C5/wKioL1cIgQjzofMuAAHKjpdVTh8549.png "style=" float: none; "title=" 1.PNG "alt=" Wkiol1cigqjzofmuaahkjpdvth8549.png "/>

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7E/C9/wKiom1cIgFjBKyHiAAAITNcJ9Lk342.png "style=" float: none; "title=" 2.PNG "alt=" Wkiom1cigfjbkyhiaaaitncj9lk342.png "/>

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7E/C9/wKiom1cIgJLgSefAAAAIAPFXkYA552.png "title=" 3.PNG " alt= "Wkiom1cigjlgsefaaaaiapfxkya552.png"/>

From the above can be clearly seen insertsubtable method opened a new connection and restarted a new transaction, insertsupertable and insertsubtable are two different connection in two different transactions. So the parent method's modifications to the database are visible to the child method, depending on the transaction isolation level of the database.

Requires_new because of the new connection and the new transaction, the exception rollback is very different from other transaction propagation mechanisms, which we will illustrate with a concrete example.

①runtimeexception

This article is from the "Bronze Gong" blog, please be sure to keep this source http://jaeger.blog.51cto.com/11064196/1762039

Instance details spring's transaction propagation mechanism (iii)

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.