Principles and Examples of distributed transactions

Source: Internet
Author: User

Principles and Examples of distributed transactions

Abstract: in software development and database operations, it is often necessary to advance and retreat together, either to succeed together or to fail together.

Case study:A Transfers 3000 RMB to B. Update Account set Amount = Amount-3000 where name = 'A' update account set Amount = Amount + 3000 where name = 'B' Scenario:Assume that the execution of the 1st-line code is successful, and the 2nd-line code is not yet executed. The execution has not continued. Result:A's money is gone! B did not receive the money!

We recommend that you use distributed transactions to solve such problems.


SolutionAtomicity should be achieved: either all succeeded or all failed (rollback)
Transaction:A Transaction is characterized by atomicity, that is, being successful or failed ". There are still many ways to implement transactions. The most common method is to use SqlTransactionSqlTransaction related to links. Advantages: 1. client and server configurations are not required; 2. Transaction Coordination Service (MSDTC) is not required. Disadvantages: it is difficult to implement distributed transactions, nested transactions, and write.
TransactionScope is used to implement Distributed transactions (cross-database and cross-machine operations). Step 1. Enable MSDTC (Distributed Transaction Coordinator) in the Windows Service and change the Startup Type to "automatic ". Note:: The same operation must be performed on the ADO. NET and database ends. 2. Example code for adding System. Transactions to the project:
// Pack the two together. Successful together, or failed together using (TransactionScope ts = new TransactionScope () {"update Account set Amount = Amount-3000 where name = 'A '"; // operation for fee deduction from account A "update account set Amount = Amount + 3000 where name = 'B'" // operation for adding money to account B ts. complete (); // if you forget this sentence, both of them failed to be inserted}




How to Implement Distributed Transaction Management

1. implement Distributed Transaction Management JTA (Java Transaction Architecture). Generally, JTA is supported by J2EE containers (WAS, Weblogic ...), JTA's 3rd-party jar.
2. distributed transactions are transactions between multiple databases. There is no Distributed Transaction in tomcat. However, they can be implemented using third-party software jotm (Java Open Transaction Manager) and AtomikosTransactionsEssentials, in spring, distributed transactions are implemented through jta (jotm, atomikos ).
3. Refer
Hi.baidu.com/..c.html

Distributed Transaction Management

Distributed refers to the sharing of information between two or more software, which can run on the same computer or on multiple computers on the network. Advantages: rare resources can be shared. distributed computing can balance computing load on multiple computers. programs can be prevented on computers that are most suitable for running them. Sharing rare resources and balancing loads are core distributed ideas!

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.