Sometimes a system's data is placed in a different library. If a branch library commits successfully with a common thing, another branch library submission fails,
This is not a successful or failed synchronization of two libraries. Can result in incomplete system data.
MySQL has a syntax for dealing with distributed (XA) things in this case
The XA start XID is used to start a XA thing with a given XID.
XID consists of 3 parts Gtrid,bqual,formatid
- Gtrid is an identifier for a distributed thing, and the gtrid of each XA thing for a distributed thing must be the same, so that you know exactly which distributed thing each xa thing belongs to.
- Bqual is a branch qualifier, and the bqual value of each XA thing for a distributed thing must be different.
- FormatID is a number used to identify the format used by the Gtrid and bqual values, which can be written by default of 1.
The key to distribution is how to ensure the integrity of distributed things and troubleshoot problems when a branch is faulty.
The related command for XA is to provide the application with the management of distributed things between multiple independent databases, including initiating a branching thing, getting things into the preparation phase, and actually submitting rollback operations for things.
Session1 in DB1 |
Session2 in DB2 |
Xa start ' gtridtest ', 'bqualdb1' database launches a branch of a distributed thing in DB1 |
Xa start ' gtridtest ', 'bqualdb2' database launches a branch of a distributed thing in DB2 |
Update Db1_table1 Set content = ' Test ' where id = 1; |
Update Db2_table1 Set content = ' experiment ' WHERE id = 1; |
Xa end ' Gtrid _test ', ' bqual_db1 ' first phase commit |
Xa end ' Gtrid _test ', ' bqual_db2 ' first phase commit |
Xa prepare ' gtrid _test ', ' bqual_db1 ' into prepare state |
Xa prepare ' gtrid _test ', ' bqual_db2 ' into prepare state |
two things go into the ready to commit phase, and if any errors are encountered before any of the branches are rolled back, the xa recover command can be viewed in the prepare state of things ( I tested and did not rollback ....) ) |
Xa commit ' Gtrid _test ', ' bqual_db1 ' |
Xa commit ' Gtrid _test ', ' bqual_db2 ' |
once the commit operation is started, it is necessary to ensure that all branches are committed successfully. See the end of the article if it fails |
If a branch thing reaches the prepare state, it encounters an exception such as a power outage. After the server restarts (XA recover can check for XA in the incomplete prepare state), the branch can continue to be rolled back or committed;
In fact, distributed things are not perfect xa things prepare after encountering an exception will not automatically rollback, need to manually rollback or commit.
Javax.transaction.xa Package is the API for JDBC to control MySQL xa things
The use of MySQL distributed things (xa things)