Transactions


Automatic transactions are the most frequently used feature of Enterprise Services. Using Enterprise Services, you can mark the components as requiring a transaction, and the transaction is then created from the COM+ runtime. All transaction-aware objects inside the component, such as ADO.NET connections, run inside the transaction.

Tip 

You can read more about the concepts of transactions in Chapter 21, “Transactions.”

Transaction Attributes

Serviced components can be marked with the [Transaction] attribute to define if and how transactions are required for the component.

Figure 38-6 shows multiple components with different transactional configurations. The client invokes component A. Because component A is configured with Transaction Required and no transaction existed previously, the new transaction, 1, is created. Component A invokes component B, which in turn invokes component C. Because component B is configured with Transaction Supported, and the configuration of component C is set to Transaction Required, all three components (A, B, and C) do use the same transaction context. If component B were configured with the transaction setting NotSupported, component C would get a new transaction. Component D is configured with the setting New Transaction Required, so a new transaction is created when it is called by component A.

image from book
Figure 38-6

Open table as spreadsheet

TransactionOption Value

Description

Required

Setting the [Transaction] attribute to TransactionOption .Required means that the component runs inside a transaction. If a transaction has been created already, the component will run in the same transaction. If no transaction exists, a transaction will be created.

RequiresNew

TransactionOption.RequiresNew always results in a newly created transaction. The component never participates in the same transaction as the caller.

Supported

With TransactionOption.Supported, the component doesn’t need transactions itself. However, the transaction will span the caller and the called component, if these components require transactions.

NotSupported

The option TransactionOption.NotSupported means that the component never runs in a transaction, regardless of whether the caller has a transaction.

Disabled

TransactionOption.Disabled means that a possible transaction of the current context is ignored.

Transaction Results

A transaction can be influenced by setting the consistent and the done bit of the context. If the consistent bit is set to true, the component is happy with the outcome of the transaction. The transaction can be committed if all components participating with the transaction are similarly successful. If the consistent bit is set to false, the component is not happy with the outcome of the transaction, and the transaction will be aborted when the root object that started the transaction is finished. If the done bit is set, the object can be deactivated after the method call ends. A new instance will be created with the next method call.

The consistent and done bits can be set using four methods of the ContextUtil class with the results that you can see in the following table.

Open table as spreadsheet

ContextUtil Method

Consistent Bit

Done Bit

SetComplete

true

true

SetAbort

false

true

EnableCommit

true

false

DisableCommit

false

false

With .NET it is also possible to set the consistent and done bit by applying the attribute [AutoComplete] to the method instead of calling the ContextUtil methods. With this attribute the method ContextUtil .SetComplete() will be called automatically if the method is successful. If the method fails and an exception is thrown, with [AutoComplete] the method ContextUtil.SetAbort() will be called.




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

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