Sunday, April 17, 2005

Transactions example

Transactions are used to make ensure that a series of statements either all get processed or don't get processed at all -- basically all or nothing. Why is this important? Let's consider a simple banking example. You transfer $500 from one bank account into another:

1. - $500 from account A
2. + $500 to account B

Step 1 processes successfully, but something bad happens between step 1 and 2. Where is your money now -- you just lost $500!! This is clearly not acceptable, we need to be able to confidently say that steps 1 and 2 must all process successfully, or none of it can process at all.

Enter the concept of transactions. With transactions, we would be able to do this:

1. BEGIN TRANSACTION
2. - $500 from account A
3. + $500 to account B
4. END TRANSACTION

Now steps 1 to 4 are treated as one atomic operation. If anything fails between steps 1 to 4, the entire operation is aborted and we revert back to the way things were before we started. The worst that can happen here is that the money doesn't get transferred, but at least you will not lose $500.

3 comments:

Unknown said...

Good Example even a non technical person can understand what is transaction using your exmple.. thanks.

Anonymous said...

Good Example.

Mohd said...

You know what! this is the most professional example I have ever read about transactions...

THANKS