TRANSACTION
TRANSACTION
Introduction:
A transaction is a set of changes that must all be made together. It is a program
unit whose execution mayor may not change the contents of a database. Transaction is
executed as a single unit. If the database was in consistent state before a transaction,
then after execution of the transaction also, the database must be in a consistate. For
example, a transfer of money from one bank account to another requires two changes to
the database both must succeed or fail together.
Consider that you are working in a system for a bank. A customer goes to the
ATM and instructs it to transfer Rs. 1000 from savings to a checking account. This
simple transaction requires two steps:
• Subtracting the money from the savings account balance.
• Adding the money to the checking account balance.
The code to create this transaction will require two updates to the database. For
example, there will be two SQL statements: one UPDATE command to decrease the
balance in savings and a second UPDATE command to increase the balance in the
checking account.
What would happen if a machine crashed between these two operations. The
money has already been subtracted from the savings account will not be added to the
checking account. It is lost. You might consider performing the addition to checking
first, but then the customer ends up with extra money, and the bank loses. The point is
that both changes must be made successfully. Thus, a transaction is defined as a set of
changes that must be made together.
Process of Transaction
The transaction is executed as a series of reads and writes of database objects, which are
explained below:
Read Operation
To read a database object, it is first brought into main memory from disk, and then its
value is copied into a program variable as shown in figure.
Write Operation
To write a database object, an in-memory copy of the object is first modified and then
written to disk.
For Example, T1 (debit of Rs. 1000 from A) and T2 (credit of 500 to A) executing con-
currently, the database reaches inconsistent state.
Let us assume Account balance of A is Rs. 5000. T1 reads A(5000) and stores the value
in its local buffer space. Then T2 reads A(5000) and also stores the value in its local buf-
fer space.
T1 performs A=A-1000 (5000-1000=4000) and 4000 is stored in T1 buffer space. Then
T2 performs A=A+500 (5000+500=5500) and 5500 is stored in T2 buffer space. T1
writes the value from its buffer back to database.
A’s value is updated to 4000 in database and then T2 writes the value from its buffer
back to database. A’s value is updated to 5500 which shows that the effect of debit trans -
action is lost and database has become inconsistent.
To maintain consistency of database, we need concurrency control protocols which will
be discussed in next article. The operations of T1 and T2 with their buffers and database have
been shown in Table 1.
Table 1
Example
In order to understand above properties consider the following example:
Let, Ti is a transaction that transfers Rs 50 from account A to account B. This
transaction can be defined as:
States of Transaction
A transaction must be in one of the following states:
Active: the initial state, the transaction stays in this state while it is executing.
Partially committed: after the final statement has been executed.
Failed: when the normal execution can no longer proceed.
Aborted: after the transaction has been rolled back and the database has been
restored to its state prior to the start of the transaction.
Committed: after successful completion.
The state diagram corresponding to a transaction is shown in Figure.
Active − In this state, the transaction is being executed. This is the initial state of every
transaction.
Partially Committed − When a transaction executes its final operation, it is said to be in
a partially committed state.
Failed − A transaction is said to be in a failed state if any of the checks made by the
database recovery system fails. A failed transaction can no longer proceed further.
Aborted − If any of the checks fails and the transaction has reached a failed state, then
the recovery manager rolls back all its write operations on the database to bring the
database back to its original state where it was prior to the execution of the transaction.
Transactions in this state are called aborted. The database recovery module can select one
of the two operations after a transaction aborts −
o Re-start the transaction
o Kill the transaction
Committed − If a transaction executes all its operations successfully, it is said to be
committed. All its effects are now permanently established on the database system.
A transaction starts in the active state. When it finishes its final statement, it enters the
partially committed state. At this point, the transaction has completed its execution, but
it is still possible that it may have to be aborted, since the actual output may still be
temporarily hiding in main memory and thus a hardware failure may preclude its
successful completion
The database system then writes out enough information to disk that, even in the event
of a failure, the updates performed by the transaction can be recreated when the system
restarts after the failure. When the last of this information is written out, the
transaction enters the committed state.