Transactions


Automatic transactions are the most frequently used feature of Enterprise Services. What are transactions? Think about ordering a book from a Web site. The book order process removes the book you want to buy from the stock and puts it in your order box, and the cost of your book is charged to your credit card. With these two actions, either both actions should complete successfully or neither of these actions should happen.

Such a scenario can be solved with transactions. A transaction has some specific requirements; for example, a valid state must be the result of a transaction. Valid state is also required if the server has a power failure. The characteristics of transactions can be defined with the term "ACID," as described in the following section.

ACID Properties

ACID is a four-letter acronym for atomicity, consistency, isolation, and durability:

  • Atomicity. Atomicity represents one unit of work. With a transaction, either the complete unit of work succeeds or nothing is changed.

  • Consistency. The state of the database must be a valid, consistent state after the transaction committed.

  • Isolation. Isolation means that transactions that happen concurrently are isolated from state that is changed during a transaction. Transaction A cannot see the changed state of transaction B until the transaction is completed.

  • Durability. After the transaction is completed, it must be stored in a durable way. This means that if the power goes down or the server crashes, the state must be recovered at reboot.

Transaction Attributes

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

TransactionOption Value

Description

Required

Setting the [Transaction] attribute to Transaction Option.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 transac- tion exists, a transaction will be created.

RequiresNew

TransactionOption.RequiresNew always results in a newly created transaction. The component never partici- pates 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 com- ponents 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.

Figure 30-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 30-6

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.

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
Pro Visual C++ 2005 for C# Developers
ISBN: 1590596080
EAN: 2147483647
Year: 2005
Pages: 351
Authors: Dean C. Wills

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