Preface There is no doubt that MySQL supports single-machine transactions well. So in a distributed system involving multiple nodes, how does MySQL implement distributed transactions? For example, when developing a business system, it accepts external requests and then accesses multiple internal systems to execute the request. During execution, we need to update the values of multiple databases (D1, D2, D3) at the same time. Since the system must be consistent, the values of the three databases must either be updated successfully at the same time or not updated at all. Otherwise, some instructions of the subsystem will succeed, while some instructions will not be executed. This leads to confusion in understanding the results. So, how does MySQL achieve consistency in updating multiple MySQL databases? That's MySQL XA. MySQL implements the operation of multiple databases by supporting the two-phase commit protocol of the XA specification. XA Protocol When mentioning the XA specification, we have to talk about the DTP model (Distributed Transaction Processing). The XA specification stipulates the communication method between the two modules in the DTP model: the transaction manager and the resource manager. DTP is actually distributed transaction processing The functions of each module are as follows:
It may be difficult to understand at first. In summary, the architecture is that applications access and use shared resources provided by the resource manager and define transaction operations through the transaction interface (TX interface) provided by the transaction manager. The transaction manager and resource management will execute the two-phase commit protocol based on the XA specification.
How to implement distributed transactions with MySQL XAThere are two types of XA transactions in MySQL. One is the internal XA transaction, which is mainly used to coordinate the storage engine and binary log. The other is the external transaction, which can participate in external distributed transactions (such as distributed transactions implemented by multiple databases). Here we mainly discuss external transactions. Note: Distributed transactions can only be used in MySQL when the isolation level is set to Serializable. XA {START|BEGIN} xid [JOIN|RESUME] XA PREPARE xid XA END xid XA COMMIT xid[ONE PHASE] XA ROLLBACK xid XA RECOVER[CONVERT XID ] Among them, xid is the transaction ID, which uniquely represents a transaction branch. Each transaction branch has an id. Set the isolation level to serializable Execution Results First, call the "XA START 'xid'" command to put the XA transaction into the ACTIVATE state, and then execute the multiple SQL statements that make up the transaction (such as update Next, for an XA transaction in the IDLE state, you can execute an "XA PREPARE" command or an "XA COMMIT...ONE PHASE" command. Finally, call "XA COMMIT" to commit the transaction (or "XA ROLLBACK" to rollback the transaction). This achieves the consistency of global transactions. From the above process, we can see that in the MySQL database distributed transaction, the role of MySQL is actually the RM in the XA transaction process, and the TM is the client connected to the MySQL server. In a distributed transaction, at least two RMs are generally involved. So when we say that MySQL supports the XA protocol, we are referring to MySQL as RM, which means that MySQL implements the functions that RM should have in the XA protocol. This is the end of this article about understanding how MySQL XA implements distributed transactions. For more information about MySQL XA distributed transactions, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed explanation of JavaScript implementation of hash table
Preface It took two days to reconstruct a puzzle ...
<base target=_blank> changes the target fram...
Why is it that when the input tag type is file an...
The specific code is as follows: /*Scroll bar wid...
Use native JavaScript to simply implement the cou...
The effect to be achieved is: fixed zoom in twice...
The reason is that it was not uninstalled cleanly...
Compared with ordinary programs, dynamic link lib...
<a href="https://www.jb51.net/" titl...
This script can satisfy the operations of startin...
<br />Conditional comments are a feature uni...
To install a virtual machine on a Windows system,...
Preface In a recent project, we need to save a la...
1. Tomcat service is not open Enter localhost:808...
Table of contents 1. View the storage engine of t...