Programming Transactions


Indigo allows you to pass a transaction across a communication channel, which lets you coordinate operations performed by multiple services. To users, a transaction is a single event that either happens or doesn t happen. To developers, a transaction allows them to write components that can participate in distributed environments.

The System.Transactions.Transaction is the base class representing a single transaction. A transaction is created through a TransactionManager instance using the CreateTransaction or UnmarshalTransaction method. Alternatively, you can also create a transaction by calling the static System.Transactions.Transaction.Create method, which creates a new transaction with default values.

A transaction can be committed only by the client application that created the transaction. When a client application wants to allow access to the transaction by multiple threads but also wants to prevent those other threads from committing the transaction, the application can use a clone of the transaction. A cloned transaction has the same capabilities as the original transaction, except for the ability to commit the transaction.

Transaction instances can be constructed using the static method Create of the Transaction class as follows :

 tx = Transaction.Create(); 

Alternatively, you can create a new TransactionManager instance and have it create a transaction for you, as follows:

 tm = TransactionManager.Create(); 
tx = tm.CreateTransaction();

There are also alternative creation mechanisms that allow you to supply the description, isolation level, and timeout value of the transaction. To commit or roll back a transaction, you will call the respective Commit or Rollback methods of the Transaction instance.

 tx.Commit(); 
tx.Rollback();

Applications use outcome notifications to release resources or perform other actions after a transaction commits or aborts. A transaction participant that wants to be notified of the commit-abort decision registers for the TransactionOutcome event.

In the following example, we have implemented an event handler named OnTransactionCompleted that is going to handle the TransactionCompleted event. We then associate this handler with the event by using the following code:

 tx.TransactionCompleted += 
new TransactionCompletedEventHandler (OnTransactionCompleted);

After you have committed or rolled back the transaction, the TransactionCompleted event will be fired automatically by the system, which triggers the execution of OnTransactionCompleted .

This example will demonstrate how to enlist in a Microsoft SQL Server transaction. It first creates a transaction, and then opens a connection to "pubs" , which is a SQL server database. The example then marshals the transaction object to make it compatible with EnterpriseService transactions, and associates the marshaled transaction with the opened connection. The user then has a chance to commit or roll back the transaction.

A new SqlConnection is instantiated and opened, with the connecting string set to the local server and the database set to pubs .

 conn = new SqlConnection(); 
conn.ConnectionString = @"Server=(local);Trusted_Connection=SSPI;Database=pubs;Enlist=false;";
conn.Open();

To enlist the current transaction as a distributed transaction of the open connection, you will call the EnlistDistributedTransaction method of the SqlConnection class. However, because this method only takes an object of the System.EnterpriseServices.ITransaction type, you will need to marshal the existing transaction for compatibility first. The Marshal method is used to perform such functionality.

 estx = (System.EnterpriseServices.ITransaction) 
tx.Marshal(MarshaledTransactionTypeNamespaceUri.ITransaction);
conn.EnlistDistributedTransaction (estx);



Introducing Microsoft WinFX
Introducing WinFX(TM) The Application Programming Interface for the Next Generation of Microsoft Windows Code Name Longhorn (Pro Developer)
ISBN: 0735620857
EAN: 2147483647
Year: 2004
Pages: 83
Authors: Brent Rector

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net