[Oracle] distributed transactions and two-phase commit (2 PC)

Source: Internet
Author: User

Distributed transactions are transactions that occur between multiple databases. Oracle uses the dblink Method for transaction processing. distributed transactions are much more complex than standalone transactions. Most relational databases use the two-Phase Commit (2 Phase Commit 2 PC) algorithm to complete distributed transactions. The following describes the two PC algorithm.

1. Composition of distributed transactions

Distributed transactions consist of the following parts:

  • Client: the node that calls other database information
  • Database: nodes that receive requests from other nodes
  • Global Coordinator (GC): the node that initiates distributed transactions
  • Local Coordinator (LC): A node that processes Local transactions and communicates with other nodes.
  • Commit Point Site (CPS): the node specified by Global Coordinator to submit or roll back the transaction first
In distributed transactions, the Commit Point Site is very important. It does not need to enter the 2 PC Prepared State, because it usually operates on the most critical data, so it does not appear in-doubt state. The Commit Point Site always takes precedence over the Commit of other database nodes to protect the most critical data. It determines whether the entire distributed transaction is committed or rolled back. In distributed transactions, other database nodes perform subsequent commit (or rollback) under the command of GC ). In Oracle, how does one select a Commit Point Site? It serves as the Commit Point Site based on the database with the largest commi _ point_strength parameter. 2. Two-phase commit (2 PC) The two-phase commit protocol ensures strong data consistency. Many Distributed Relational Data Management systems use this Protocol to complete distributed transactions. It is a distributed algorithm that coordinates all distributed atomic Transaction participants and decides to commit or cancel (rollback. It is also an algorithm for solving consistency problems. This algorithm can solve many temporary system faults (including processes, network nodes, communications, and other faults) and is widely used. However, it cannot solve all faults through configuration. In some cases, it still requires human intervention to solve the problem. As the name suggests, two-Phase submission is divided into the following two phases: 1) Prepare Phase (preparation node) 2) Commit Phase (submission Phase) 1) Prepare Phase in the request Phase, the Coordinator will notify the transaction participants to prepare to submit or cancel the transaction and then proceed to the voting process. During the voting, the participants will inform the coordinator of their decision: consent (successful execution of the local job of the transaction participant) or cancellation (local job execution failure ). To complete the quasi-preparation phase, other database nodes except the commit point site follow these steps:
  • Each node checks whether it is referenced by other nodes. If yes, it notifies these nodes to Prepare for submission (entering the Prepare stage ).
  • Each node checks its own transaction. if it finds that the local transaction has not modified the data (read-only), It skips the subsequent steps and returns a read only to the global coordinator.
  • If the transaction needs to modify the data, allocate the corresponding resources for the transaction to ensure the normal modification.
  • After all the preceding operations are successful, the prepared information is returned to the global coordinator. Otherwise, the information about failure is returned.
2) in this Phase of Commit Phase, the Coordinator will make a decision based on the voting results of the first Phase: Submit or cancel. If and only when all participants agree to submit the Transaction Coordinator notifies all participants to submit the transaction, otherwise the Coordinator will notify all participants to cancel the transaction. After receiving a message from the Coordinator, the participant performs a response operation. In the submission phase, follow these steps:
  • The Global Coordinator notifies commit point site for submission.
  • The commit point site is submitted, and the global coordinator is notified after submission.
  • The Global Coordinator notifies other nodes for submission.
  • Other nodes submit local transactions and release the locks and resources.
  • Other nodes notify the Global Coordinator that the submission is complete.
3) End Stage
  • The Global Coordinator notifies commit point site that all nodes have been submitted.
  • The commit point site Database releases all transaction-related resources and notifies the global coordinator.
  • The Global Coordinator releases its own resources.
  • Distributed Transaction ends
Generally, the two-phase commit mechanism works well. When a participant goes down during a transaction and restarts, you can ask other participants or coordinators, to know whether the transaction has been committed. Of course, the premise is that each participant writes logs in advance during each step of operation.

The only dilemma that cannot be solved by a two-phase commit is that when the Coordinator sends a commit message, the only participant who receives the command goes down, at this time, the transaction is in an unknown state. No one knows whether the transaction has been committed or not. This requires the intervention of the database administrator to prevent the database from entering an inconsistent state. Of course, if one premise is that all node or network exceptions will eventually be restored, the problem will not exist, and the coordinators and participants will eventually restart, other nodes will eventually receive the commit information. This is also in line with the CAP theory.

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.