Spring Cloud/boot uses transactions and multiple data sources

Source: Internet
Author: User

first, the four characteristics of business

test interview to be used, atomic, consistent, isolated and lasting

1. Atomic

try{
DoA ()
DoB ()
DoC ()
...
}catch (anyexception) {
Undoa (), Undob (), Undoc ()
}

2. Consistent

consistency involves the transaction, defined as the end of the transaction, all the data, the index should be consistent.

Personal Understanding: That is, after the atomic operation, all the people get the data, the index should be consistent.

3. Isolation

Ensure that different transactions are carried out independently and transparently with each other.
Cascade Scrap: If the transaction T1 read the transaction T2 uncommitted data, if T2
Failure, then T1 to roll back.

Isolation level:
READ UNCOMMITTED (not submitted)
Read Committed (has been submitted)
REPEATABLE READ (repeatable) (MySQL default isolation level)
Serializable (serialized)



Dirty read, non-repeatable read, Phantom read

Dirty reads: One transaction changed the data, but it was not committed, it was possible to rollback, and another transaction read the uncommitted data.

Non-repeatable reads: One transaction updates or deletes the data, and the other transaction two times queries the data inconsistently.

Phantom reads: One transaction adds a new operation to the data, and the other transaction two times queries the data is inconsistent.

Dirty reads: Primarily for column content changes.

Phantom reads: Mainly for the change of line number


second, distributed transactions (popular solution )

Refers to transactions that involve operating multiple databases

1. A database operation involving multiple systems, multiple data sources, and multiple connection.

2. How to ensure consistency.

Two-stage commit, three-phase commit, event model

the implementation of the specific scheme to be supplemented

third, spring transaction management @Transacational

1, Isolation (isolation):
Default: Using Database Settings
Read_uncommitted: Dirty reads, non-repeatable reads, and phantom reads occur
Read_committed: There will be no repeat reading, phantom reading problems
Repeatable_read: It's going to be a phantom reading.
SERIALIZABLE: Make sure everything doesn't happen.

2, propagation (communication):

Requeired
Supports
Requried_new
Not supported
Mandatory
Never
Nested

3, Code Analysis

public class txtest{

   @Transacational public
   void foo () {
       bar ();
   }

   @Transacational (propagation= "required_new") public
   Void Bar () {

   }
}

Calling the Foo () method creates only the transaction of the current method, in which bar () does not create a new transaction even if it is defined required_new.

public class txtest{
    @Autowird
    private TxTest2 txTest2;
   @Transacational public
   void foo () {
       txtest2.bar ();
   }
}

public class txtest2{
 @Transacational (propagation= "required_new") public
    Void Bar () {
  }
}
Calling the Foo () method creates a transaction for the current method, and a new transaction is created when it runs to Txtest2.bar ().







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.