`
gaojingsong
  • 浏览: 1197377 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

【Mysql数据库之XA】

阅读更多

Support for XA transactions is available for the InnoDB storage engine. The MySQL XA implementation is based on the X/Open CAE document Distributed Transaction Processing: The XA Specification.

XA 事务的基础是两阶段提交协议。XA–eXtended Architecture 在事务中意为分布式事务 

XA由协调者(coordinator,一般为transaction manager)和参与者(participants,一般在各个资源上有各自的resource manager)共同完成。在MySQL中,XA事务有两种。

 

 

On the client side, there are no special requirements. The XA interface to a MySQL server consists of SQL statements that begin with the XA keyword. MySQL client programs must be able to send SQL statements and to understand the semantics of the XA statement interface. They do not need be linked against a recent client library. Older client libraries also will work.

 

Among the MySQL Connectors, MySQL Connector/J 5.0.0 and higher supports XA directly, by means of a class interface that handles the XA SQL statement interface for you.

 

XA supports distributed transactions, that is, the ability to permit multiple separate transactional resources to participate in a global transaction. Transactional resources often are RDBMSs but may be other kinds of resources.

 

 

A global transaction involves several actions that are transactional in themselves, but that all must either complete successfully as a group, or all be rolled back as a group. In essence, this extends ACID properties “up a level” so that multiple ACID transactions can be executed in concert as components of a global operation that also has ACID properties. (As with nondistributed transactions, SERIALIZABLE may be preferred if your applications are sensitive to read phenomena. REPEATABLE READ may not be sufficient for distributed transactions.)

 

 

To carry out a global transaction, it is necessary to know which components are involved, and bring each component to a point when it can be committed or rolled back. Depending on what each component reports about its ability to succeed, they must all commit or roll back as an atomic group. That is, either all components must commit, or all components must roll back. To manage a global transaction, it is necessary to take into account that any component or the connecting network might fail.

 

XA一般由两阶段完成,称为two-phase commit(2PC)。 

The process for executing a global transaction uses two-phase commit (2PC). This takes place after the actions performed by the branches of the global transaction have been executed.

In the first phase, all branches are prepared. That is, they are told by the TM to get ready to commit. Typically, this means each RM that manages a branch records the actions for the branch in stable storage. The branches indicate whether they are able to do this, and these results are used for the second phase.

In the second phase, the TM tells the RMs whether to commit or roll back. If all branches indicated when they were prepared that they will be able to commit, all branches are told to commit. If any branch indicated when it was prepared that it will not be able to commit, all branches are told to roll back.

阶段一为准备阶段,即所有的参与者准备执行事务并锁住需要的资源。参与者ready时,向transaction manager汇报自己已经准备好。 

阶段二为提交阶段。当transaction manager确认所有参与者都ready后,向所有参与者发送commit命令。 

 

In some cases, a global transaction might use one-phase commit (1PC). For example, when a Transaction Manager finds that a global transaction consists of only one transactional resource (that is, a single branch), that resource can be told to prepare and commit at the same time.

 

 

XA事务状态进展过程

1. 使用XA START 启动一个XA事务,并把它置为ACTIVE状态。

2. 对一个ACTIVE XA事务,发布构成事务的SQL语句,然后发布一个XA END 语句,XA END 把事务置为IDLE状态。

3. 对一个IDLE XA 事务, 发布一个XA PREPARE语句或者一个XA COMMIT ... ONE PHASE语句: 前者把事务置为PREPARE状态,此时XA RECOVER 语句的输出包含事务的xid值(XA RECOVER 语句会列出所有处于PREPARE状态的XA事务); 后者用于预备和提交事务,不会被XA RECOVER列出,因为事务已经终止。

4. 对一个PREPARE XA 事务,可以发布一个XA COMMIT语句来提交和终止事务,或者发布一个XA ROLLBACK 来回滚并终止事务。

 

 

If you have a failed XA Transaction, it will show as "ACTIVE (PREPARED)" with process no 0 and thread id 0:

 

mysql> show engine innodb status\G

....

---TRANSACTION 0 1192549934, ACTIVE (PREPARED) 791 sec, process no 0, OS thread id 0

1 lock struct(s), heap size 368, undo log entries 3

 

To rollback the transaction, first get its xid:

 

mysql> xa recover;

+----------+--------------+--------------+------------------------------------------------------------+

| formatID | gtrid_length | bqual_length | data                                                       |

+----------+--------------+--------------+------------------------------------------------------------+

|   131075 |           30 |           28 | 1-a00640d:c09d:4ac454ef:b284c0a00640d:c09d:4ac454ef:b284c2 |

+----------+--------------+--------------+------------------------------------------------------------+

1 row in set (2.13 sec)

 

The xid is present in this output, but you have to perform a little string manipulation to get it. The format of a xid is: gtrid,bqual,formatID. The column 'data' contains a concatenation of 'gtrid' and 'bqual'. The columns 'gtrid_length' and 'bqual_length' specify how many bytes each of these values uses; use them to split apart 'data'. In this example, the result is:

 

mysql> xa rollback '1-a00640d:c09d:4ac454ef:b284c0','a00640d:c09d:4ac454ef:b284c2',131075;

 

ERROR 1402 (XA100): XA_RBROLLBACK: Transaction branch was rolled back

 

 

Mysql 的XA事务分为内部XA和外部XA。 外部XA可以参与到外部的分布式事务中,需要应用层介入作为协调者;内部XA事务用于同一实例下跨多引擎事务,由Binlog作为协调者,比如在一个存储引擎提交时,需要将提交信息写入二进制日志,这就是一个分布式内部XA事务,只不过二进制日志的参与者是MySQL本身。 Mysql 在XA事务中扮演的是一个参与者的角色,而不是协调者。

MySQL本身的插件式架构导致在其内部需要使用XA事务,此时MySQL即是协调者,也是参与者。例如,不同的存储引擎之间是完全独立的,因此当一个事务涉及两个不同的存储引擎时,就必须使用内部XA事务。需要特别注意的是,如果将二进制日志看做一个独立的“存储引擎”,就不难理解为什么即使是一个存储引擎参与的事务也需要使用XA事务了。在向存储引擎提交数据时,同时需要将提交的信息写入二进制日志,这就是一个分布式事务。

 

XA的性能问题 

XA的性能很低。一个数据库的事务和多个数据库间的XA事务性能对比可发现,性能差10倍左右。因此要尽量避免XA事务,例如可以将数据写入本地,用高性能的消息系统分发数据。或使用数据库复制等技术。 

只有在这些都无法实现,且性能不是瓶颈时才应该使用XA。

 

C:\Documents and Settings\Administrator>mysql -uroot -p

Enter password: ****

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 3

Server version: 5.5.47 MySQL Community Server (GPL)

 

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

 

mysql> help xa;

Name: 'XA'

Description:

Syntax:

XA {START|BEGIN} xid [JOIN|RESUME]

 

XA END xid [SUSPEND [FOR MIGRATE]]

 

XA PREPARE xid

 

XA COMMIT xid [ONE PHASE]

 

XA ROLLBACK xid

 

XA RECOVER

 

For XA START, the JOIN and RESUME clauses are not supported.

 

For XA END the SUSPEND [FOR MIGRATE] clause is not supported.

 

Each XA statement begins with the XA keyword, and most of them require

an xid value. An xid is an XA transaction identifier. It indicates

which transaction the statement applies to. xid values are supplied by

the client, or generated by the MySQL server. An xid value has from one

to three parts:

 

xid: gtrid [, bqual [, formatID ]]

 

gtrid is a global transaction identifier, bqual is a branch qualifier,

and formatID is a number that identifies the format used by the gtrid

and bqual values. As indicated by the syntax, bqual and formatID are

optional. The default bqual value is '' if not given. The default

formatID value is 1 if not given.

 

gtrid and bqual must be string literals, each up to 64 bytes (not

characters) long. gtrid and bqual can be specified in several ways. You

can use a quoted string ('ab'), hex string (0x6162, X'ab'), or bit

value (b'nnnn').

 

formatID is an unsigned integer.

 

The gtrid and bqual values are interpreted in bytes by the MySQL

server's underlying XA support routines. However, while an SQL

statement containing an XA statement is being parsed, the server works

with some specific character set. To be safe, write gtrid and bqual as

hex strings.

 

xid values typically are generated by the Transaction Manager. Values

generated by one TM must be different from values generated by other

TMs. A given TM must be able to recognize its own xid values in a list

of values returned by the XA RECOVER statement.

 

For XA START xid starts an XA transaction with the given xid value.

Each XA transaction must have a unique xid value, so the value must not

currently be used by another XA transaction. Uniqueness is assessed

using the gtrid and bqual values. All following XA statements for the

XA transaction must be specified using the same xid value as that given

in the XA START statement. If you use any of those statements but

specify an xid value that does not correspond to some existing XA

transaction, an error occurs.

 

One or more XA transactions can be part of the same global transaction.

All XA transactions within a given global transaction must use the same

gtrid value in the xid value. For this reason, gtrid values must be

globally unique so that there is no ambiguity about which global

transaction a given XA transaction is part of. The bqual part of the

xid value must be different for each XA transaction within a global

transaction. (The requirement that bqual values be different is a

limitation of the current MySQL XA implementation. It is not part of

the XA specification.)

 

 

XA RECOVER 列出所有处于PREPARE状态的XA事务

The XA RECOVER statement returns information for those XA transactions

on the MySQL server that are in the PREPARED state. (See

http://dev.mysql.com/doc/refman/5.5/en/xa-states.html.) The output

includes a row for each such XA transaction on the server, regardless

of which client started it.

 

XA RECOVER output rows look like this (for an example xid value

consisting of the parts 'abc', 'def', and 7):

 

mysql> XA RECOVER

+----------+--------------+--------------+--------+

| formatID | gtrid_length | bqual_length | data   |

+----------+--------------+--------------+--------+

|        7 |            3 |            3 | abcdef |

+----------+--------------+--------------+--------+

 

The output columns have the following meanings:

 

o formatID is the formatID part of the transaction xid

 

o gtrid_length is the length in bytes of the gtrid part of the xid

 

o bqual_length is the length in bytes of the bqual part of the xid

 

o data is the concatenation of the gtrid and bqual parts of the xid

1. formatID 是事务xid的formatID部分。

2. gtrid_length 是xid的gtrid部分的长度,以字节为单位。

3. bqual_length 是xid的bqual部分的长度,以字节为单位。

4. data 是xid的gtrid部分和bqual部分的串联。

URL: http://dev.mysql.com/doc/refman/5.5/en/xa-statements.html

mysql>

0
0
分享到:
评论

相关推荐

    MySQL 外部XA及其在分布式事务中的应用分析

    标题《MySQL 外部XA及其在分布式事务中的应用分析》指出了文章的核心议题,即分析MySQL数据库管理系统中外部XA事务的实现机制以及它在分布式事务环境中的应用。本文将深入讨论与理解分布式事务处理原理,MySQL XA...

    mysql分布式事务实现 MySQL XA pdf

    本文将深入探讨MySQL中的分布式事务处理机制,特别是MySQL如何通过XA协议来实现跨数据库的一致性。 #### 二、什么是分布式事务处理(DTP) 分布式事务处理(Distributed Transaction Processing, DTP)是一种确保在多...

    mysql数据库函数大全

    mysql数据库函数大全 mysql数据库函数大全提供了多种字符串操作函数,以下是对标题、描述、标签和部分内容的详细解释: ASCII函数 ASCII(str) 返回字符串str的最左面字符的ASCII代码值。如果str是空字符串,返回0...

    mysql-connector-java-8.0.29.zip_数据库

    MySQL Connector/J是MySQL数据库系统与Java应用程序之间的桥梁,它是一个实现了Java Database Connectivity (JDBC) API的驱动程序,使得Java开发者能够方便地在Java应用程序中访问MySQL数据库。MySQL-connector-java...

    2024 中国MySQL生态年会(11份PPT下载).zip

    6、Making MySQL XA transactionprocessing crash safe 7、面向云原生架构的数据库引擎 8、云计算成本优化-大数据降本增效实战 9、瑞幸咖啡数据库平台与架构实践 10、MySQL HeatWave Lakehouse 11、分布式数据库必须...

    掌握分布式事务的艺术:深入MySQL XA事务处理

    ### 掌握分布式事务的艺术:深入 MySQL XA 事务处理 #### 1. 分布式事务与 XA 事务概述 ##### 1.1 分布式事务定义 分布式事务是指那些跨越多个数据库实例或者服务的事务。这类事务的管理比单一数据库上的事务更加...

    mysql-connector-j-8.4.0.jar.rar

    MySQL Connector/J 8.4.0 是MySQL数据库与Java应用程序之间的桥梁,它是MySQL官方提供的用于Java平台的JDBC驱动程序。这个压缩包“mysql-connector-j-8.4.0.jar.rar”包含了这个驱动的最新版本,使得Java开发者能够...

    mysql-connector-java-5.1.18.jar

    MySQL Connector/J 5.1.18 是MySQL数据库管理系统用于Java应用程序的官方驱动程序,它实现了JDBC(Java Database Connectivity)接口,使得Java开发者能够方便地在Java程序中连接和操作MySQL数据库。这个版本的驱动...

    mysql-connector-java-5.1.38.jar

    MySQL Connector/J是MySQL数据库与Java应用程序之间的桥梁,它是一个实现了JDBC(Java Database Connectivity)标准的驱动程序,允许Java开发者在Java应用中访问和操作MySQL数据库。`mysql-connector-java-5.1.38....

    解决Mysql数据库插入数据出现问号(?)的解决办法

    首先,我用的mysql数据库是5.7.12版本。 出现的问题: 1.插入数据显示错误,插入不成功,出现:Incorrect string value: ‘\xCD\xF5\xD5\xBC\xBE\xA9’ for column ‘Sname’ at row 1 2.插入中文,虽然插入成功,...

    Mysql事务控制(XA分布式事务)和锁定语句

    在分布式系统中,MySQL支持通过X/Open XA协议实现的分布式事务,这允许跨越多个资源管理器(如不同的数据库实例)的事务一致性。分布式事务的处理分为两个主要阶段: 1. **预提交阶段(Prepare Phase)**:在这一...

    mysql连接驱动包

    MySQL连接驱动包是Java应用程序与MySQL数据库进行交互的关键组件,它允许Java开发者通过JDBC(Java Database Connectivity)接口执行SQL语句,管理数据库事务,以及处理数据库结果集。本压缩包包含了两个不同版本的...

    mysql-connector-java-8.0.19_Java8_MYSQL_源码

    MySQL Connector/J 8.0.19 是MySQL数据库与Java应用程序之间的重要桥梁,它是一个用于连接Java应用程序到MySQL服务器的JDBC驱动程序。这个压缩包包含的“mysql-connector-java-8.0.19.jar”是这个驱动的实现文件,...

    mysql-connector-java-5.1.41.jar

    《MySQL Connector/J 5.1.41:Java与MySQL数据库的桥梁》 MySQL Connector/J是MySQL官方提供的用于连接Java应用程序和MySQL数据库的驱动程序,它遵循Java Database Connectivity (JDBC) API规范,使得Java开发者...

    mysql-connector-java-5.1.37.zip

    MySQL Connector/J是MySQL数据库与Java应用程序之间的重要桥梁,它是一个实现了Java Database Connectivity (JDBC) API的驱动程序,使得Java开发者能够方便地在他们的应用中连接到MySQL数据库。"mysql-connector-...

    Mysql 8.0资源安装包下载

    MySQL 8.0是当前MySQL数据库管理系统的主要版本,它提供了许多增强的功能和性能优化,使得这个开源关系型数据库系统在各种规模的企业中被广泛应用。在这个"Mysql 8.0资源安装包下载"中,我们主要关注的是MySQL的Java...

    mysql-connector-java-5.1.48.zip

    MySQL Connector/J是MySQL数据库系统与Java应用程序之间的重要桥梁,它是一个实现了JDBC(Java Database Connectivity)规范的驱动程序,使得Java开发者能够通过编写Java代码来访问和操作MySQL数据库。"mysql-...

    mysql-connector-java-6.0.2

    MySQL Connector/J 6.0.2 是MySQL数据库与Java应用程序之间的关键桥梁,它是一个用于连接Java应用程序到MySQL服务器的驱动程序。这个版本是MySQL官方提供的,确保了与MySQL数据库的兼容性和稳定性。在Java开发中,它...

    mysql-connector-java-5.1.27.rar

    MySQL Connector/J是MySQL数据库与Java应用程序之间的重要桥梁,它是一个实现了Java Database Connectivity (JDBC) API的驱动程序,使得Java开发者能够通过编写Java代码来访问和操作MySQL数据库。在本例中,"mysql-...

Global site tag (gtag.js) - Google Analytics