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
Which parameter does the rpm command use to insta...
1. Write Shell script crontab.sh #!/bin/bash step...
This article uses examples to explain the princip...
1. Check whether event is enabled show variables ...
1. Syntax TIMESTAMPDIFF(unit,begin,end); Returns ...
Table of contents 1. MySQL data backup 1.1, mysql...
Because I have a database tutorial based on SQL S...
Table of contents What is a Mapping Difference be...
Table of contents 1. Optional chaining operator [...
I've been learning Docker recently, and I oft...
1. HTML Overview htyper text markup language Hype...
A problem that front-end developers often encount...
Written in front Environment: MySQL 5.7+, MySQL d...
Since this is my first post, if there are any mis...
When you write buttons (input, button), you will f...