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
Detailed explanation of Linux vi command The vi e...
Docker supports running on the following CentOS v...
A few months after entering the industry in 2005, ...
This article introduces an example of using HTML+...
For example, when you create a new table or updat...
I often see some circular wave graphics on mobile...
Web forms are the primary communication channel b...
Preview knowledge points. Animation Frames Backgr...
Today I was asked what the zoom attribute in CSS ...
Installation of MySQL decompression version and N...
Recently, I participated in the development of th...
【Problem Analysis】 We can use the chown command. ...
Table of contents Preface Six features of JSON.st...
Microsoft IIS IIS (Internet Information Server) i...
Since the network requests initiated by native js...