Understanding Transaction Processing in DBMS
Understanding Transaction Processing in DBMS
Transaction States
These are different types of Transaction States:
Active State – When the instructions of the transaction are running then the transaction is in active state. If all
the ‘read and write’ operations are performed without any error then it goes to the “partially committed state”; if
any instruction fails, it goes to the “failed state”.
Partially Committed – In the partially committed state, a transaction executes its final operation, but the data is
still not saved to the database.
Failed State –When any instruction of the transaction fails, it goes to the “failed state” or if failure occurs in
making a permanent change of data on Database.
Aborted State –After having any type of failure, the transaction goes from “failed state” to “aborted state” and
since in previous states, the changes are only made to local buffer or main memory and hence these changes
Prepared By- Charu Kavadia Page 1
Database Management System Unit 4
Transaction Properties
Transactions access data using two operations:
read(X), which transfers the data item X from the database to a variable, also called X, in a buffer in
main memory belonging to the transaction that executed the read operation.
write(X), which transfers the value in the variable X in the main-memory buffer of the transaction that
executed the write to the data item X in the database.
Example- A simple bank application consisting of several accounts and a set of transactions that access and
update those accounts. Let Ti be a transaction that transfers $50 from account A to account B. This transaction
can be defined as:
Prepared By- Charu Kavadia Page 2
Database Management System Unit 4
inconsistent state.
For example, as we saw earlier, the database is temporarily inconsistent while the transaction to transfer funds
from A to B is executing, with the deducted total written to A and the increased total yet to be written to B. If a
second concurrently running transaction reads A and B at this intermediate point and computes A + B, it will
observe an inconsistent value. Furthermore, if this second transaction then performs updates on A and B based
on the inconsistent values that it read, the database may be left in an inconsistent state even after both
transactions have completed.
A way to avoid the problem of concurrently executing transactions is to execute transactions serially—that is,
one after the other.
4) Durability: Once a transaction completes successfully, the changes it has made into the database should be
permanent even if there is a system failure. The recovery management component of database systems ensures
the durability of transaction. Once the execution of the transaction completes successfully, and the user who
initiated the transaction has been notified that the transfer of funds has taken place, it must be the case that no
system failure can result in a loss of data corresponding to this transfer of funds. The durability property
guarantees that, once a transaction completes successfully, all the updates that it carried out on the database
persist, even if there is a system failure after the transaction completes execution.
We can guarantee durability by ensuring that either:
1. The updates carried out by the transaction have been written to disk before the transaction completes.
2. Information about the updates carried out by the transaction is written to disk, and such information is
sufficient to enable the database to reconstruct the updates when the database system is restarted after the
failure.
Concurrent Execution
A schedule is a collection of many transactions which is implemented as a unit.
Depending upon how these transactions are arranged in within a schedule, a schedule can be of two types:
Serial: The transactions are executed one after another, in a non-pre-emptive (which cannot be stopped)
manner.
Concurrent: The transactions are executed in a pre-emptive (which can be stopped), time shared method.
In Serial schedule, there is no question of sharing a single data item among many transactions, because not more
than a single transaction is executing at any point of time. However, a serial schedule is inefficient in the sense
that the transactions suffer for having a longer waiting time and response time, as well as low amount of
resource utilization.
In concurrent schedule, CPU time is shared among two or more transactions in order to run them concurrently.
However, this creates the possibility that more than one transaction may need to access a single data item for
read /write purpose and the database could contain inconsistent value if such accesses are not handled properly.
Example-
Let us consider there are two transactions T1 and T2, whose instruction sets are given as following.
T1 is an example where $100 is transferred from A to B.
T2 is transaction which deposits to account C 10% of the amount in account A.
T1
Read A; A=500
A = A – 100; A=400
Write A;
Read B;
B = B + 100;
Write B;
T2
Read A; A=400
Temp = A * 0.1; Temp=40
Read C;
C = C + Temp; =C+40
Write C;
If we prepare a serial schedule, then either T1 will completely finish before T2 can begin, or T2 will completely
finish before T1 can begin. However, if we want to create a concurrent schedule, then some Context Switching
need to be made, so that some portion of T1 will be executed, then some portion of T2 will be executed and so
on. For example say we have prepared the following concurrent schedule.
T1 T2
Read A; A=500
A = A - 100; A=400
Write A; A=400
Read A; A=400
Temp = A * 0.1; Temp=40
Read C;
C = C + Temp; =C + 40
Write C;
Read B; B=500
B = B + 100; B=600
Write B; B=600
No problem here. We have made some Context Switching in this Schedule, the first one after executing the
third instruction of T1, and after executing the last statement of T2. T1 first deducts Rs 100/- from A and writes
the value to C. The new value of Rs 400/- into A. T2 reads the value of A, calculates the value of Temp to
be Rs 40/- and adds remaining part of T1 is executed and Rs 100/- is added to B.
It is clear that a proper Context Switching is very important in order to maintain the Consistency and Isolation
properties of the transactions. But let us take another example where a wrong Context Switching can bring
about disaster. Consider the following example involving the same T1 and T2.
T1 T2
Read A; A=500
A = A - 100; A=400
Read A; A=500
Temp = A * 0.1; Temp=50
Read C;
C = C + Temp; C+50
Write C;
Write A; A= 400
Read B; B= 500
B = B + 100; B=600
Write B; B=600
This schedule is wrong, because we have made the switching at the second instruction of T1. The result is very
confusing. If we consider accounts A and B both containing Rs 1000/- each, then the result of this schedule
should have left Rs 400/- in A, Rs 600/- in B and add Rs 50 in C (as C should be increased by 10% of the
amount in A). But in this wrong schedule, the Context Switching is being performed before the new value of Rs
400/- has been updated in A. T2 reads the old value of A, which is still Rs 500/-, and deposits Rs 50/- in C. C
makes an unjust gain of Rs 10/- out of nowhere.
Schedule
A schedule is defined as an execution sequence of transactions. A schedule maintains the order of the operation
in each individual transaction. A schedule is the arrangement of transaction operations. A schedule may contain
a set of transactions. We already know that a transaction is a set of operations. To run transactions concurrently,
we arrange or schedule their operations in an interleaved fashion.
Schedules are divided into following categories-
Serial Schedule
Non-serial Schedule
Serializable Schedule
Conflict Serializable Schedule
View Serializable Schedule
Non-Serializable Schedule
Recoverable Schedule
Cascading Schedule
Cascadeless Schedule
Strict Schedule
Non-Recoverable Schedule
1. Serial Schedules: Schedules in which the transactions are executed non-interleaved, i.e., a serial schedule is
one in which no transaction starts until a running transaction has ended are called serial schedules. i.e., 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.
Example: Consider the following schedules (A and B) each hold two transactions T1 and T2. In Serial schedule
two transactions execute separately. They cannot execute at the same time as given below.
These are serial schedules since the transactions perform serially in the order T1 —> T2 or T2 —> T1.
Prepared By- Charu Kavadia Page 7
Database Management System Unit 4
The Non-Serial Schedule can be divided further into, Serializable and Non-Serializable Schedule.
a) Serializable Schedule: This is used to maintain the consistency of the database. It is mainly used in the Non-
Serial scheduling to verify whether the scheduling will lead to any inconsistency or not. On the other hand, a
serial schedule does not need the serializability because it follows a transaction only when the previous
transaction is complete. The non-serial schedule is said to be in a serializable schedule only when it is
equivalent to the serial schedules, for an n number of transactions. Since concurrency is allowed in this case
thus, multiple transactions can execute concurrently.
reads the old value of the data item and after the reading is over, the new value of the data item is
written. It the write instruction is performed first, then it updates the data item with the new value and
the read instruction reads the newly updated value. If both the transactions are for write operation, then
they are in conflict but can be allowed to take place in any order, because the transaction do not read
the value updated by each other. However, the value that persists in the data item after the schedule is
over is the one written by the instruction that performed the last write. )
Example 1-
We finally got a serial schedule after swapping all the non-conflicting operations so we can say that the given
schedule is Conflict Serializable.
Example 2-
To convert this schedule into a serial schedule we must have to swap the R(A) operation of transaction T2 with
the W(A) operation of transaction T1. However we cannot swap these two operations because they are
conflicting operations, thus we can say that this given schedule is not Conflict Serializable.
2. View Serializable: A Schedule is called view serializable if it is view equivalent to a serial schedule (no
overlapping transactions).
View Equivalent-
Two schedules T1 and T2 are said to be view equivalent, if they satisfy all the following conditions:
1. Initial Read: Initial read of each data item in transactions must match in both schedules. For example, if
transaction T1 reads a data item X before transaction T2 in schedule S1 then in schedule S2, T1 should read X
before T2.
Read v/s Initial Read: Initial read means the first read operation on a data item, for example, a data item X can
be read multiple times in a schedule but the first read operation on X is called the initial read.
2. Final Write: Final write operations on each data item must match in both the schedules. For example, a data
item X is last written by Transaction T1 in schedule S1 then in S2, the last write operation on X should be
performed by the transaction T1.
3. Update Read: If in schedule S1, the transaction T1 is reading a data item updated by T2 then in schedule S2,
T1 should read the value after the write operation of T2 on same data item. For example, In schedule S1, T1
performs a read operation on X after the write operation on X by T2 then in S2, T1 should read the X after T2
performs write on X.
Example-
on Y is performed by T1.
We checked for both data items X & Y and the initial read condition is satisfied in S1 & S2.
Final Write
In schedule S1, the final write operation on X is done by transaction T2. In S2 also transaction T2 performs the
final write on X.
Lets check for Y. In schedule S1, the final write operation on Y is done by transaction T2. In schedule S2, final
write on Y is done by T2.
We checked for both data items X & Y and the final write condition is satisfied in S1 & S2.
Update Read
In S1, transaction T2 reads the value of X, written by T1. In S2, the same transaction T2 reads the X after it is
written by T1.
In S1, transaction T2 reads the value of Y, written by T1. In S2, the same transaction T2 reads the value of Y
after it is updated by T1.
The update read condition is also satisfied for both the schedules.
Result: Since all the three conditions that checks whether the two schedules are view equivalent are satisfied in
this example, which means S1 and S2 are view equivalent. Also, as we know that the schedule S2 is the serial
schedule of S1, thus we can say that the schedule S1 is view serializable schedule.
Note- all conflict serializable schedules can be view serializable, but all view serializable schedules may or may
not be conflict serializable.
If a precedence graph contains a single edge Ti → Tj, then all the instructions of Ti are executed before
the first instruction of Tj is executed.
If a precedence graph for schedule S contains a cycle, then S is non-serializable. If the precedence graph
has no cycle, then S is known as serializable.
Example 1:
Explanation:
Read(A): In T1, no subsequent writes to A, so no new edges
Read(B): In T2, no subsequent writes to B, so no new edges
Read(C): In T3, no subsequent writes to C, so no new edges
The precedence graph for schedule S1 contains a cycle that's why Schedule S1 is non-serializable.
Example 2:
Explanation:
Read(A): In T4,no subsequent writes to A, so no new edges.
Read(C): In T4, no subsequent writes to C, so no new edges.
Write(A): A is subsequently read by T5, so add edge T4 → T5.
Read(B): In T5,no subsequent writes to B, so no new edges.
Write(C): C is subsequently read by T6, so add edge T4 → T6.
Write(B): A is subsequently read by T6, so add edge T5 → T6.
Prepared By- Charu Kavadia Page 14
Database Management System Unit 4
The precedence graph for schedule S2 contains no cycle that's why ScheduleS2 is serializable.
This is a recoverable schedule since T1 commits before T2,that makes the value read by T2 correct.
Recoverable Schedule is further classified into cascading, cascadeless and strict schedules.
(i) Cascading Schedule- Also called Avoids cascading aborts/rollbacks (ACA). When there is a failure in
one transaction and this leads to the rolling back or aborting other dependent transactions, then such
scheduling is referred to as Cascading rollback or cascading abort.
Example: For example, transaction T1 writes uncommitted x that is read by Transaction [Link]
T2 writes uncommitted x that is read by Transaction T3. Suppose at this point T1 fails.T1 must be rolled
back, since T2 is dependent on T1, T2 must be rolled back, and since T3 is dependent on T2, T3 must be
rolled back.
Because of T1 rollback, all T2, T3, and T4 should also be rollback (Cascading dirty read problem).
This phenomenon, in which a single transaction failure leads to a series of transaction rollbacks is called
Cascading rollback.
Dirty Read Problem
When a Transaction reads data from uncommitted write in another transaction, then it is known as Dirty
Read. If that writing transaction failed, and that written data may updated again. Therefore, this
causes Dirty Read Problem. In other words, Reading the data written by an uncommitted transaction is
called as dirty read.
It is called as dirty read because there is always a chance that the uncommitted transaction might roll back
later. Thus, uncommitted transaction might make other transactions read a value that does not even exist.
This leads to inconsistency of the database.
For example, let’s say transaction 1 updates a row and leaves it uncommitted, meanwhile, Transaction 2
reads the updated row. If transaction 1 rolls back the change, transaction 2 will have read data that is
considered never to have existed.
There will not be any Dirty Read problem, if a transaction is reading from another committed transaction.
So, no rollback is required.
(ii) 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. That means
there must not be Dirty Read. Because Dirty Read Problem can cause Cascading Rollback, which is
inefficient.
or
When a transaction is not allowed to read data until the last transaction which has written it is committed
or aborted, these types of schedules are called cascadeless schedules.
Example-
Here, the updated value of X is read by transaction T2 only after the commit of transaction T1. Hence,
the schedule is cascadeless schedule.
Cascadeless Schedule avoids cascading aborts/rollbacks (ACA). Schedules in which transactions read
values only after all transactions whose changes they are going to read commit are called cascadeless
schedules. Avoids that a single transaction abort leads to a series of transaction rollbacks .A strategy to
prevent cascading aborts is to disallow a transaction from reading uncommitted changes from another
transaction in the same schedule.
In other words, if some transaction Tj wants to read value updated or written by some other transaction
Ti, then the commit of Tj must read it after the commit of Ti.
Example-
Note that Cascadeless schedule allows only committed read operations. However, it allows uncommitted
write operations. Also note that Cascadeless Schedules are always recoverable, but all recoverable
transactions may not be Cascadeless Schedule.
Example: Consider the following schedule involving two transactions T1 and T2.
It is a recoverable schedule but it does not avoid cascading aborts. It can be seen that if T 1 aborts, T2 will
have to be aborted too in order to maintain the correctness of the schedule as T 2 has already read the
uncommitted value written by T 1.
(iii) Strict Schedule - If in a schedule, a transaction is neither allowed to read nor write a data item until the
last transaction that has written it is committed or aborted, then such a schedule is called as a Strict
Schedule.
In other words,
Strict schedule allows only committed read and write operations.
Clearly, strict schedule implements more restrictions than cascadeless schedule.
Prepared By- Charu Kavadia Page 18
Database Management System Unit 4
Example- Consider the following schedule involving two transactions Ta and Tb. The write operation
of transaction Ta precedes the read or write operation of transaction Tb, so the commit or abort
operation of transaction Ta should also precede the read or write of Tb. Here, transaction Tb reads and
writes the updated or written value of transaction Ta only after the transaction T1 commits. Hence, the
schedule is strict schedule.
2. Non-Recoverable Schedule: If the system doesn’t recover to a regular database state, it is considered a non-
recoverable schedule.
If in a schedule,
A transaction performs a dirty read operation from an uncommitted transaction
And commits before the transaction from which it has read the value, then such a schedule is known
as an Irrecoverable Schedule.
Example- Consider the following schedule involving two transactions T1 and T2.