Before talking about the XA specification, you must first understand the concept of Distributed Transaction Processing, DTP. Transaction, ie transaction, also known as transactions, refers to a program or block, in one or more resources such as database or files to complete certain functions of certain functions.
Distributed transaction processing means that a transaction may involve multiple database operations, the key to distributed transaction processing is that there must be a way to know all the actions, or rollback transactions, the decision to submit or rollback must generate a unified result. (All submit or all rollback).
The X / Open organization (ie, the current Open Group) defines a distributed transaction model. X / Open DTP Model (1994) includes an application (AP), Transaction Manager (TM), Explorer (RM), and Communication Explorer (CRM). Typically, common transaction manager (TM) is a transaction middleware, and common resource manager (RM) is a database, and Common Communication Explorer (CRM) is a message middleware. For the convenience of expression, it is described herein to be described herein.
Treatment within a database, such as the operation of multiple tables, as a local transaction. The database's transaction object is a local transaction, while the object of distributed transaction processing is a global transaction.
The so-called global affairs refers to a distributed transaction environment, and multiple databases may need to work together. This job is a global transaction, for example, a few different databases may update several different databases. The operation of the database occurs in each of the system but must be all submitted or rolled. At this time, a database is successfully submitted to himself not only on whether it is successful, but also relies on whether the operation of other databases related to global transactions is successful. If any of the operations of any database fails, participate in this transaction all All the operations made by the database must roll back.
Under normal circumstances, a database cannot know what other databases are doing, so in a DTP environment, the transaction middleware is required, which is notified and coordinated by the relevant database submission or rollback. And a database onlys itself (recoverable) to the global transaction.
XA is the interface specification (ie, interface function) defined by the X / Open DTP definition, the internet access, the transaction middleware to notify the start, end, and submission of the database transaction, rollback, etc. The XA interface function is provided by a database manufacturer.
XA and two-stage submission agreement
Under normal circumstances, the transaction middleware and the database pass the XA interface specification, using two-stage submission to complete a global transaction, the XA specification basis is two-stage submission protocol.
In the first phase, the transaction middleware requests all relevant databases to prepare for the respective transaction branches to confirm that all relevant databases can be submitted to their own transaction branches. When a database receives the pre-submit, if you can submit your own transaction branch, you will fix your own operations to the operation branch, and give a reply to the transaction middleware, at which time the database will No one will be added to the transaction branch, but the database does not actually submit the transaction, the database has not been released (in the locked state) on the operation of the shared resource. If a database cannot be submitted to your own transaction branch due to some reason, it will roll back all your own operations, release the lock on the shared resource and return to the transaction middleware failed to answer.
In the second phase, the transaction middleware reviews the pre-transmission results returned by all databases, such as all databases can be submitted, and the transaction middleware will require all databases to formally submitted so that the global transaction is submitted. And if there is any database pre-submission return failure, the transaction middleware will require all other databases to roll their operation so that the global transaction is rolled back. Take a global affairs as an example, the AP first informs the transaction middleware to start a global transaction, and the transaction middleware notifies the database to start transactions via the XA interface function, and then the AP can operate on the resources managed by the database, the database system records all the local resources. operating. After the operation is completed, the transaction middleware notifies the database operation through the XA interface function. Trading middleware is responsible for recording AP operations (transaction branch). The AP is notified of the transaction middleware according to the situation to submit the global affairs, and the transaction middleware will require each database to make pre-submission through the XA interface function. All databases return to successfully require formal submission, and a global transaction ends.
The XA specification is for applications. The greatest advantage is that the integrity of transactions is controlled by the transaction middleware and database through XA interface. AP only needs to pay attention to the processing of application logic with the database, without having to care for the integrity of transactions, application design and development It will be simplified.
Specifically, if there is no trading middleware, the application system needs to directly notify the database to start, end, and submit a transaction inside. When there is an abnormal condition, it must be reversed by a dedicated program to reverse the database to complete the rollback. If there is a global transaction with many transaction branches, the situation will become abnormal when rolling. Using the XA interface, the commitment of global transactions is controlled by the intermediate part of the transaction. The application only needs to inform the transaction middleware to submit or roll up the transaction, you can control all the transactions (possibly related to multiple different places) all commit or return. Roll, the application does not need to consider the key logic at all.