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

    Since transactions need to achieve ACID, that is, atomicity, consistency, isolation, and durability, a certain mechanism must be used to ensure that, usually in a staged submission method.

 

    XA: XA protocol. Specifies the transaction manager and resource manager interfaces. A two-phase commit protocol is used.

One-Phase Commit Protocol

    The one-phase commit protocol is relatively simple. For example the following picture:

    

    Of course, the premise is that the transaction is opened, and then after the application makes a commit/rollback request, the database runs the operation, and then returns the success/failure to the application. The program continues to run.

    The one-phase commit protocol is relatively simple. The advantage of simplicity is that it no longer has to interact with other objects. It saves inference steps and time, so performance is better in the phase-commit protocol.

two-phase commit protocol

    The one-phase commit protocol has its advantages, but its disadvantages are also very obvious:

 

  • The database has taken a long time to confirm running transactions. The possibility of problems increases accordingly.
  • Assuming there are multiple data sources, the one-phase commit protocol cannot coordinate their relationship.

     

 

   So on the basis of a one-phase agreement. With the Phase 2 protocol, the advantage of the Phase 2 protocol is the addition of a manager role, such as the following:

    

    very obvious. The phase two protocol works by turning two layers into three. An intermediate manager role is added to coordinate the relationship between multiple data sources. The two-phase commit protocol is divided into two phases.

    The first stage

    

    The application calls the commit method of the transaction manager. The first stage is then divided into two steps:

 

  • The transaction manager notifies the various resource managers participating in the transaction. Notify them to start preparing the transaction.
  • After the resource manager receives the message, it starts the preparation phase, writes the transaction log and runs the transaction, but does not commit it, and then returns the message of readiness to the transaction manager (at this time, most of the transaction has been completed, and the future content takes very little time).

     

 

    second stage

    

    The second stage is also divided into two steps:    

 

  • After accepting each message, the transaction manager begins to analyze, assuming that any one of them fails. Send a rollback command, otherwise send a commit command.
  • After each resource manager receives the command, it runs (very little time-consuming). and return a commit message to the transaction manager.

     

    After the transaction manager accepts the message. Transaction is over. The application continues to run.
    Why run it in two steps? One is due to two steps. There is an opportunity for unified management by the transaction manager; 2. Commit the transaction as late as possible, so that the transaction can complete all the work that can be completed as much as possible before committing. The final commit phase will be extremely short. Extremely short time-consuming means less chance of an operation failing.
    at the same time. The two-phase commit protocol ensures transaction consistency, whether it is the transaction manager or each resource manager. Each step is run. Logs will be recorded to prepare the basis for recovery after a problem occurs.
    The disadvantage of the two-phase commit protocol is that it is blocked. Since the transaction manager needs to collect the response messages of each resource manager, if one or more of them does not return a message, the transaction manager keeps waiting, and the application program is blocked, even May be permanently blocked.

Transactions and Agreements

    So what protocols are used for local transactions and distributed transactions? What I see in one of the RedBooks docs 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 acommit.……
Local transactions
    A local transaction is managed by the resource manager without the need for an 
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.……
    The general idea is: although the purpose of implementing the XA interface is to support the two-phase commit protocol, it also supports the one-phase commit protocol. Local transactions only support one-phase commit. Distributed transactions use two-phase commit by default. Suppose that the one-phase commit protocol must be used in distributed transactions. Then, if there is more than one data source, an exception will be thrown. It works correctly assuming there is only one data source.

Summarize

    The one-phase commit protocol and the two-phase commit protocol are just two of the more frequently used ones, and there are other protocols that you can study by yourself.
 
Reprinted from: http://www.cnblogs.com/blfshiye/p/5207901.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326539174&siteId=291194637