Support for XA transactions is available for the InnoDB
storage engine. InnoDB
存储引擎支持XA事务。The MySQL XA implementation is based on the X/Open CAE document Distributed Transaction Processing: The XA Specification. MySQL XA实现基于X/Open CAE文档《分布式事务处理:XA规范》。This document is published by The Open Group and available at http://www.opengroup.org/public/pubs/catalog/c193.htm. 本文档由开放组发布,可在http://www.opengroup.org/public/pubs/catalog/c193.htm访问到。Limitations of the current XA implementation are described in Section 13.3.8.3, “Restrictions on XA Transactions”.第13.3.8.3节,“XA事务的限制”描述了当前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 MySQL服务器的XA接口由以XA
keyword. XA
关键字开头的SQL语句组成。MySQL client programs must be able to send SQL statements and to understand the semantics of the XA statement interface. MySQL客户端程序必须能够发送SQL语句并理解XA语句接口的语义。They do not need be linked against a recent client library. 它们不需要与最近的客户端库相链接。Older client libraries also 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.在MySQL连接器中,MySQL连接器/J 5.0.0及更高版本通过为您处理XA SQL语句接口的类接口直接支持XA。
XA supports distributed transactions, that is, the ability to permit multiple separate transactional resources to participate in a global transaction. XA支持分布式事务,即允许多个单独的事务资源参与全局事务的能力。Transactional resources often are RDBMSs but may be other kinds of resources.事务性资源通常是RDBMS,但也可能是其他类型的资源。
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. 本质上,这将ACID属性“扩展到一个级别”,以便多个ACID事务可以作为也具有ACID属性的全局操作的组件协同执行。(As with nondistributed transactions, (与非分发事务一样,如果您的应用程序对读取现象敏感,则可能首选SERIALIZABLE
may be preferred if your applications are sensitive to read phenomena. SERIALIZABLE
。REPEATABLE READ
may not be sufficient for distributed transactions.)REPEATABLE READ
可能不足以进行分布式事务。)
Some examples of distributed transactions:分布式事务的一些示例:
An application may act as an integration tool that combines a messaging service with an RDBMS. 应用程序可以充当集成工具,将消息传递服务与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.”您可以将其视为“事务性电子邮件”。
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.应用程序执行涉及不同数据库服务器的操作,例如MySQL服务器和Oracle服务器(或多个MySQL服务器),其中涉及多个服务器的操作必须作为全局事务的一部分进行,而不是作为每个服务器本地的单独事务进行。
A bank keeps account information in an RDBMS and distributes and receives money through automated teller machines (ATMs). 银行将账户信息保存在RDBMS中,并通过自动柜员机(ATM)分发和接收资金。It is necessary to ensure that ATM actions are correctly reflected in the accounts, but this cannot be done with the RDBMS alone. 有必要确保ATM操作正确地反映在帐户中,但这不能仅用RDBMS来实现。A global transaction manager integrates the ATM and database resources to ensure overall consistency of financial transactions.全局事务管理器集成ATM和数据库资源,以确保金融事务的总体一致性。
Applications that use global transactions involve one or more Resource Managers and a Transaction Manager:使用全局事务的应用程序涉及一个或多个资源管理器和一个事务管理器:
A Resource Manager (RM) provides access to transactional resources. 资源管理器(RM)提供对事务性资源的访问。A database server is one kind of resource manager. 数据库服务器是一种资源管理器。It must be possible to either commit or roll back transactions managed by the RM.必须能够提交或回滚由RM管理的事务。
A Transaction Manager (TM) coordinates the transactions that are part of a global transaction. 事务管理器(TM)协调作为全局事务一部分的事务。It communicates with the RMs that handle each of these transactions. 它与处理这些事务的RMs进行通信。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.全局事务及其分支由后面描述的命名方案标识。
The MySQL implementation of XA enables a MySQL server to act as a Resource Manager that handles XA transactions within a global transaction. XA的MySQL实现使MySQL服务器能够充当资源管理器,在全局事务中处理XA事务。A client program that connects to the MySQL server acts as the Transaction Manager.连接到MySQL服务器的客户端程序充当事务管理器。
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.要管理全局事务,必须考虑到任何组件或连接网络可能出现故障。
The process for executing a global transaction uses two-phase commit (2PC). 执行全局事务的过程使用两阶段提交(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. 也就是说,TM告诉他们准备好提交。Typically, this means each RM that manages a branch records the actions for the branch in stable storage. 通常,这意味着管理分支的每个RM都会在稳定存储中记录分支的操作。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. 在第二阶段,TM告诉RMs是提交还是回滚。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.如果任何分支在准备时指出它无法提交,则所有分支都会被告知回滚。
In some cases, a global transaction might use one-phase commit (1PC). 在某些情况下,全局事务可能使用单阶段提交(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.例如,当事务管理器发现全局事务只包含一个事务资源(即单个分支)时,可以告诉该资源同时准备和提交。