How an Application Program Initiates a Transaction

 

Applies To: Windows 10, Windows 7, Windows 8, Windows 8.1, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2, Windows Server Technical Preview, Windows Vista

DTC transactions may be initiated and controlled by an application program written in C or C++. Most resource managers can participate in application program-initiated transactions, which allows your application program to do the following:

  • Invoke any OLE Transactions–compliant resource manager.

  • Explicitly control the scope and duration of the transaction.

Note

Although the DTC supports the ability to propagate a transaction from one application program to another, it is recommended that transaction propagation between application components be handled by the system. That is, the database system, transaction processing monitor, object request broker, or queuing system should perform transaction propagation on behalf of the application.

The following steps describe how an application program initiates a transaction:

  1. Connects to the DTC and obtains a transaction dispenser interface that it uses to initiate transactions.

  2. Opens the appropriate resource managers—for example, by connecting to a relational database using ODBC.

  3. Initiates the DTC transaction by calling BeginTransaction and obtaining a transaction object that represents the transaction.

  4. Propagates the transaction to each resource manager by invoking the resource manager's transaction enlistment interface. For example, the application program uses the ODBC SQLSetConnectAttr function to propagate the DTC transaction object to resource managers that support ODBC. All subsequent work done on the ODBC connection is performed as part of the transaction. When the resource manager becomes aware of the DTC transaction, it enlists in the transaction with its local the DTC transaction manager. This permits the resource manager to participate in the two-phase commit protocol and to receive transaction commit or abort notifications from the DTC.

  5. Invokes the normal resource manager functions that update transaction-protected resource manager data. For example, the application might insert, delete, or update records in a relational database, or it might invoke a database stored procedure that modifies the relational database.

  6. Calls Commit when the work of the transaction is complete. In response to the Commit call, the DTC uses the two-phase commit protocol to coordinate commitment of the transaction with all of the resource managers enlisted in the transaction. Alternatively, the application program could call Abort to undo the effects of the transaction. The application can then go on to perform more DTC transactions. For more information, see How Application Program Transactions Commit and Abort.

  7. Releases the transaction dispenser object and closes the ODBC connections when the application program completes.

In the DTC, a deadlock condition can occur if you attempt to nest transactions in the same resource manager. For example, if you have two objects (A and B) that attempt to acquire a lock on the same resource manager, the following scenario would force a deadlock condition:

  1. Object A initiates a transaction in the resource manager and acquires a lock on a specific resource.

  2. Object A invokes a method on Object B.

  3. Object B initiates its own transaction and attempts to acquire a lock on the same resource manager.

  4. Since the lock request from Object B is running on a different transaction than Object A, the resource manager blocks the attempt of Object B to block the resource, creating a deadlock condition.

The lock manager in the resource manager cannot detect the deadlock because the transactions for each object can wait indefinitely for the other to commit its associated transactions. The DTC cannot break the deadlock either because it is unaware of the dependency of each object on the other.

See Also

Example: Using an Application Program to Update Two SQL Server Databases
How Application Program Transactions Commit and Abort