6 Transaction Processing and Management
6 Transaction Processing and Management
For example, you are transferring money from your bank account to your
friend’s account, the set of operations would be like this:
Simple Transaction Example
1. R(A);
2. A = A - 10000;
3. W(A);
4. R(B);
5. B = B + 10000;
6. W(B);
In the above transaction R refers to the Read operation and W refers to the write operation.
The main problem that can happen during a transaction is that the
transaction can fail before finishing the all the operations in the set.
This can happen due to power failure, system crash etc. This is a
serious problem that can leave database in an inconsistent state.
Assume that transaction fail after third operation (see the example above)
then the amount would be deducted from your account but your friend will
not receive it.
Rollback: If any of the operation fails then rollback all the changes done by
previous operations.
Let’s say first operation passed successfully while second failed, in this
case A’s balance would be 300$ while B would be having 700$ instead
of 800$. This is unacceptable in a banking system. Either the
transaction should fail without executing any of the operation or it
should process both the operations. The Atomicity property ensures
that.
As we can see in the above diagram that a transaction goes into “partially
committed” state from the active state when there are read and write
operations present in the transaction.
A transaction contains number of read and write operations. Once the whole
transaction is successfully executed, the transaction goes into partially
committed state where we have all the read and write operations performed
on the main memory (local memory) instead of the actual database.
The reason why we have this state is because a transaction can fail during
execution so if we are making the changes in the actual database instead of
local memory, database may be left in an inconsistent state in case of any
failure. This state helps us to rollback the changes made to the
database in case of a failure during execution.
Only once the last statement of the transaction is executed, the transaction
enters this state.
Failed State
Aborted – The state when the transaction has been completely rolled back
after restoring the failure database
if a transaction fails during execution then the transaction goes into a failed
state. The changes made into the local memory (or buffer) are rolled back to
the previous consistent state and the transaction goes into aborted state
from the failed state.
This schedule determines the exact order of operations that are going to be
performed on database. In this example, all the instructions of transaction
T1 are executed before the instructions of transaction T2, however this is
not always necessary and we can have various types of schedules which we
will discuss in this article.
T1 T2
---- ----
R(X)
W(X)
R(Y)
R(Y)
R(X)
W(Y)
Serial Schedule
In Serial schedule, a transaction is executed completely before starting the
execution of another transaction. In other words, you can say that in serial
schedule, a transaction does not start execution until the currently running
transaction finished execution. This type of execution of transaction is also known
as non-interleaved execution. The example we have seen above is the serial
schedule.
T1 T2
---- ----
R(A)
R(B)
W(A)
commit
R(B)
R(A)
W(B)
commit
Strict Schedule
In Strict schedule, if the write operation of a transaction precedes a
conflicting operation (Read or Write operation) of another transaction
then the commit or abort operation of such transaction should also
precede the conflicting operation of other transaction.
Ta Tb
----- -----
R(X)
R(X)
W(X)
commit
W(X)
R(X)
commit
Cascadeless Schedule
In Cascadeless Schedule, if a transaction is going to perform read
operation on a value, it has to wait until the transaction who is
performing write on that value commits.
Cascadeless Schedule example
For example, lets say we have two transactions Ta and Tb. Tb is going
to read the value X after the W(X) of Ta then Tb has to wait for the
commit operation of transaction Ta before it reads the X.
Ta Tb
----- -----
R(X)
W(X)
W(X)
commit
R(X)
W(X)
commit
Recoverable Schedule
In Recoverable schedule, if a transaction is reading a value which has
been updated by some other transaction then this transaction can
commit only after the commit of other transaction which is updating
value.
Ta Tb
----- -----
R(X)
W(X)
R(X)
W(X)
R(X)
commit
commit
DBMS Serializability
When multiple transactions are running concurrently then there is a
possibility that the database may be left in an inconsistent state.
Serializability is a concept that helps us to check which schedules are
serializable. A serializable schedule is the one that always leaves the
database in consistent state.