DBMS Concurrency Control - Two Phase, Timestamp, Lock-Based Protocol
DBMS Concurrency Control - Two Phase, Timestamp, Lock-Based Protocol
(https://www.guru99.com/)
Concurrency control is used to address such conflicts which mostly occur with a multi-user
system. It helps you to make sure that database transactions are performed concurrently
without violating the data integrity of respective databases.
Therefore, concurrency control is a most important element for the proper functioning of a
system where two or multiple database transactions that require access to the same data, are
executed simultaneously.
Lost Updates occur when multiple transactions select the same row and update the row
based on the value selected
Uncommitted dependency issues occur when the second transaction selects a row which is
updated by another transaction (dirty read)
Non-Repeatable Read occurs when a second transaction is trying to access the same row
several times and reads different data each time.
Incorrect Summary issue occurs when one transaction takes summary over the value of all
the instances of a repeated data-item, and second transaction update few instances of that
specific data-item. In that situation, the resulting summary does not reflect a correct result.
Example
Assume that two people who go to electronic kiosks at the same time to buy a movie ticket for
the same movie and the same show time.
However, there is only one seat left in for the movie show in that particular theatre. Without
concurrency control, it is possible that both moviegoers will end up purchasing a ticket.
However, concurrency control method does not allow this to happen. Both moviegoers can still
access information written in the movie seating database. But concurrency control only
provides a ticket to the buyer who has completed the transaction process first.
https://www.guru99.com/dbms-concurrency-control.html 2/11
4/14/2019 DBMS Concurrency Control: Two Phase, Timestamp, Lock-Based Protocol
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.
Lock-Based Protocols
Two Phase
Timestamp-Based Protocols
Validation-Based Protocols
Lock-based Protocols
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 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.
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 based on their uses. If a
lock is acquired on a data item to perform a write operation, it is called an exclusive lock.
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.
https://www.guru99.com/dbms-concurrency-control.html 3/11
With the Exclusive Lock, a dataDBMS
4/14/2019
item can be read as well as written. This is exclusive and can't
Concurrency Control: Two Phase, Timestamp, Lock-Based Protocol
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.
This type of lock-based protocols allows transactions to obtain a lock on every object before
beginning operation. Transactions may unlock the data item after finishing the 'write'
operation.
4. Pre-claiming Locking
Pre-claiming lock protocol helps to evaluate operations and create a list of required data items
which are needed to initiate an execution process. In the situation when all locks are granted,
the transaction executes. After that, all locks release when all of its operations are over.
Starvation
Starvation is the situation when a transaction needs to wait for an indefinite period to acquire a
lock.
Deadlock
Deadlock refers to a specific situation where two or more processes are waiting for each other
to release a resource or more than two processes are waiting for the resource in a circular
chain.
https://www.guru99.com/dbms-concurrency-control.html 4/11
4/14/2019 DBMS Concurrency Control: Two Phase, Timestamp, Lock-Based Protocol
Two Phase Locking (2PL) Protocol
Two-Phase locking protocol which is also known as a 2PL protocol. It is also called P2L. In this
type of locking protocol, the transaction should acquire a lock after it releases one of its locks.
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.
(/images/1/100518_0439_DBMSConcurr1.png)
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.
https://www.guru99.com/dbms-concurrency-control.html 5/11
In the above-given diagram, you
4/14/2019
can see that local and global deadlock detectors are searching
DBMS Concurrency Control: Two Phase, Timestamp, Lock-Based Protocol
for deadlocks and solve them with resuming transactions to their initial states.
Centralized 2PL
In Centralized 2 PL, a single site is responsible for lock management process. It has only one
lock manager for the entire DBMS.
Distributed 2PL
In this kind of two-phase locking mechanism, Lock managers are distributed to all sites. They
are responsible for managing locks for data at that site. If no data is replicated, it is equivalent
to primary copy 2PL. Communication costs of Distributed 2PL are quite higher than primary
copy 2PL
Timestamp-based Protocols
The timestamp-based algorithm uses a timestamp to serialize the execution of concurrent
transactions. This protocol ensures that every conflicting read and write operations are
executed in timestamp order. The protocol uses the System Time or Logical Count as a
Timestamp.
The older transaction is always given priority in this method. It uses system time to determine
the time stamp of the transaction. This is the most commonly used concurrency protocol.
Lock-based protocols help you to manage the order between the conflicting transactions when
they will execute. Timestamp-based protocols manage conflicts as soon as an operation is
created.
https://www.guru99.com/dbms-concurrency-control.html 6/11
Example:
4/14/2019 DBMS Concurrency Control: Two Phase, Timestamp, Lock-Based Protocol
Advantages:
Disadvantages:
Summary
transactions. The protocol uses the System Time or Logical Count as a Timestamp.
https://www.guru99.com/dbms-concurrency-control.html 8/11
4/14/2019 DBMS Concurrency Control: Two Phase, Timestamp, Lock-Based Protocol
DBMS Tutorial
3) DBMS Schemas (/dbms-schemas.html)
https://www.guru99.com/dbms-concurrency-control.html 9/11
4/14/2019 DBMS Concurrency Control: Two Phase, Timestamp, Lock-Based Protocol
(https://www.facebook.com/guru99com/)
(https://twitter.com/guru99com)
(https://www.youtube.com/channel/UC19i1XD6k88KqHlET8atqFQ)
(https://forms.aweber.com/form/46/724807646.htm)
About
About Us (/about-us.html)
Advertise with Us (/advertise-us.html)
Write For Us (/become-an-instructor.html)
Contact Us (/contact-us.html)
Career Suggestion
https://www.guru99.com/dbms-concurrency-control.html 10/11
4/14/2019
SAP Career Suggestion Tool (/best-sap-module.html)
DBMS Concurrency Control: Two Phase, Timestamp, Lock-Based Protocol
Interesting
Books to Read! (/books.html)
Blog (/blog/)
Quiz (/tests.html)
eBook (/ebook-pdf.html)
Execute online
Execute Java Online (/try-java-editor.html)
Execute Javascript (/execute-javascript-online.html)
Execute HTML (/execute-html-online.html)
Execute Python (/execute-python-online.html)
https://www.guru99.com/dbms-concurrency-control.html 11/11