Atomicity, Consistency, Isolation and Durability (ACID)
theory is familiar to many programmers. Those who work on Operating System
design use it almost everday.
The most important aspect of ACID is an Atomic transaction.
What is an Atomic transaction? The best way to describe it is a famous
example: a bank accounting system.
Consider that bank A and bank B want to interact with
someone's account at the same time. Both banks want to withdraw from the account
and the account has $10.00 in it. If bank A takes $7.00 and at the same time
bank B tries to get $5.00, what will happen? When they start the transaction
each bank believes there is $10.00 available in the account. When one of them
finishes the other one will find there is not enough money to finish the
This scenario is common for computer systems and you can see
it many times in memory management, IO operations and database interactions.
Atomic transactions are a way to avoid this problem. They
simply lock on a transaction and do not allow any other transaction to interact
with the resource. If anything fails during the Atomic transaction, everything
will return to the state before the transaction started.
In this article I give a step by step sample to write an
Atomic transaction in .NET and test it to see it in action. In this article I
created a simple accounting system and then in an Atomic transaction I add
values to an account and a place to throw exceptions for some arguments to see
what would happen if something goes wrong in an Atomic transaction.