Transactions


Transactions within a serviced component are controlled either with code inside a method call or with attributes. Transactions in COM+ work in a similar fashion as the new System.Transactions namespace in the .NET Framework. If a serviced component indicates that a transaction should commit, then if that component is the owner of the transaction, it will commit. However, if that component was invoked by another component, the component will only be allowed to "vote" as to the commit status of the parent transaction. This can all be configured through various options.

To control the transactional properties of a class through attributes, you use the transactionAttribute class.

Table 40.2 contains a list of the properties that can be set for the transactionAttribute class.

Table 40.2. transactionAttribute Properties

Property

Description

Isolation

Controls the isolation level of the transaction: Any, ReadCommitted, ReadUncommitted, RepeatableRead, Serializable

Timeout

Controls the timeout period for the transaction

Value

Sets the transaction option through the transactionOption enumeration: Disabled, NotSupported, Required, RequiresNew, Supported


The following code shows a class with the TRansactionAttribute controlling transaction options for the component:

[Transaction(TransactionOption.Required,   Isolation=TransactionIsolationLevel.Serializable,   Timeout=20)] public class TransactionalComponent: ServicedComponent {     // class implementation } 


Using the ContextUtil property of the ServicedComponent class, your component can manually control its vote for transaction commit or rollback, as shown in the following lines:

[Transaction(TransactionOption.Required,   Isolation=TransactionIsolationLevel.Serializable,   Timeout=20)] public class TransactionalComponent: ServicedComponent {     public void TransactionalMethod()     {         ContextUtil.MyTransactionVote = TransactionVote.Commit;         try         {           // perform work         }         catch         {           ContextUtil.MyTransactionVote = TransactionVote.Rollback;         }     } } 


This code will perform some task and if an exception occurs during the execution of that task, the method will indicate that the transaction should be rolled back.

Note

Keep in mind that transactions only work with Compensating Resource Managers (CRM). What this boils down to is that not all work will be automatically rolled back if your component indicates a rollback. For example, if your transaction modified a database and modified a text file and then indicated a rollback, the database would roll back all changes made during the transaction, but the text file would not. This is because files on disk aren't transactional. You need to be acutely aware of which resources you affect during a transaction to make sure they all roll back properly.




Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298

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