0% found this document useful (0 votes)
10 views28 pages

Week 11 DBMS Online Lecture Lock Based Protocol

The document discusses concurrency control in database systems, focusing on lock-based protocols, including shared and exclusive locks, and the issues of deadlocks. It explains two-phase locking as a method to ensure conflict-serializable schedules and outlines strategies for deadlock prevention and handling. The document also covers automatic lock acquisition, lock conversions, and the conditions that lead to deadlocks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views28 pages

Week 11 DBMS Online Lecture Lock Based Protocol

The document discusses concurrency control in database systems, focusing on lock-based protocols, including shared and exclusive locks, and the issues of deadlocks. It explains two-phase locking as a method to ensure conflict-serializable schedules and outlines strategies for deadlock prevention and handling. The document also covers automatic lock acquisition, lock conversions, and the conditions that lead to deadlocks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Unit No: 1 : Concurrency Control

Lock Based Protocols


Locking

 Lock guarantee s exclusive use of data item to current


transactions
 Prevents reading inconsistent data
 Lock manager is responsible for assigning and policing the
locks used by transaction

Types of lock
 Binary locks- Lock with two states (Locked and
unlocked)
 Shared or exclusive locks

2
Shared or Exclusive locks

 Indicates the nature of locks


 Shared locks
 Concurrent transactions are granted read access on the
basis of common lock
 Exclusive lock
 Access is reserved for the transaction that locked the
object
 Three states: Unlocked , shared(read), exclusive (write)
 More efficient data access solution
 More overhead for lock manager

3
Problem with locking

 Transactions schedule may not be serializable


 Can be solved with two phase locking
 May cause deadlocks
 Dead lock is caused when two transactions wait for
each other to unlock data

4
Lock based protocol

 Locking is an operation which secures permission to read


and write data item for transactions
 Eg Lock (x)
 Unlocking: it is an operation which removes these
permission from the data item
 Eg Unlock (X)
 Lock and unlock are atomic operations

5
Shared or Exclusive locks

 Data items can be locked in two modes


 Shared (read) mode: read_lock(Q)/lock-S(Q)
 More than one transaction can apply share lock on
Q for reading its value but no write lock can be
applied on Q by any other transaction
 Exclusive (write) mode: write_lock(Q)/lock- X(Q)
 Only one write lock on Q can exist at any time and
no shared lock can be applied by any other
transaction on Q
 Conflict matrix
 Lock compatibility matrix

6
Lock-Based Protocols (Cont.)

 Lock-compatibility matrix

 A transaction may be granted a lock on an item if the requested


lock is compatible with locks already held on the item by other
transactions
 Any number of transactions can hold shared locks on an item,
 But if any transaction holds an exclusive on the item no other
transaction may hold any lock on the item.
 If a lock cannot be granted, the requesting transaction is made to
wait till all incompatible locks held by other transactions have
been released. The lock is then granted.

7
Lock-Based Protocols (Cont.)

 Example of a transaction performing locking:


T2: lock-S(A);
read (A);
unlock(A);
lock-S(B);
read (B);
unlock(B);
display(A+B)
 Locking as above is not sufficient to guarantee serializability — if A
and B get updated in-between the read of A and B, the displayed
sum would be wrong.
 A locking protocol is a set of rules followed by all transactions
while requesting and releasing locks. Locking protocols restrict the
set of possible schedules.

8
Two Phase Locking Protocol

 This protocol ensures conflict-serializable schedules.


 Phase 1: Growing Phase
 Transaction may obtain locks
 Transaction may not release locks
 Phase 2: Shrinking Phase
 Transaction may release locks
 Transaction may not obtain locks
 The protocol assures serializability. It can be proved that the
transactions can be serialized in the order of their lock points (i.e., the
point where a transaction acquired its final lock).

9
Two Phase Locking Protocol

 There can be conflict serializable schedules that cannot be obtained if


two-phase locking is used.
 However, in the absence of extra information (e.g., ordering of access
to data), two-phase locking is needed for conflict serializability in the
following sense:
 Given a transaction Ti that does not follow two-phase locking, we
can find a transaction Tj that uses two-phase locking, and a
schedule for Ti and Tj that is not conflict serializable.

1
0
Lock Conversion

 Two-phase locking with lock conversions:


– First Phase:
 can acquire a lock-S on item
 can acquire a lock-X on item
 can convert a lock-S to a lock-X (upgrade)
– Second Phase:
 can release a lock-S
 can release a lock-X
 can convert a lock-X to a lock-S (downgrade)
 This protocol assures serializability. But still relies on the
programmer to insert the various locking instructions.

1
1
Automatic Acquisition of Locks

 A transaction Ti issues the standard read/write instruction, without


