Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
X/Open distributed transaction model implementation steps
The principle of local transaction is basically the same, the only difference is that if RM represents the database, TM can manage transactions of multiple databases, and the steps are as follows:
- Configure TM to register multiple RMS with TM, which is equivalent to TM registering RM as data source
- The AP gets the connection from RM managed by TM, or if RM is a database, it gets the JDBC connection
- The AP initiates a global transaction to TM, generating a global transaction ID(XID), which is notified to each RM
- The AP directly operates RM to complete the data operation by obtaining the connection in step 2. The AP passes the XID to RM in each operation
- The AP ends the global transaction, and TM notifies each RM that the global transaction ends
- Perform commit or rollback operations based on transaction execution results of each RM
In the case of single-node transactions, the visibility of cross-library transactions will be a problem, which makes it impossible to achieve global control of multi-node transactions. And TM is a global transaction manager, it can manage the affairs of multiple resource managers, TM according to the result of the execution of each branch affair will eventually commit or rollback, if all branches registered in the transaction any a node failed to perform the transaction, in order to ensure the consistency of the data, the TM trigger each RM transaction rollback operation.
Note: Transaction control between TM and multiple RMS is completed based on XA protocol, which is the distributed transaction processing specification proposed by X/Open and the standard of distributed transaction processing. Both Oracle and MySQL implement XA interfaces, so they can be used as RM
Two-phase commit protocol
TM implements multiple RM transaction management, involving two phases of commit, the first phase is the preparation phase of the transaction, and the second phase is the commit or rollback phase of the transaction, which is initiated by the transaction manager.