dbms unit 4
dbms unit 4
1. Serial Schedule
The serial schedule is a type of schedule where one transac on is
executed completely before star ng another transac on. In the serial
schedule, when the first transac on completes its cycle, then the
next transac on is executed.
For example: Suppose there are two transac ons T1 and T2 which
have some opera ons. If it has no interleaving of opera ons, then
there are the following two possible outcomes:
1. Execute all the opera ons of T1 which was followed by all the
opera ons of T2.
2. Execute all the opera ons of T1 which was followed by all the
opera ons of T2.
o In the given (a) figure, Schedule A shows the serial schedule
where T1 followed by T2.
o In the given (b) figure, Schedule B shows the serial schedule
where T2 followed by T1.
2. Non-serial Schedule
o If interleaving of opera ons is allowed, then there will be non-
serial schedule.
o It contains many possible orders in which the system can
execute the individual opera ons of the transac ons.
o In the given figure (c) and (d), Schedule C and Schedule D are
the non-serial schedules. It has interleaving of opera ons.
3. Serializable schedule
o The serializability of schedules is used to find non-serial
schedules that allow the transac on to execute concurrently
without interfering with one another.
o It iden fies which schedules are correct when execu ons of the
transac on have interleaving of their opera ons.
o A non-serial schedule will be serializable if its result is equal to
the result of its transac ons executed serially.
here,
Schedule A and Schedule B are serial schedule.
Schedule C and Schedule D are Non-serial schedule.
Tes ng of Serializability
Serializa on Graph is used to test the Serializability of a
schedule.
Assume a schedule S. For S, we construct a graph known as
precedence graph. This graph has a pair G = (V, E), where V consists a
set of ver ces, and E consists a set of edges. The set of ver ces is
used to contain all the transac ons par cipa ng in the schedule. The
set of edges is used to contain all edges Ti ->Tj for which one of the
three condi ons holds:
1. Create a node Ti → Tj if Ti executes write (Q) before Tj executes
read (Q).
2. Create a node Ti → Tj if Ti executes read (Q) before Tj executes
write (Q).
3. Create a node Ti → Tj if Ti executes write (Q) before Tj executes
write (Q).
T1 T2
Read(A)
Write(A)
Read(B)
Write(B) Read(A)
Write(A)
Read(B)
Write(B)
View Serializability :-
o A schedule will view serializable if it is view equivalent to a
serial schedule.
o If a schedule is conflict serializable, then it will be view
serializable.
o The view serializable which does not conflict serializable
contains blind writes.
View Equivalent :-
Two schedules S1 and S2 are said to be view equivalent if they sa sfy
the following condi ons:
1. Ini al Read
An ini al read of both schedules must be the same. Suppose two
schedule S1 and S2. In schedule S1, if a transac on T1 is reading the
data item A, then in S2, transac on T1 should also read A.
Above two schedules are view equivalent because Ini al read
opera on in S1 is done by T1 and in S2 it is also done by T1.
2. Updated Read
In schedule S1, if Ti is reading A which is updated by Tj then in S2
also, Ti should read A which is updated by Tj.
Adver sement
Above two schedules are not view equal because, in S1, T3 is reading
A updated by T2 and in S2, T3 is reading A updated by T1.
3. Final Write
A final write must be the same between both the schedules. In
schedule S1, if a transac on T1 updates A at last then in S2, final
writes opera ons should also be done by T1.
Schedule S
With 3 transac ons, the total number of possible schedule
1. = 3! = 6
2. S1 = <T1 T2 T3>
3. S2 = <T1 T3 T2>
4. S3 = <T2 T3 T1>
5. S4 = <T2 T1 T3>
6. S5 = <T3 T1 T2>
7. S6 = <T3 T2 T1>
Taking first schedule S1:
Schedule S1
Step 1: final upda on on data items
In both schedules S and S1, there is no read except the ini al read
that's why we don't need to check that condi on.
Step 2: Ini al Read
The ini al read opera on in S is done by T1 and in S1, it is also done
by T1.
Step 3: Final Write
The final write opera on in S is done by T3 and in S1, it is also done
by T3. So, S and S1 are view Equivalent.
The first schedule S1 sa sfies all three condi ons, so we don't need
to check another schedule.
Hence, view equivalent serial schedule is:
Recoverability of Schedule :-
Some mes a transac on may not execute completely due to a
so ware issue, system crash or hardware failure. In that case, the
failed transac on has to be rollback. But some other transac on may
also have used value produced by the failed transac on. So we also
have to rollback those transac ons.
The above table 1 shows a schedule which has two transac ons. T1
reads and writes the value of A and that value is read and wri en by
T2. T2 commits but later on, T1 fails. Due to the failure, we have to
rollback T1. T2 should also be rollback because it reads the value
wri en by T1, but T2 can't be rollback because it already commi ed.
So this type of schedule is known as irrecoverable schedule.
Irrecoverable schedule: The schedule will be irrecoverable if Tj reads
the updated value of Ti and Tj commi ed before Ti commit.
The above table 2 shows a schedule with two transac ons.
Transac on T1 reads and writes A, and that value is read and wri en
by transac on T2. But later on, T1 fails. Due to this, we have to
rollback T1. T2 should be rollback because T2 has read the value
wri en by T1. As it has not commi ed before T1 commits so we can
rollback transac on T2 as well. So it is recoverable with cascade
rollback.
Recoverable with cascading rollback: The schedule will be
recoverable with cascading rollback if Tj reads the updated value of
Ti. Commit of Tj is delayed ll commit of Ti.
The above Table 3 shows a schedule with two transac ons.
Transac on T1 reads and write A and commits, and that value is read
and wri en by T2. So this is a cascade less recoverable schedule.
Failure Classifica on :-
To find that where the problem has occurred, we generalize a
failure into the following categories:
1. Transac on failure
2. System crash
3. Disk failure
1. Transac on failure
The transac on failure occurs when it fails to execute or when it
reaches a point from where it can't go any further. If a few
transac on or process is hurt, then this is called as transac on
failure.
Reasons for a transac on failure could be -
1. Logical errors: If a transac on cannot complete due to
some code error or an internal error condi on, then the
logical error occurs.
2. Syntax error: It occurs where the DBMS itself terminates
an ac ve transac on because the database system is not
able to execute it. For example, The system aborts an
ac ve transac on, in case of deadlock or resource
unavailability.
2. System Crash
o System failure can occur due to power failure or other
hardware or so ware failure. Example: Opera ng system
error.
Fail-stop assump on: In the system crash, non-vola le storage is
assumed not to be corrupted.
3. Disk Failure
o It occurs where hard-disk drives or storage drives used to
fail frequently. It was a common problem in the early days
of technology evolu on.
o Disk failure occurs due to the forma on of bad sectors,
disk head crash, and unreachability to the disk or any
other failure, which destroy all or part of disk storage.
Log-Based Recovery :-
o The log is a sequence of records. Log of each transac on is maintained in some s
storage so that if any failure occurs, then it can be recovered from there.
o If any opera on is performed on the database, then it will be recorded in the log
o But the process of storing the logs should be done before the actual transac on
in the database.
Let's assume there is a transac on to modify the City of a student. The following logs
for this transac on.
When the transac on is ini ated, then it writes 'start' log.
1. <Tn, Start>
When the transac on modifies the City from 'Noida' to 'Bangalore', then another log
to the file.
1. <Tn, City, 'Noida', 'Bangalore' >
When the transac on is finished, then it writes another log to indicate the end of the
transac on.
1. <Tn, Commit>
There are two approaches to modify the database:
1. Deferred database modifica on:
o The deferred modifica on technique occurs if the transac on does not modify t
database un l it has commi ed.
o In this method, all the logs are created and stored in the stable storage, and the
is updated when a transac on commits.
T1 T2
A : = A - 200 C : = C - 100
Read (B)
B := B + 200
Write (B)
The order in which these transac ons will be stored in system log will be discussed b
o When a transac on starts a transac on start record is wri en in the system log,
when transac on starts T1, a start record is wri en on the log. Similarly, when a
transac on T2 starts a start record is wri en in the system log.
o When a write opera on is performed, a write log record is wri en which contain
data given earlier. So when transac on T1 performs a Write (A) opera on, a writ
[Write, T1, A, 800] is wri en on the system log. One should note at this point, updates
wri en to disk. Similarly on execu on of Write (B) opera on by transac on T1 and Wr
opera on by transac on T2, appropriate write log records are wri en.
Adver sement
o When the transac on is commi ed, a commit record is wri en to the system log
transac on T1 is about to be commi ed, a transac on commit record is wri en
system log.
[Commit, T1]
All log records for the disk and transac on is commi ed.Similarly for transac on T2 is
be commi ed, a transac on commit record is wri en to the system log.
The full contents of the system log when these two transac ons (T1 and T2) are execu
sequence T1 and T2 are shown below:
Log Database
[Write , T1 , A, 800]
[Write , T1 , B, 1700]
B = 1700
[Write , T2 , C, 1900]
[Commit , T2]
C = 1900
Let's take an example where a system crashes before a transac on completes. So the
handles failure that result is loss of informa on using the log. In case of failure, the lo
examined star ng with the last entry ll the most recent checkpoint. All transac on s
transac on commit records before the abort record should be redone. The redo meth
performs all the writes to the database in the order they were wri en to the log. If th
has already been performed then the write opera on has no effect on the data item w
redone. This guarantees that all data items will be updated properly. All those transac
only contains start record but no commit record need to be restarted.
The figure below shows different situa ons when a system crash occurs:
The redo opera on must be mandatory in a deferred update, meaning running it repe
the same as running it only once. If the system fails during the recovery process, a suc
recovery a empts may redo some of the write opera ons that were already redone d
first recovery process, as if the recovery opera ons were restarted from the beginning
the redo write value is currently in the database, the result of a successful second rec
process on redo is the same as if the redo had succeeded the first me.
Condi on 1 Condi on 2 Condi on 3
[abort, T1]
crash
crash
Redo (T1)
No Redo Redo (T1) , Redo (T1
T2 must be re-executed
Log Database
A = 800
B = 1700
[Commit, T1]
C = 1900
[Commit , T2]
Now shall examine the different condi ons when a failure occurs.
[abort, T1]
crash
crash
Redo (T1)
Undo (T1) Redo (T1) , Redo (T2
Undo (T2)
Fullscreen
o The checkpoint is a type of mechanism where all the previous
logs are removed from the system and permanently stored in
the storage disk.
o The checkpoint is like a bookmark. While the execu on of the
transac on, such checkpoints are marked, and the transac on
is executed then using the steps of the transac on, the log files
will be created.
o When it reaches to the checkpoint, then the transac on will be
updated into the database, and ll that point, the en re log file
will be removed from the file. Then the log file is updated with
the new step of transac on ll next checkpoint and so on.
o The checkpoint is used to declare a point before which the
DBMS was in the consistent state, and all transac ons were
commi ed.
When a system issues a checkpoint, the following opera ons are
performed:
o The log informa on from the buffers in the vola le storage is
copied to log on the stable storage.
o All database updates from the buffers in the vola le storage are
wri en to the disk.
o Write a checkpoint record to log on the disk so subsequent
database recovery coordinated with the checkpoint.
Recovery using Checkpoint
In the following manner, a recovery system recovers the database
from this failure:
o The recovery system reads log files from the end to start. It
reads log files from T4 to T1.
o Recovery system maintains two lists, a redo-list, and an undo-
list.
o The transac on is put into redo state if the recovery system
sees a log with <Tn, Start> and <Tn, Commit> or just <Tn,
Commit>. In the redo-list and their previous list, all the
transac ons are removed and then redone before saving their
logs.
o For example: In the log file, transac on T2 and T3 will have <Tn,
Start> and <Tn, Commit>. The T1 transac on will have only <Tn,
commit> in the log file. That's why the transac on is commi ed
a er the checkpoint is crossed. Hence it puts T1, T2 and T3
transac on into redo list.
Deadlock in DBMS :-
A deadlock is a condi on where two or more transac ons are wai ng
indefinitely for one another to give up locks. Deadlock is said to be
one of the most feared complica ons in DBMS as no task ever gets
finished and is in wai ng state forever.
For example: In the student table, transac on T1 holds a lock on
some rows and needs to update some rows in the grade table.
Simultaneously, transac on T2 holds locks on some rows in the grade
table and needs to update the rows in the Student table held by
Transac on T1.
Now, the main problem arises. Now Transac on T1 is wai ng for T2
to release its lock and similarly, transac on T2 is wai ng for T1 to
release its lock. All ac vi es come to a halt state and remain at a
stands ll. It will remain in a stands ll un l the DBMS detects the
deadlock and aborts one of the transac ons.
Deadlock Preven on
o Deadlock preven on method is suitable for a large database. If
the resources are allocated in such a way that deadlock never
occurs, then the deadlock can be prevented.
o The Database management system analyzes the opera ons of
the transac on whether they can create a deadlock situa on or
not. If they do, then the DBMS never allowed that transac on
to be executed.
Each transac on has unique iden fier which is called mestamp. It is
usually based on the state of the transac on and assigned once the
transac on is started. For example if the transac on T1 starts before
the transac on T2 then the mestamp corresponding to the
transac on T1 will be less than mestamp corresponding to
transac on T2. The mestamp decides whether a transac on should
wait or abort and rollback. Aborted transac on retain their
mestamps values and hence the seniority.
The following deadlock preven on schemes using mestamps have
been proposed.
o Wait-Die scheme
o Wound wait scheme
The significant disadvantage of both of these techniques is that some
transac ons are aborted and restarted unnecessarily even though
those transac ons never actually cause a deadlock.
Wait-Die scheme
In this scheme, if a transac on requests for a resource which is
already held with a conflic ng lock by another transac on then the
DBMS simply checks the mestamp of both transac ons. It allows
the older transac on to wait un l the resource is available for
execu on.
Let's assume there are two transac ons Ti and Tj and let TS(T) is a
mestamp of any transac on T. If T2 holds a lock by some other
transac on and T1 is reques ng for resources held by T2 then the
following ac ons are performed by DBMS:
1. Check if TS(Ti) < TS(Tj) - If Ti is the older transac on and Tj has
held some resource, then Ti is allowed to wait un l the data-
item is available for execu on. That means if the older
transac on is wai ng for a resource which is locked by the
younger transac on, then the older transac on is allowed to
wait for resource un l it is available.
2. Check if TS(Ti) < TS(Tj) - If Ti is older transac on and has held
some resource and if Tj is wai ng for it, then Tj is killed and
restarted later with the random delay but with the same
mestamp.
If T1 is allowed to
Transac on Timestamp
T1 5
T2 7
T3 9
If T1 is allowed to
Transac on Timestamp
T1 5
T2 7
T3 9
The number of ab
The number of aborts and
3. rollback transac
rollback is higher.
in this scheme.
As the transac o
A transac on T1 aborts and greater is the pro
rollback because retrieving a data
transac ons T2 contain the older would force
4. requested data items so T1 abor on of any y
can reissue the same transac ons that
transac on sequence of item it needs but
requests when started again. be aborted by tra
older then itself.