Sunday, 20 May 2012

SQL 2008 Transaction

What Is a Transaction?
A transaction may be a single operation or collection of different operations that succeed or fail together as a whole, thereby ensuring consistency of data should unexpected situation arise. One scenario might be a financial transaction. For example, let's say you buy a car. The single transaction of purchasing item from online store a consists of distinct operations as below:
1.       Select a product.
2.       Select the payment mode.
3.       Enter credential for payment.
4.       Payment transferred
5.       Product ship to customer address.
Skipping any of these steps could cause major angst to one or more of the parties involved. This is a simple example of a set of steps that must always occur together in a consistent manner.
Transactions allow us to ensure consistency in our data through four basic principles. These principles provide a set of rules that must be followed for our transaction to succeed. The four principles help ensure that the state of our data is atomic, consistent, isolated, and durable, regardless of the success or failure of the transaction.

Acid Property

Consider the transaction of purchasing product using debit card to purchase a product worth $200. The data for this transaction can be easily represented in a database as a table with two columns, one holding your AccountID and the other holding your AccountBalance.
To begin with, consider your account balance is $200, so after withdrawing $200, your updated balance should be zero. Also, it makes logical sense that the system must ensure that you have the funds available in your account before the cash can be dispensed. What this means in database terms is that two database queries must be run in this transaction. The first query checks the account balance, as shown here:

SELECT AccountBalance FROM Account WHERE AccountID = @AccountID

If the query returns an AccountBalance value greater than or equal to the requested withdrawal, you can withdraw the cash.
After withdrawing the cash, you must update the account record with the updated balance. To do so, you run an UPDATE query:


UPDATE Account
 SET AccountBalance = AccountBalance – 100
 WHERE AccountID = @AccountID

The two distinct operations in this transaction are the two database queries that support the one operation of withdrawing cash. Both must succeed or fail together in an atomic manner, or the transaction should not be considered complete. Atomicity is the first ACID property.
Now let's change the nature of the transaction a bit. Let's say that the original account balance is $250 and that within the same transaction, the user requests to withdraw $200 and to purchase another product worth $75 to a second account. The first update query will succeed, changing the account balance to $250 – $200 = $50. But the second operation will fail because there won't be enough money left in the account to purhcaser the $75. You therefore need a way to undo the cash withdrawal and return the database to its original state. You cannot leave the database midway through a transaction because it is in an inconsistent state. In a real-world scenario, you would not normally wrap a withdrawal and a transfer in the same transaction, but the point here is to show how data can end up in an inconsistent state between multiple operations. Rolling back operations that cannot be completed in this manner demonstrates the second ACID property, consistency.
Let's say now that the withdrawal and transfer operations are separated into two distinct transactions instead of one but that they happen to run simultaneously. Each transaction will have to check the current balance by attempting to execute a query like this:


SELECT AccountBalance FROM Account WHERE AccountID = @AccountID

Unless your system has explicit checks blocking concurrent reads, both transactions will get the same result: $250. Thus they will both assume that the account has enough funds for the transaction. One transaction will disburse $200, and the other will transfer $75. The result will be an overall deduction of $200 + $75 = $275, even though the account actually has only $250 available. In many systems, especially financial applications, such transactions must be isolated from each other to prevent what is known as a "dirty read." A dirty read happens when data is read at one point in a transition state and the result of the query doesn't reflect the data's true state at the end of the current operation. This brings us to the third ACID property, isolation.
Isolation means that other transactions attempting to request a common resource will be blocked. Blocking, in turn, seriously affects the response times of your application. As it turns out, you'll often want to relax this blocking behavior to suit your application architecture. You can do that by using isolation levels, which we'll discuss later in this chapter.
Last, when you have successfully completed all your operations within a transaction, you don't want to lose the changes made. In other words, system failures must not affect the transactional integrity of your operations. This brings us to the fourth ACID property, durability. Durability means that the systems involved with the transaction will faithfully retain the correct transacted state even if the system sustains catastrophic failure nanoseconds after the transaction completes. Conversely, if the transaction isn't completed because of system failure, it won't be allowed to complete (or be undone) until the system is reset and the application restarted. Transacted steps are retained, and the application can resume operations where it left off with no ill effects (at least from a data consistency perspective).
Let's summarize the four ACID properties before we continue:
  • Atomicity Operations succeed or fail together. Unless all steps succeed, the transaction cannot be considered complete.
  • Consistency Operations leave the database in a consistent state. The transaction takes the underlying database from one stable state to another, with no rules violated before the beginning or after the end of the transaction.
  • Isolation Every transaction is an independent entity. One transaction will not affect any other transaction that is running at the same time.
  • Durability Every transaction is persisted to a reliable medium that cannot be undone by system failures. Furthermore, if a system failure does occur in the middle of a transaction, either the completed steps must be undone or the uncompleted steps must be executed to finish the transaction. This typically happens by use of a log that can be played back to return the system to a consistent state.