explicit locking calls.
 The operation read(D) is processed as:
if Ti has a lock on D
then
read(D)
else begin
if necessary wait until no other
transaction has a lock-X on D
grant Ti a lock-S on D;
read(D)
end

1
2
Automatic Acquisition of Locks

 write(D) is processed as:


if Ti has a lock-X on D
then
write(D)
else begin
if necessary wait until no other transaction has any lock on D,
if Ti has a lock-S on D
then
upgrade lock on D to lock-X
else
grant Ti a lock-X on D
write(D)
end;
 All locks are released after commit or abort

1
3
Deadlocks
 System is deadlocked if there is a set of transactions such that every
transaction in the set is waiting for another transaction in the set
 Consider the partial schedule

Read(A)

write(A)

 Neither T3 nor T4 can make progress — executing lock-S(B) causes T4 to


wait for T3 to release its lock on B, while executing lock-X(A) causes T3 to
wait for T4 to release its lock on A.
 Such a situation is called a deadlock.
 To handle a deadlock one of T3 or T4 must be rolled back
and its locks released.
1
4
Deadlock

1
5
Conditions to Occur Deadlock

 Hold and Wait


 Mutual Exclusion
 No Preemption
 Circular Wait

1
6
Conditions to Occur Deadlock cont..

 Hold and Wait

1
7
Conditions to Occur Deadlock

 Mutual Exclusion
 It implies if two processes cannot use the same resource at the
same time

1
8
Conditions to Occur Deadlock

 Preemption means if work is complete then release the Lock


 No Preemption
 Even though Work is completed it don’t release the Lock
 One process has obtained a resource a system cannot remove it
from the process control until the process has finished using the
resoure

1
9
Conditions to Occur Deadlock

 Circular Wait
 All the processes must be waiting for the resources in a cyclic
manner so that the last process is waiting for the resource
which is being held by the first process

T1

T2
T4

T3

2
0
Problems of Deadlocks

 Two-phase locking does not ensure freedom from deadlocks.


 In addition to deadlocks, there is a possibility of starvation.
 Starvation occurs if the concurrency control manager is badly
designed. For example:
 A transaction may be waiting for an X-lock on an item, while a
sequence of other transactions request and are granted an S-
lock on the same item.
 The same transaction is repeatedly rolled back due to
deadlocks.
 Concurrency control manager can be designed to prevent starvation.

2
1
Deadlocks avoidance and Handling

 Whenever a database is stuck in a deadlock it’s better to abort the


transactions that are resulting deadlock but this is a waste of
resources and time

 Deadlock avoidance mechanism used to detect any deadlock


situation in advance by using some techniques like WEIGHTS FOR
GRAPH but this wait for graph method is suitable only for smaller
databases, for large databases other prevention techniques are used

2
2
Deadlocks avoidance and Handling

 The potential for deadlock exists in most locking protocols.


Deadlocks are a necessary evil.

 When a deadlock occurs there is a possibility of cascading roll-backs.

 Cascading roll-back is possible under two-phase locking. To avoid


this, follow a modified protocol called strict two-phase locking -- a
transaction must hold all its exclusive locks till it commits/aborts.

 Rigorous two-phase locking is even stricter. Here, all locks are held
till commit/abort. In this protocol transactions can be serialized in
the order in which they commit.

2
3
Deadlocks Prevention

 Deadlock prevention protocols ensure that the system will never


enter into a deadlock state. Some prevention strategies :
 Require that each transaction locks all its data items before it
begins execution (predeclaration).
 Impose partial ordering of all data items and require that a
transaction can lock data items only in the order specified by
the partial order.

2
4
Deadlocks Prevention cont…

 Following schemes use transaction timestamps for the sake


of deadlock prevention alone.
 wait-die scheme — non-preemptive
 older transaction may wait for younger one to release
data item. (older means smaller timestamp) Younger
transactions never wait for older ones; they are rolled
back instead.
 a transaction may die several times before acquiring
needed data item

2
5
Deadlocks Prevention cont…

 wound-wait scheme — preemptive


 older transaction wounds (forces rollback) of younger
transaction instead of waiting for it. Younger transactions
may wait for older ones.
 may be fewer rollbacks than wait-die scheme.

2
6
Deadlocks Prevention cont…

 Both in wait-die and in wound-wait schemes, a rolled back


transactions is restarted with its original timestamp. Older
transactions thus have precedence over newer ones, and starvation
is hence avoided.

 Timeout-Based Schemes:
 a transaction waits for a lock only for a specified amount of
time. If the lock has not been granted within that time, the
transaction is rolled back and restarted,
 Thus, deadlocks are not possible
 simple to implement; but starvation is possible. Also difficult to
determine good value of the timeout interval.

2
7
Thank You

You might also like