This article uses an example to describe the MySQL cross-database transaction XA operation. Share with you for your reference, the details are as follows: Some time ago, I encountered a cross-database transaction problem at work. Later, I searched on the Internet and now I will organize and summarize it. 1. First, make sure that MySQL enables XA transaction support SHOW VARIABLES LIKE '%XA%' If the value of innodb_support_xa is ON, it means that MySQL has enabled support for XA transactions. If not, execute: SET innodb_support_xa = ON <?PHP $dbtest1 = new mysqli("172.20.101.17","public","public","dbtest1")or die("dbtest1 connection failed"); $dbtest2 = new mysqli("172.20.101.18","public","public","dbtest2")or die("dbtest2 connection failed"); //Specify an id for the XA transaction. xid must be a unique value. $xid = uniqid(""); //The two databases specify the same transaction ID, indicating that the operations of the two databases are in the same transaction $dbtest1->query("XA START '$xid'"); //Prepare transaction 1 $dbtest2->query("XA START '$xid'"); //Prepare transaction 2 try { //$dbtest1 $return = $dbtest1->query("UPDATE member SET name='唐大麦' WHERE id=1"); if($return == false) { throw new Exception("The update member operation failed in the library [email protected]!"); } //$dbtest2 $return = $dbtest2->query("UPDATE memberpoints SET point=point+10 WHERE memberid=1"); if($return == false) { throw new Exception("The update memberpoints operation failed in the database [email protected]!"); } //Phase 1: $dbtest1 is ready to submit $dbtest1->query("XA END '$xid'"); $dbtest1->query("XA PREPARE '$xid'"); //Phase 1: $dbtest2 is ready to submit $dbtest2->query("XA END '$xid'"); $dbtest2->query("XA PREPARE '$xid'"); //Phase 2: Submit two databases $dbtest1->query("XA COMMIT '$xid'"); $dbtest2->query("XA COMMIT '$xid'"); } catch (Exception $e) { //Phase 2: Rollback $dbtest1->query("XA ROLLBACK '$xid'"); $dbtest2->query("XA ROLLBACK '$xid'"); die($e->getMessage()); } $dbtest1->close(); $dbtest2->close(); ?> XA has very low performance. Comparing the performance of transactions in one database with XA transactions between multiple databases, we can find that the performance is about 10 times worse. Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL transaction operation skills summary", "MySQL query skills collection", "MySQL stored procedure skills collection", "MySQL database lock related skills summary" and "MySQL common function summary" I hope this article will be helpful to everyone's MySQL database design. You may also be interested in:
|
>>: How to set background color and transparency in Vue
1. v-on event monitoring To listen to DOM events,...
1. Create a scheduling task instruction crontab -...
When making a form in a recent project, I need to...
Table of contents 1 Question 2 Methods 3 Experime...
This article describes how to use docker to deplo...
This article shares the data display code for Jav...
Go to https://dev.mysql.com/downloads/mysql/ to d...
Table of contents 1. Three modes of binlog 1.Stat...
I have previously introduced to you the configura...
1. Implementation principle of scrolling The scro...
Preface Sometimes, we need a floating effect requ...
The implementation principle of Vue2.0/3.0 two-wa...
Table of contents background question Problem ana...
Table of contents Configuration parsing Service C...
Table of contents 1. Use the a tag to preview or ...