Some misconceptions about spring business

Source: Internet
Author: User
Tags savepoint

the spring transaction reminds us of four basic features, five isolation levels and seven propagation characteristics. I believe most people know these things, but it is another thing to know that it is a matter of being able to use it well. I've had a few serious questions about using spring transactions, and I'm doing a self-summary here. Question one, propagation. What's the difference between nested and propagation.required_new?

When the caller does not have a transaction, the effect is consistent. So the premise of this discussion is that the caller has a transaction. Propagation_requires_new starts a new, "internal" transaction that does not depend on the environment. This transaction will be fully commited or rolled back without relying on external transactions, it has its own isolation range, its own locks, and so on. When an internal transaction starts executing, the external transaction will be suspended and the external transaction will continue to execute at the end of the housekeeping transaction.
On the other hand, propagation_nested begins a "nested" transaction, which is a real child of a transaction that already exists. When the dive set starts executing, it will get a savepoint. If this nested transaction fails, we will roll back to this savepoint. A latent transaction is a part of an external transaction that is committed only after the external transaction has ended.
Thus, the biggest difference between propagation_requires_new and propagation_nested is that propagation_requires_new is entirely a new business, and propagation_nested is a child of an external transaction, and if an external transaction commits, a nested transaction is also commit, and the same rule applies to roll back.

Question two, why does @Transactional fail?

1. Caller and callee belong to the same component, the callee's @Transacational annotations are invalid

  

 Packagecom.transacational;Importorg.springframework.stereotype.Component;Importorg.springframework.transaction.annotation.Transactional;/*** Created by Chenqimiao on 17/10/31.*/@Component Public classService { Public voidtest1 () {test2 (); } @Transactional//the annotations here are invalid     Public voidtest2 () {}}

  

2. Callee is not a public method, the callee's @Transacational annotation is invalid

  

  @Component  public  class   Service {@Resource  privat        E   Service1 Service1;  public  void          Test1 () {test2 ();    Service1.test3 (); @Transactional  // 1. The annotations here are invalid  public  void   Test2 ( ){    }}
 Package com.transacational; Import org.springframework.stereotype.Component; Import org.springframework.transaction.annotation.Transactional; /**  */@Componentpublicclass  Service1 {    @Transactional  //2. Invalid note    protectedvoid  test3 () {    }}

3. The transaction switch is not turned on, such as: In Springboot, the startup class is not used @EnableTransactionManagement

Question three, how to understand @transactional time-out?

Timeout is a property for developers to set the timeout period. The default value is 1, and the time-out is determined by the specific SQL system.

/*** Created by Chenqimiao on 17/10/31.*/@Component Public classService3 {@ResourcePrivateAdmininfodomapper Admininfodomapper; @Transactional (Timeout= 4)//does not time out     Public voidtest4 () {Admininfodomapper.selectnamebyid (1); Try{Thread.Sleep (5000); } Catch(interruptedexception e) {e.printstacktrace (); }    }}

Time-out specific definition: The transaction begins (before the first sentence of the method executes) to the last statement execution is complete

So if you write like this, the transaction will time out.

@Component  Public class Service3 {    @Resource    private  admininfodomapper admininfodomapper;     = 4)    publicvoid  test4 () {        try  {            Thread.Sleep (the);         Catch (interruptedexception e) {            e.printstacktrace ();        }        Admininfodomapper.selectnamebyid (1);}    }

Problem four, @Transactional the default rollback policy?

By default, a transaction is rolled back only if the exception for RuntimeException or its subclasses is caught by the transaction, and if you want the transaction to roll back all exceptions, you must manually specify the @Transactional (rollbackfor=exception. Class) so that inheriting exception subclasses or exception itself allows the transaction to be rolled back.

Some misconceptions about spring business

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.