Tuesday, July 29, 2008

MTS Transactions

MTS Transactions
A transaction is an operation or set of operations that succeeds or fails as a logical unit. A good example of a transaction is the transfer of funds from one bank account to another. In this case, the funds must be debited from the first account and credited to the second account before the operation can be considered a success. If the funds are successfully debited but not credited, the debit from the first account must be undone to leave both accounts in a correct and consistent state.
Transactions are normally managed by declaring boundaries around a set of operations. Operations that execute in the context of the transaction boundary then succeed or fail as a unit. For ASP.NET, the transaction boundary is the execution of a single request to a page, which might contain nested components that participate in the same transaction. While the page is executing, if an operation on the page itself or a nested component in the same transaction fails, it can call ContextUtil.SetAbort. This is then picked up by the current transaction context, the entire transaction fails, and any operations that were already completed are undone. If nothing fails, the transaction is committed.
ASP.NET support for transactions consists of the ability to allow pages to participate in ongoing Microsoft .NET Framework transactions. Transaction support is exposed via an @Transaction directive that indicates the desired level of support:
<%@ Transaction="Required" %>
The following table defines the supported transaction attributes. The absence of a transaction directive is the same as an explicit directive to "Disabled". Unlike ASP, ASP.NET has no explicit directive for none (that is, Transaction="None").
Attribute Description
Required The page requires a transaction. It runs in the context of an existing transaction, if one exists. If not, it starts one.
RequiresNew The page requires a transaction and a new transaction is started for each request.
Supported The page runs in the context of an existing transaction, if one exists. If not, it runs without a transaction.
NotSupported The page does not run within the scope of transactions. When a request is processed, its object context is created without a transaction, regardless of whether there is an active transaction.
A transaction can be explicitly committed or aborted using static methods of the System.EnterpriseServices.ContextUtil class. You can explicitly call the SetComplete or SetAbort method to commit or abort an ongoing transaction.
Note: A transaction will commit or abort at the end of page's lifetime depending on the whether SetComplete or SetAbort was called last, provided there is no other object join the same transaction.

' Try to do something crucial to transaction completing.
If (Not DoSomeWork())
ContextUtil.SetAbort()
End If
VB
Section Summary
1. A transaction is an operation or set of operations that succeeds or fails as a logical unit.
2. ASP.NET transaction support consists of the ability to allow pages to participate in ongoing Microsoft .NET Framework transactions. Transaction support is exposed via an @Transaction directive that indicates the desired level of support.
3. A transaction can be explicitly committed or aborted using static methods of the System.EnterpriseServices.ContextUtil class. Developers can explicitly call the SetComplete or SetAbort method to commit or abort an ongoing transaction.

No comments: