Overview C# Help

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 is a failure when getting the book from stock, the credit card should not be charged. Transactions address such scenarios.

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.

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

Figure 22-1 shows the main actors in a transaction. Transactions are managed and coordinated by the
“‘:msaction manager, and a resource manager manages every resource that influences the outcome of
the transaction. The transaction manager communicates with resource managers to define the outcome
of the transaction.

Transaction Phases

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

¤ Activephase – During the active phase, the transaction is created. Resource managers that manage the transaction (or resources can enlist with the transaction.

¤ Preparing phase – During the preparing phase, every resource manager can define the outcome of the transaction. This pi”rse starts when the creator of the transaction sends a commit to end the transaction. The transac on manager sends a Prepare message to all resource managers. If the resource manager .an 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 by sending a Rollback message. After the Prepared message is sent, the resource managers must guarantee to finish the work successfully in the committing phase. Tomake this possible, durable resource managers must write a log with the information from the prepared state, so that 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 have 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 specific requirements; for example, a transaction must result in a valid state, even if the server has a power failure. The characteristics of transactions can be defined by the term ACID. ACID is a four-letter acronym for atomic, 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 Buntil 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 (for example, writing an entry into a list) does not need to be durable. Also, a complete isolation from the outside is not always required, as we discuss later with transaction isolation levels.

Posted on October 31, 2015 in Transactions

Share the Story

Back to Top
Share This