Documente Academic
Documente Profesional
Documente Cultură
Distributed Transaction
A distributed transaction is a transaction that updates data on two or more networked computer systems. Distributed transactions extend the benefits of transactions to applications that must update distributed data. Implementing robust distributed applications is difficult because these applications are subject to multiple failures, including failure of the client, the server, and the network connection between the client and server. In the absence of distributed transactions, the application program itself must detect and recover from these failures.
For distributed transactions, each computer has a local transaction manager. When a transaction does work at multiple computers, the transaction managers interact with other transaction managers via either a superior or subordinate relationship. These relationships are relevant only for a particular transaction. Each transaction manager performs all the enlistment, prepare, commit, and abort calls for its enlisted resource managers (usually those that reside on that particular computer). Resource managers manage persistent or durable data and work in cooperation with the DTC to guarantee atomicity and isolation to an application. In a distributed transaction, each participating component must agree to commit a change action (such as a database update) before the transaction can occur. The DTC performs the transaction coordination role for the components involved and acts as a transaction manager for each computer that manages transactions. When committing a transaction that is distributed among several computers, the transaction manager sends prepare, commit, and abort messages to all its subordinate transaction managers. In the two-phase commit algorithm for the DTC, phase one involves the transaction manager requesting each enlisted component to prepare to commit; in phase two, if all successfully prepare, the transaction manager broadcasts the commit decision. In general, transactions involve the following steps: 1. Applications call the transaction manager to begin a transaction. 2. When the application has prepared its changes, it asks the transaction manager to commit the transaction. The transaction manager keeps a sequential transaction log so that its commit or abort decisions will be durable.
o
If all components are prepared, the transaction manager commits the transaction and the log is cleared. If any component cannot prepare, the transaction manager broadcasts an abort decision to all elements involved in the transaction.
While a component remains prepared but not committed or aborted, it is in doubt about whether the transaction committed or aborted. If a component or transaction manager fails, it reconciles in-doubt transactions when it reconnects.
When a transaction manager is in-doubt about a distributed transaction, the transaction manager queries the superior transaction manager. The root transaction manager, also referred to as the global commit coordinator, is the transaction manager on the system that initiates a transaction and is never in-doubt. If an in-doubt transaction persists for too long, the system administrator can force the transaction to commit or abort.
The enlisted resource managers are also members of this commit tree. They usually have a subordinate relationship to their local transaction manager. The superior and subordinate relationships are relevant only for a particular transaction. That is, transaction manager A can be the superior to transaction manager B on a particular transaction while functioning as a subordinate to transaction manager B on a different transaction. When a distributed transaction commits or aborts, the prepare, commit, and abort messages flow outward on the commit tree. Any node of the tree can unilaterally abort a transaction at any time before it agrees to the prepare request sent in the first phase. After a node has prepared, it remains prepared and in-doubt until the commit coordinator tells it to commit or abort the transaction. The root transaction manager of the commit tree is called the global commit coordinator. This coordinator makes the decision to either commit or abort the transaction and is never in doubt. If a computer fails and then restarts, the transaction manager at that computer tries to determine the outcome of all in-doubt transactions in which it participated. The local transaction manager reads its log file to determine the outcomes of transactions for which it was the commit coordinator and takes one of the following actions: 1. If it has a superior transaction manager, the local transaction manager determines whether it was previously notified of the outcome of the transaction by the superior transaction manager. 2. If the local transaction manager is in-doubt regarding a particular transaction, it queries its superior to learn the transactions outcome. The transaction manager also responds to queries from subordinate transaction managers regarding in-doubt transactions, similar to the protocol that transaction managers and resource managers follow at restart. The transaction manager determines the outcome of each in-doubt transaction and informs the resource managers of the transaction's outcome. System or communication failures can leave transactions in-doubt for extended periods of time. While a transaction is in-doubt, the resources modified by the transaction remain locked and unavailable to others. The system administrator can use the Component Services administrative tool to resolve transactions that remain in-doubt.
commit fails, the transaction aborts. If the application program can't successfully complete the transaction, it can call the Abort method to undo the transaction's effects. If the program fails before it commits the transaction, the transaction manager aborts the transaction and informs each enlisted resource manager to rollback the transaction's effects. If either the computer or resource manager fails, the transaction is also aborted. If the transaction successfully commits, the resource managers and the transaction manager ensure that the transaction's effects are durable, even if there are subsequent failures.
transaction manager tells the resource manager the outcome of each in-doubt transaction, and the resource manager commits or aborts these transactions accordingly. The DTC helps resource managers make transactions atomic and durable when a resource manager fails and then restarts. The resource manager must reconstruct the committed state of the resources it manages, reflecting all of the effects of committed transactions and none of the effects of aborted transactions.
Commit Coordination
The DTC uses the presumed abort two-phase commit protocol optimization. That is, if an enlisted transaction participant asks the DTC about a transaction and the DTC has no information, the enlisted participant assumes that the transaction has aborted. The DTC also supports the following two-phase commit optimizations:
In this case, the coordinating transaction manager sets fSinglePhase when invoking the ITransactionResourceAsync::PrepareRequest method at phase one. This instructs the resource manager that it should act as the transaction coordinator. The DTC delegates commit coordinator responsibility to the subordinate transaction manager when both of the following two conditions are true:
1. No resource manager is enlisted in the transaction with the current coordinating transaction manager. 2. Only one subordinate transaction manager is enlisted in the transaction.
In this case, the coordinating transaction manager sends a "delegated commit" message in place of the phase one message. The "delegated commit" message transfers commit coordinator responsibility from the current coordinating transaction manager to its subordinate transaction manager. This optimization is applied recursively. A transaction manager that receives commit coordinator responsibility may delegate commit coordinator responsibility to its subordinate
transaction manager or resource manager. Using this optimization eliminates the need for certain log writes and also allows for earlier release of locks on some resources.
Transaction Propagation
A transaction begun in one process can be propagated to another process. Resource managers built on a client/server architecture have a server side and a client side. The server side enlists in a transaction so that it can participate in the two-phase commit. However, because the DTC transaction might have been initiated on the client side, client/server resource managers use a DTC-implemented mechanism to propagate a transaction from the client side to the server side. To propagate a transaction, the resource manager proxy on the client side provides a function that an application can call to request a resource manager to enlist in a transaction. After obtaining the transaction to enlist in, the resource manager proxy uses ITransactionExport to propagate the transaction to the transaction coordinator.
To export a transaction
1. The application client obtains an ITransaction interface by invoking the ITransactionDispenser::BeginTransaction method. 2. The application client calls the IResourceManager2::Enlist2 method, passing it the transaction in which it wants the resource manager to enlist. 3. Within the IResourceManager2::Enlist2 method, the resource manager proxy does the following:
o
Invokes ITransactionExport::Export, passing it the ITransaction interface provided by the client. (See Obtaining the Whereabouts of the Participating DTC for information about how to obtain the ITransactionExport interface. If the preceding step succeeds, the resource manager proxy obtains the transaction cookie by invoking ITransactionExport::GetTransactionCookie. The resource manager proxy sends the transaction cookie to the resource manager via a communication pipe that is used for the transmission of all messages between the proxy and the resource manager server.
After the resource manager server receives the transaction cookie, it invokes ITransactionImport::Import, providing it with the cookie that it received from its proxy. This invocation translates the cookie to an ITransaction interface, and if it succeeds, the resource manager has an interface to the client-initiated transaction.