MySQL cross-database transaction XA operation example

MySQL cross-database transaction XA operation example

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:
  • Solution to the error message "java.sql.SQLException: Incorrect string value:'\xF0\x9F\x92\xA9\x0D\x0A...'" when storing emoticons in MySQL
  • How to change the mysql password on the Xampp server (with pictures)
  • How to use the terminal to open the MySQL that comes with XAMPP on Mac
  • How to modify the default password of MySQL in xampp
  • ThinkPHP framework construction and common problems (XAMPP installation failure, Apache/MySQL startup failure)
  • How to modify the default empty password (root password) of MySQL in xampp
  • Detailed explanation of MySQL's XA transaction recovery process
  • Solve the xampp self-start and mysql.sock problems
  • How does the MySQL database implement the XA specification?

<<:  Apache Flink arbitrary Jar package upload leads to remote code execution vulnerability recurrence problem (vulnerability warning)

>>:  How to set background color and transparency in Vue

Recommend

Summary of event handling in Vue.js front-end framework

1. v-on event monitoring To listen to DOM events,...

A brief analysis of crontab task scheduling in Linux

1. Create a scheduling task instruction crontab -...

Implementation of element input box automatically getting focus

When making a form in a recent project, I need to...

How to use javascript to do simple algorithms

Table of contents 1 Question 2 Methods 3 Experime...

CentOS 6 uses Docker to deploy redis master-slave database operation example

This article describes how to use docker to deplo...

JavaScript to implement search data display

This article shares the data display code for Jav...

How to choose the format when using binlog in MySQL

Table of contents 1. Three modes of binlog 1.Stat...

Analysis of Difficulties in Hot Standby of MySQL Database

I have previously introduced to you the configura...

Vue uses better-scroll to achieve horizontal scrolling method example

1. Implementation principle of scrolling The scro...

Docker container time zone error issue

Table of contents background question Problem ana...

Implementation of master-slave replication in docker compose deployment

Table of contents Configuration parsing Service C...

How to run the react project on WeChat official account

Table of contents 1. Use the a tag to preview or ...