Monday, July 05, 2004

What is a Transaction?

The term Transaction is used in different functional areas to mean different functional aspect, albeit with a significant degree of similarity. As a result, the definitions vary depending upon the perspective from which it is looked upon.
In terms of software applications, I have found a few definitions/explanations of a transaction and summed up to something as follows:


A transaction denotes a smallest possible set of activities to complete a unit of work pertaining to the functional requirements for that particular application.

Let's take an example (this is one those heavily used example to explain transaction, and I like this very much). There is an account A and an account B, each containing 1000 bucks. Now, a transaction has been requested to transfer 500 bucks from account A to account B. From functional point of view, this transfer denotes a transaction. From application point of view, this denotes a set of activities which are to be performed as a unit of work. The activities are:

1. Deduct 500 bucks from account A.
2. Add 500 bucks into account B.
3. Log the final balance of account A as 500.
4. Log the final balance of account B as 1500.

All these four activities must be completed successfully to ensure that the transaction gets successfully completed. If any one of these fails, all earlier activities have to be rolled back to bring the states of the accounts into their originial ones, thus denoting the transaction to be a failed/incomplete one. So in such an example, the set of these four activities are considered to be a transaction.

Depending upon the functional requirement, number of activities can vary. As example, the transaction above, may include some validations to ensure that the account balance does not fall below zero. That way, one more additional activity would get added prior to activity #1, and the transaction would be a set of five activities:

1. Check that account balance of A is greater than or equal to 500.
2. Deduct 500 bucks from account A.
3. Add 500 bucks into account B.
4. Log the final balance of account A as 500.
5. Log the final balance of account B as 1500.

Each of these activities can be implemented using a specific programming block called functions or methods.