XA 事务的支持可用于 InnoDB 存储引擎。MySQL 的 XA 实现基于 X/Open CAE 文档 分布式事务处理:XA 规范。该文档由 The Open Group 发布,可在 http://www.opengroup.org/public/pubs/catalog/c193.htm 获取。当前 XA 实现的限制描述见 第 15.3.8.3 节,“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. This document is published by The Open
Group and available at
http://www.opengroup.org/public/pubs/catalog/c193.htm.
Limitations of the current XA implementation are described in
Section 15.3.8.3, “Restrictions on XA Transactions”.
在客户端,没有特别的要求。与 MySQL 服务器的 XA 接口由以 XA 关键字开头的 SQL 语句组成。MySQL 客户端程序必须能够发送 SQL 语句并理解 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 work.
在 MySQL 连接器中,MySQL Connector/J 5.0.0 及更高版本直接支持 XA,通过一个类接口为你处理 XA SQL 语句接口。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 支持分布式事务,也就是允许多个独立的事务资源参与全局事务的能力。事务资源通常是关系型数据库管理系统(RDBMS),但也可能是其他类型的资源。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.
全局事务涉及多个本身具有事务性的操作,但这些操作必须作为一个整体成功完成,或作为一个整体回滚。本质上,这将 ACID 属性“提升一级”,以便多个 ACID 事务可以作为全局操作的组成部分同时执行,该全局操作也具有 ACID 属性。(与非分布式事务一样,如果应用程序对读取异常敏感,可能首选 SERIALIZABLE。REPEATABLE READ 对于分布式事务可能不够。)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.)
一些分布式事务的例子:Some examples of distributed transactions:
应用程序可以作为集成工具,将消息服务与 RDBMS 结合起来。该应用程序确保涉及消息发送、检索和处理的事务,以及涉及事务性数据库的操作,都发生在全局事务中。你可以将其视为“事务性邮件”An application may act as an integration tool that combines a messaging service with an RDBMS. The application makes sure that transactions dealing with message sending, retrieval, and processing that also involve a transactional database all happen in a global transaction. You can think of this as “transactional email.”
应用程序执行涉及不同数据库服务器的操作,如 MySQL 服务器和 Oracle 服务器(或多个 MySQL 服务器),其中涉及多个服务器的操作必须作为全局事务的一部分发生,而不是作为每个服务器本地的单独事务。An application performs actions that involve different database servers, such as a MySQL server and an Oracle server (or multiple MySQL servers), where actions that involve multiple servers must happen as part of a global transaction, rather than as separate transactions local to each server.
银行将账户信息保存在关系数据库管理系统(RDBMS)中,并通过自动取款机(ATM)分发和接收资金。必须确保 ATM 操作正确反映到账户中,但仅靠 RDBMS 无法实现这一点。全球事务管理器整合 ATM 和数据库资源,以确保金融事务的整体一致性。A bank keeps account information in an RDBMS and distributes and receives money through automated teller machines (ATMs). It is necessary to ensure that ATM actions are correctly reflected in the accounts, but this cannot be done with the RDBMS alone. A global transaction manager integrates the ATM and database resources to ensure overall consistency of financial transactions.
使用全球事务的应用程序涉及一个或多个资源管理器和一个事务管理器:Applications that use global transactions involve one or more Resource Managers and a Transaction Manager:
资源管理器(RM)提供对事务性资源的访问。数据库服务器是一种资源管理器。必须能够提交或回滚由 RM 管理的事务。A Resource Manager (RM) provides access to transactional resources. A database server is one kind of resource manager. It must be possible to either commit or roll back transactions managed by the RM.
事务管理器(TM)协调属于全球事务的各个事务。它与处理这些事务的 RM 进行通信。全球事务中的各个事务是全球事务的“分支”。全球事务及其分支通过后面描述的命名方案进行标识。A Transaction Manager (TM) coordinates the transactions that are part of a global transaction. It communicates with the RMs that handle each of these transactions. The individual transactions within a global transaction are “branches” of the global transaction. Global transactions and their branches are identified by a naming scheme described later.
MySQL 的 XA 实现使 MySQL 服务器能够作为资源管理器处理全球事务中的 XA 事务。连接到 MySQL 服务器的客户端程序则作为事务管理器。The MySQL implementation of XA enables a MySQL server to act as a Resource Manager that handles XA transactions within a global transaction. A client program that connects to the MySQL server acts as the Transaction Manager.
要执行全球事务,必须知道涉及哪些组件,并将每个组件带到可以提交或回滚的状态。根据每个组件报告的成功能力,必须以原子组方式全部提交或全部回滚。也就是说,要么所有组件都提交,要么所有组件都回滚。要管理全球事务,还必须考虑任何组件或连接网络可能失败的情况。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.
执行全球事务的过程使用两阶段提交(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.
在第一阶段,所有分支都要做好准备。也就是说,TM 通知它们准备提交。通常,这意味着管理分支的每个 RM 将在稳定存储中记录分支的操作。各分支指示其是否能够执行此操作,这些结果用于第二阶段。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.
在第二阶段,TM 告诉 RM 是提交还是回滚。如果所有分支在准备阶段都表示能够提交,则所有分支被通知提交。如果任何分支在准备阶段表示无法提交,则所有分支被通知回滚。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 were able to commit, all branches are told to commit. If any branch indicated when it was prepared that it was not able to commit, all branches are told to roll back.
在某些情况下,全球事务可能使用单阶段提交(1PC)。例如,当事务管理器发现全球事务只包含一个事务性资源(即单个分支)时,该资源可以同时被告知准备和提交。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.