One-phase commit protocol and two-phase commit protocol for transactions

Source: Internet
Author: User

One-phase commit protocol and two-phase commit protocol for transactions

Because transactions need to implement ACID, that is, atomicity, consistency, isolation, and durability, a certain mechanism is required to ensure that transactions are committed in stages.

XA: XA protocol, which specifies the Transaction Manager and resource manager interfaces and adopts the two-phase commit protocol.

Phase-1 Submission Protocol

The phase-1 Submission protocol is relatively simple, for example:

Of course, the premise is that the transaction is enabled, and after the application sends a commit/rollback request, the database executes the operation, and then returns the success/failure to the application, and the program continues to execute.

The one-stage commit protocol is relatively simple. The advantage of simple introduction is that it does not need to interact with other objects, saving judgment steps and time. Therefore, it is good in terms of performance in the phase commit protocol. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPGgxPrb + release/Uo7o8L3A + release/4si3yM/release + 1 oaMKPHA + release + bShyc + jrNPQwcu2/release "http://www.2cto.com/uploadfile/Collfiles/20140905/20140905091214279.png" alt = "\">

Obviously, the two-phase protocol changes the two layers to three layers, and adds the intermediate manager role to coordinate the relationship between multiple data sources. The two-phase commit protocol is divided into two phases.

Stage 1


The application calls the commit method of the Transaction Manager. The first stage is divided into two steps:

The transaction manager notifies the resource managers involved in the transaction to start preparing the transaction. After receiving the message, the resource manager starts the preparation phase, writes the transaction log and executes the transaction, but does not commit the transaction, then, return the ready message to the Transaction Manager (most of the transaction has been completed, and the subsequent content will take a very small amount of time ).

Stage 2


The second stage is divided into two steps:

After accepting messages, the transaction manager starts analysis. If any of the messages fails, the transaction manager sends the rollback command. Otherwise, the transaction manager sends the submit command. After each resource manager receives the command, it executes the command (which takes little time) and returns the submitted message to the Transaction Manager. After the Transaction Manager accepts the message, the transaction ends and the application continues to run. Why do we need to do this in two steps? First, the transaction manager has the opportunity to manage the transaction in two steps. Second, commit the transaction as late as possible so that the transaction can complete all the work that can be done before submission, the final submission phase will be time-consuming and extremely short, which means the possibility of operation failure will be reduced.
At the same time, in order to ensure transaction consistency, the two-phase commit protocol records logs for each operation, whether it is the Transaction Manager or each resource manager, and provides a basis for recovery after a fault occurs. The disadvantage of the two-phase commit protocol is blocking, because the transaction manager needs to collect the response messages of each resource manager. If one or more of them never return messages, the transaction manager waits, applications are also blocked, or even permanently blocked. What protocols are used for local transactions and distributed transactions respectively? What I see in a RedBooks document is: Global transactions
Although the XAResource interface is intended to support two phase commit, the specification does not force an adapter to support two phase commit. however, if the resource adapter does implement XAResource it must also implement support for one phase commit. this allows the transaction manager to do one phase commit optimization (explained later) by setting the onePhase flag to true when doing aco Mmit .......
Local transactions
A local transaction is managed by the resource manager without the need for
External transaction manager, and can be utilized when only one resource is
Involved. Local transactions only support one phase commit, Because they only
Reference one EIS .......
Although the objective of implementing the XA interface is to support the two-stage commit protocol, it also supports the one-stage commit protocol. Local transactions only support one-stage commit. distributed transactions use two-stage commit by default. If a one-stage commit protocol is required in a distributed transaction, an exception is thrown as long as the data source is redundant, if there is only one data source, it is executed correctly. To sum up, one-stage submission protocol and two-stage submission protocol are commonly used. In addition, there are other protocols that can be researched by yourself.





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.