What's a Transaction?

What is a transaction? Broadly speaking, a transaction is a mechanism to ensure that several operations succeed or fail as an atomic unit. In other words, it ensures that all operations succeed or all operations fail in the event that even one of the constituent components of the transaction encounters an error. Broadly speaking, transactions enforce the ACID rule popularized in database programming. WCF provides a simple centralized manner to support transactions within your applications. Prior to WCF, although there were mechanisms to support transactions (Begin Transaction...End Transaction in Visual Basic, for example), a single standard means of being able to support nondatabase transactions was not a trivial task to say the least. A transaction enables you to carry out a set of operations as a single execution unit whereby you achieve reliable results.

In the QuickReturns Ltd. scenario, you conduct trades on the stock exchange, and not knowing for sure whether a trade was successful could have disastrous results. Using the transaction mechanism, you achieve the following results:


Atomicity

The trades go through the system as single unit, thus achieving durability or aborting as a single unit, where nothing goes through in a rollback of the transaction.


Consistency

This guarantees that the transaction is a correct transformation in the system state. All orders that are part of a single transaction do so with the correct attributes (buy/sell, associated quantities/prices), and that behavior is repeatable identically time after time.


Isolation

This ensures that each transaction is independent and isolated from other transactions that could be occurring simultaneously in the system. The transactions do not have any impact on the behavior of any other transaction that might be occurring in the system. Looking at the trade system, it is more than likely that more than one user is using the system to trade at the same time. The isolation of the transactions ensures that the trades conducted by other users, or even multiple trades occurring at the same time from a single user, are treated as being distinct and separate from each other.


Durability

As the name suggests, this ensures that all data is committed to a nonvolatile resource such as a database, an XML file, or even a flat file. The data of the transaction must remain available even though the transaction has long since completed. For us, this provides a record for the trades provided. You can check when a particular trade was conducted and have the data available to you whenever you want it.

WCF implements the WS-Atomic protocol to enable transaction support. This enables you to build transactions that can interact with multiple endpoints inside WCF and with third-party systems such as web services.

A transaction has multiple discrete players involved in the process. These components interact with each other and are essential for any given transaction. If you consider Figure 9-1, you can see that any transaction has three essential components: the application, the persistent store, and the transaction manager.

NOTE

Looking again at QuickReturns Ltd. from a business perspective, you might need transactions that span multiple business activities. For example, you might put in a sell order and then want to buy oil futures at a predefined price. Should you be unable to get your oil futures at the desired price, you might want to hold off on the sale itself. This scenario is referred to as a compensating transaction, whereby all discrete components of the flow are treated as a single transaction. The compensating transaction offers looser coupling than the atomic transaction protocol, with the trade-off that compensating transactions are inherently more difficult to code and maintain. WCF does not provide support for compensating transactions out of the box. In other words, should you need a compensating transaction, you would need to implement the code yourself, use a complementary product such as Windows Workflow Foundation, or utilize Microsoft Distributed Transaction Coordinator (MS DTC) to achieve this.

Participants in a transaction

KERNEL TRANSACTION COORDINATOR

The Kernel Transaction Coordinator is a feature of Windows Server 2007 that allows you to make the transaction available as a kernel object rather than a user object. The advantage of the Kernel Transaction Manager (KTM) is that it allows you to access system objects such as the file system or access files via transactions. You can leverage this in conjunction with WCF applications that need to read from files and write to files, for example, to provide transactional support where hitherto there was none.

It is important to keep in mind that the KTM is not part of WCF but rather the Windows Server 2007 family. You can find more information about the KTM and its usage at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/KTM/fs/transaction_managers.asp.


The application imitates a transaction, which could be over any of the supported protocols, in essence to commit it to a persistent store such as SQL Server or Oracle or even a flat file. The coordination between the application and the persistent store to comply with the ACID rule for transactions takes place by the transaction manager in the background. This could be the Lightweight Transaction Manager (LTM), the MS DTC, or even a custom transaction manager. The role of the transaction manager is to enlist all parties in the transaction, preparing them for the transaction. This means ensuring that they are available and ready to participate in the transaction. (The persistent store is also referred to as a resource manager in some documentation.) Then you can commit, or you can roll back if the data is not persisted. The LTM is implemented by System.Transactions and provides an extremely fast means for transaction management for volatile data within the application domain. Figure 9-2 displays the transaction stack in .NET 3.0 including the kernel transaction manager, which will be available in Windows Server 2007. (See the "Kernel Transaction Coordinator" sidebar for more information.)

Transactions in .NET 3.0

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset