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.
Two-phase submission agreement
The one-phase commit protocol has its advantages, but its disadvantages are also obvious:
- When the Database confirms that the transaction is executed for a long time, the possibility of a problem increases.
- If multiple data sources exist, the one-phase commit protocol cannot coordinate their relationships.
Therefore, on the basis of the Phase-one protocol, a phase-two Protocol has the advantage of adding a manager role, as shown below:
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.
One-phase commit protocol and two-phase commit protocol for transactions