[go: up one dir, main page]

0% found this document useful (0 votes)
123 views15 pages

Unit 4 Locking Based Protocol

The document discusses different concurrency control protocols in databases including lock-based protocols and two phase locking protocol. Lock-based protocols use locks to control access to data by transactions to avoid conflicts. There are two main types of locks - shared locks that allow multiple transactions to read data concurrently, and exclusive locks that allow a transaction to write to data preventing other transactions from reading or writing to it concurrently. The two phase locking protocol divides a transaction into a growing phase where it acquires locks and a shrinking phase where it only releases locks to ensure serializability.

Uploaded by

Yash
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)
123 views15 pages

Unit 4 Locking Based Protocol

The document discusses different concurrency control protocols in databases including lock-based protocols and two phase locking protocol. Lock-based protocols use locks to control access to data by transactions to avoid conflicts. There are two main types of locks - shared locks that allow multiple transactions to read data concurrently, and exclusive locks that allow a transaction to write to data preventing other transactions from reading or writing to it concurrently. The two phase locking protocol divides a transaction into a growing phase where it acquires locks and a shrinking phase where it only releases locks to ensure serializability.

Uploaded by

Yash
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/ 15

UNIT -4

Locking and Timestamp Based


Scheduler
Concurrency Control Protocols
• Different concurrency control protocols offer different
benefits between the amount of concurrency they allow and
the amount of overhead that they impose. Following are the
Concurrency Control techniques in DBMS:
• Lock-Based Protocols
• Two Phase Locking Protocol
• Timestamp-Based Protocols
• Validation-Based Protocols
Lock-based Protocols
• Lock Based Protocols in DBMS is a mechanism in which a
transaction cannot Read or Write the data until it acquires an
appropriate lock.
• Lock based protocols help to eliminate the concurrency
problem in DBMS for simultaneous transactions by locking or
isolating a particular transaction to a single user.
• A lock is a data variable which is associated with a data item.
This lock signifies that operations that can be performed on the
data item. Locks in DBMS help synchronize access to the
database items by concurrent transactions.
• All lock requests are made to the concurrency-control manager.
Transactions proceed only once the lock request is granted.
Lock-based Protocols
• Binary Locks: A Binary lock on a data item can either locked or
unlocked states.
• Shared/exclusive: This type of locking mechanism separates
the locks in DBMS based on their uses. If a lock is acquired on
a data item to perform a write operation, it is called an
exclusive lock.
Shared Lock
• 1. Shared Lock (S):
• A shared lock is also called a Read-only lock. With the
shared lock, the data item can be shared between
transactions. This is because you will never have
permission to update data on the data item.
• For example, consider a case where two transactions are
reading the account balance of a person. The database will
let them read by placing a shared lock. However, if another
transaction wants to update that account's balance,
shared lock prevent it until the reading process is over.
Exclusive Lock
• Exclusive Lock (X):
• With the Exclusive Lock, a data item can be read as well as
written. This is exclusive and can't be held concurrently on
the same data item. X-lock is requested using lock-x
instruction. Transactions may unlock the data item after
finishing the 'write' operation.
• For example, when a transaction needs to update the
account balance of a person. You can allows this
transaction by placing X lock on it. Therefore, when the
second transaction wants to read or write, exclusive lock
prevent this operation.
Lock Compatibility Matrix
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(X) 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. Then the lock is granted.
Two Phase Locking Protocol

• Two Phase Locking Protocol 


• It is a method of concurrency control in DBMS
that ensures serializability by applying a lock
to the transaction data which blocks other
transactions to access the same data
simultaneously.
• Two Phase Locking protocol helps to eliminate
the concurrency problem in DBMS.
Two Phase Locking Protocol

• This locking protocol divides the execution phase of a


transaction into three different parts.
• In the first phase, when the transaction begins to
execute, it requires permission for the locks it needs.
• The second part is where the transaction obtains all the
locks. When a transaction releases its first lock, the third
phase starts.
• In this third phase, the transaction cannot demand any
new locks. Instead, it only releases the acquired locks.
Two Phase Locking Protocol
Two Phase Locking Protocol
• The Two-Phase Locking protocol allows each
transaction to make a lock or unlock request in two
steps:
• Growing Phase: In this phase transaction may obtain
locks but may not release any locks.
• Shrinking Phase: In this phase, a transaction may
release locks but not obtain any new lock
• It is true that the 2PL protocol offers serializability.
However, it does not ensure that deadlocks do not
happen.
T1 T2 Let’s see a transaction implementing 2-PL.
lock-
1
S(A)

2 lock-S(A)

lock-
3
X(B)
4 ……. ……

Unlock(
5
A)

6 Lock-X(C)

Unlock(
7
B)

8 Unlock(A)

9 Unlock(C)

10 ……. ……
• This is just a skeleton transaction which shows how
unlocking and locking works with 2-PL. Note for:
Transaction T1:
• Growing Phase is from steps 1-3.
• Shrinking Phase is from steps 5-7.
• Lock Point at 3
• Transaction T2:
• Growing Phase is from steps 2-6.
• Shrinking Phase is from steps 8-9.
• Lock Point at 6
•  LOCK POINT ?The Point at which the growing
phase ends, i.e., when transaction takes the
final lock it needs to carry on its work.

You might also like