Overview


What are transactions? Think about ordering a book from a Web site. The book ordering process removes the book you want to buy from 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. If there’s a failure when getting the book from stock, the credit card shouldn’t be charged. Such a scenario can be addressed by transactions.

The most common use of transactions is writing or updating data within the database. Transactions can also be performed when writing a message to a message queue, or writing data to a file or the registry. Multiple actions can be part of a single transaction.

Tip 

System.Messaging is discussed in Chapter 39, “Message Queuing.”

Figure 21-1 shows the main actors in a transaction. Transactions are managed and coordinated by the transaction manager. Every resource that influences the outcome of the transaction is managed by a resource manager. The transaction manager communicates with the resource managers to define the outcome of the transaction.

image from book
Figure 21-1

Transaction Phases

The timely phases of a transaction are the active, preparing, and committing phases.

  • Active phase - During the active phase the transaction is created. Resource managers that manage the transaction for resources can enlist with the transaction.

  • Preparing phase - During the preparing phase every resource manager can define the outcome of the transaction. This phase starts when the creator of the transaction sends a commit to end the transaction. The transaction manager sends a Prepare message to all resource managers. If the resource manager can produce the transaction outcome successfully, it sends a Prepared message to the transaction manager. Resource managers can abort the transaction if they fail to prepare by forcing a rollback with the transaction manager. After the Prepared message is sent, the resource managers must guarantee to finish the work successfully in the committing phase. To make this possible, durable resource managers must write a log with the information from the prepared state, so they can continue from there in case of, for example, a power failure between prepared and committing.

  • Committing phase.   The Committing phase begins when all resource managers prepared successfully. This is when the Prepared message is received from all resource managers. Then the transaction manager can complete the work by sending a Commit message to all participants. The resource managers can now finish the work on the transaction and return a Committed message.

ACID Properties

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

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 before the transaction was started and after the transaction is completed must be valid. During the transaction, the state may have interim values.

  • Isolation - Isolation means that transactions that happen concurrently are isolated from the state, which is changed during a transaction. Transaction A cannot see the interim 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.

Not every transaction requires all four ACID properties. For example, a memory-based transaction (e.g., writing an entry into a list) doesn’t need to be durable. Also, it is not always required to have a complete isolation from the outside, as is discussed later with transaction isolation levels.




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