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 Linux vi command knowledge points and usage

Detailed explanation of Linux vi command The vi e...

Detailed steps to install Docker 1.8 on CentOS 7

Docker supports running on the following CentOS v...

Discussion on Web Imitation and Plagiarism

A few months after entering the industry in 2005, ...

How to view mysql binlog (binary log)

For example, when you create a new table or updat...

Use CSS to achieve circular wave effect

I often see some circular wave graphics on mobile...

40+ Beautiful Web Form Design Examples

Web forms are the primary communication channel b...

CSS3 uses var() and calc() functions to achieve animation effects

Preview knowledge points. Animation Frames Backgr...

Definition and function of zoom:1 attribute in CSS

Today I was asked what the zoom attribute in CSS ...

WeChat applet development chapter: pitfall record

Recently, I participated in the development of th...

How to authorize all the contents of a folder to a certain user in Linux?

【Problem Analysis】 We can use the chown command. ...

Steps to configure IIS10 under Win10 and support debugging ASP programs

Microsoft IIS IIS (Internet Information Server) i...

Define your own ajax function using JavaScript

Since the network requests initiated by native js...