Lock based protocols and recovery - short form
Lock based protocols and recovery - short form
S X
S TRUE False
X False False
Lock compatibility table
Unlock(Q) =>Lock on data item Q is released
Granting of locks:
If a transaction have a Lock – S(Q), then Lock – S(Q) request by other transactions can
be granted but Lock – X(Q) can’t be granted due to incompatibility => Lead to starvation
Starvation can be avoided by concurrency control manager
It is not necessary that all the lock must be unlocked only at end of transaction.
Unlock can be done after the Lock point.
Lock Point: The point where a transaction obtain a final lock is called lock point.(i.e.,)
End of growing phase.
1
2-phase locking does not ensure freedom from dead lock.
Here T3 and T4 are in dead lock state and can’t proceed further
T3 T4
Lock-X(B)
Read(B)
B:=B-50
Write(B)
Lock-S(A)
Read(A)
Lock-S(B)
Lock – X(A)
T5 T6 T7
Lock-X(A)
Read(A)
Write(A)
Unlock(A)
Lock-X(A)
Read(A)
Write(A)
Unlock(A)
Lock-X(A)
Read(A)
abort
Schedule with cascading rollback
Strict 2-phase locking protocol:
Cascading roll back is avoided by modification of 2-phase locking protocol called
strict 2-phase locking protocol
In this protocol, all the Exclusive locks obtained by the transaction is released only after the
transaction commits.
Upgrade(A1) is possible only when T9 unlock the shared lock on data item A1.
2
iii) Graph based protocols:
Consider, Set of data items D = {d1,d2,…………,dn}
If di dj, then any transaction must access di before accessing dj.
In a tree based protocol, only Exclusive locks are allowed.
Each transaction Ti can lock a data item atmost once and must follow the below rules.
The first lock by Ti may be on any data item
Subsequently, the data item Q can be locked by Ti, only if the parent of Q is currently
locked by Ti.
Data items may be unlocked at any time
T1 T2 T3
Lock-X(B)
Lock-X(E)
Lock-X(D)
Lock-X(G)
Unlock(D)
Lock-X(A)
Unlock(B)
Unlock(E)
Lock-X(B)
Unlock(A)
Unlock(B)
Unlock(G)
3
2. Explain recovery mechanisms in detail. (or) Explain recovery and atomicity in detail (or)
Explain how database is recovered from failures. (or) Explain recovery algorithms in detail
Recovery scheme must provide high availability, such that it must minimize the time to
recover from the failure.
LOG BASED RECOVERY:
The log is a sequence of log records, recording all update activities on the database.
When transaction Ti has started, it writes a log record <Ti, start>.
o When Ti perform write on data item X, update log record <Ti, X, V1, V2> is written to log.
Where Ti is a transaction which performs update, X is a data item being updated,
V1 is the old value of X and V2 is a new value of X.
When transaction Ti has committed, it writes a log record <Ti, commit>.
When transaction Ti has aborted, it writes a log record <Ti, abort>.
Two approaches using logs
i) Deferred database modification
ii) Immediate database modification
i) Deferred Database Modification
Transaction modifies the database only after it has committed is called Deferred
database modification
Assume that transactions execute serially
Transaction starts by writing <Ti start>record to log.
Write(X) operation results in a log record <Ti, X, V>,where V is the new value of X.
Note: old value is not needed for this scheme.
o New value of X is written to database only after <Ti,commit>
In case of system crash, Ti can be redone only if both <Ti start> and<Ti commit> are in log.
o Redo(Ti) => Data items will have new values
Below we show the log when it appears at three instances of time. If system crash
occurs at this instance,
Checkpoints:
Check pointing is performed periodically to Streamline the recovery procedure
< checkpoint L> , where L is a list of all transactions active at the time of checkpoint.
Scan backwards from the point where system crash occurs.
Only transactions that are in L or started after the checkpoint need to be redone or undone
T1 can be ignored
T2 and T3 can be redone
T4 is undone
5
Example for recovery using checkpoint:
<T1,start>
<T1,A,1000,950>
<T1,commit>
<T2,start>
Checkpoint<T2>
<T2,B,800,850>
<T2,commit>
<T3,start>
<T3,C,1000,950>
<T3,commit>
<T4,start>
<T4,D,500,550>
System crashes
Here, T2,T3 can be redone and T4 can be undone
Recovery Algorithm:
Recovery algorithms are techniques to ensure database consistency and transaction
atomicity and durability despite failures.
i) Transaction rollback (during normal operation)
Let Ti be the transaction to be rolled back
If Update log record is <Ti,X,V1,V2>
o Old value V1 is written to X
ii) Recovery from failure
Checkpointing is done and Only the transactions that are in L or started after the
checkpoint need to be redone or undone
Two phases
Redo phase:
o Let Ti be the transaction to be rolled back
o If Update log record is <Ti,X,V1,V2>
New value V1 is written to X
Undo phase:
o Let Ti be the transaction to be rolled back
o If Update log record is <Ti,X,V1,V2>
Old value V1 is written to X
o Whenever <Ti , start>is found, add Ti to undo-list
o Whenever a log record <Ticommit> or <Tiabort>is found, remove Tifrom undo-
list