0% found this document useful (0 votes)
19 views

Lock based protocols and recovery - short form

Uploaded by

madhanrajpun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Lock based protocols and recovery - short form

Uploaded by

madhanrajpun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Unit - 5

1. EXPLAIN VARIOUS LOCKING PROTOCOLS IN DETAIL.


(OR) EXPLAIN TWO PHASE LOCKING PROTOCOL IN DETAIL.
i) Locks:
 A lock is a mechanism to control concurrent access to a data item.
 Data items can be locked in two modes : Shared mode , Exclusive mode
 Shared lock : Transaction Ti can read but cannot write Q
o Lock – S(Q) denotes shared lock on data item Q
 Exclusive lock: Ti can both read and write Q.
o Lock – X(Q) denotes exclusive lock on data item Q

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

ii) Two-phase locking protocol:


 It ensures serializability.
 Each transaction obtains lock and release lock in two phases.
 Growing phase- a transaction may obtain locks but may not release any lock.
 Shrinking phase- a transaction may release lock but may not obtain any new lock
Schedule under 2-phase locking:
T1 T2
Lock-X(B)
Read(B)
Write(B)
Lock-X(A)
Read(A)
Write(A)
Unlock(A)
Unlock(B)
Lock-S(A)
Read(A)
Lock-S(B)
Read(B)
Unlock(B)
Unlock(A)

 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)

Schedule with deadlock


 In 2-phase locking, Cascading roll back may also occur.
 Failure of T5 will make T6 and T7 also to roll back in addition to T5.

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.

Rigorous 2-phase locking protocol:


 In this protocol, all the locks obtained by the transaction is held until the transaction commits.
Lock conversions:
Upgrade =>Conversion of shared lock to exclusive lock. It takes place only in the growing phase.
Downgrade => Conversion of Exclusive lock to Share lock. It takes place only in the Shrinking phase.
T8 T9
Lock-S(A1)
.
. Lock-S(A1)
Lock-S(An)

Upgrade(A1) => It is not possible since A1 is locked by T9 in shared


mode

 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

Serializable schedule under tree protocol

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)

 Example: To access data items A and J, it need to lock B,D,H.

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,

(a) No redo actions since T0 is not committed


(b) redo(T0) must be performed since <T0, commit> is present
(c) redo(T0) must be performed followed by redo(T1) since both <T0, commit> and
<Ti , commit> are present
4
ii) Immediate Database Modification
 Transaction modifies the database even before it commits. (i.e) Before <Ti, commit>
 Since undoing may be needed, update logs must have both old value and new value.
 (i.e) Update log record <Ti, X,V1,V2> is used
Recovery procedure has two operations:
 undo(Ti) => Data items will have old values.
 redo(Ti) => Data items will have new values
When recovering after failure:
 Transaction Ti will be undone if Ti is started but not committed.
 Transaction Ti needs to be redone if Ti is started and committed
Example:
 Consider T0 = Transfers Rs.50 from account A to account B
 T1 = Withdraw 100 from account C
Log Database
<T0,start>
<T0,A,1000,950>
<T0,B,2000,2050>
A=950
B=2050
<T0, commit>
<T1,start>
<T0,C,700,600>
C=600
<T1,commit>

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

You might also like