By
NeetiArora
Assistant Professor
Advanced Educational Institute
DBMS- Concurrency Control
www.advanced.edu.in
Definition
Concurrency control is a database management system (DBMS)
concept that is used to address conflicts with the simultaneous
accessing or altering of data that can occur with a multi-user
system. It ensures that database transactions are
performed concurrently without violating the data integrity of the
respective databases. Thus concurrency control is an essential
element for correctness in any system where two or more
database transactions, executed with time overlap, can access
the same data.
www.advanced.edu.in
Descriptive Example
To illustrate the concept of concurrency control, consider two travelers who go to
electronic kiosks at the same time to purchase a train ticket to the same destination
on the same train. There's only one seat left in the coach, but without concurrency
control, it's possible that both travelers will end up purchasing a ticket for that one
seat. However, with concurrency control, the database wouldn't allow this to
happen. Both travelers would still be able to access the train seating database, but
concurrency control would preserve data accuracy and allow only one traveler to
purchase the seat.
www.advanced.edu.in
Concurrency Control Protocols can be broadly divided into two
categories −
Lock based protocols
Time stamp based protocols
www.advanced.edu.in
Lock Based Protocol
A lock is nothing but a mechanism that tells the DBMS whether a particular data item is being
used by any transaction for read/write purpose. Since there are two types of operations, i.e.
read and write, whose basic nature are different, the locks for read and write operation may
behave differently.
Read operation performed by different transactions on the same data item. The value of the
data item, if constant, can be read by any number of transactions at any given time. If a
transaction is reading the content of a sharable data item, then any number of other processes
can be allowed to read the content of the same data item.
Write operation is something different. When a transaction writes some value into a data item,
the content of that data item remains in an inconsistent state, starting from the moment when
the writing operation begins up to the moment the writing operation is over.
But if any transaction is writing into a sharable data item, then no other transaction will be
allowed to read or write that same data item.
www.advanced.edu.in
Lock-based Protocols
Database systems equipped with lock-based protocols use a mechanism by which any transaction
cannot read or write data until it acquires an appropriate lock on it. Locks are of two kinds −
Binary Locks − A lock on a data item can be in two states; it is either locked or unlocked.
Shared/Exclusive Lock−
Shared Lock: A transaction may acquire shared lock on a data item in order to read its content. The
lock is shared in the sense that any other transaction can acquire the shared lock on that same data
item for reading purpose.
Exclusive Lock: A transaction may acquire exclusive lock on a data item in order to both read/write
into it. The lock is excusive in the sense that no other transaction can acquire any kind of lock (either
shared or exclusive) on that same data item.
www.advanced.edu.in
There are four types of lock protocols available −
Simplistic Lock Protocol
Simplistic lock-based protocols allow transactions to obtain a lock on every object before a 'write'
operation is performed. Transactions may unlock the data item after completing the ‘write’
operation.
Pre-claiming Lock Protocol
Pre-claiming protocols evaluate their operations and create a list of data items on which they need
locks. Before initiating an execution, the transaction requests the system for all the locks it needs
beforehand. If all the locks are granted, the transaction executes and releases all the locks when all
its operations are over. If all the locks are not granted, the transaction rolls back and waits until all
the locks are granted.
www.advanced.edu.in
Two Phase Locking Protocol
The Two Phase Locking Protocol assumes that a transaction can only be in one of two phases.
 Growing Phase: In this phase the transaction can only acquire locks, but cannot release any lock.
The transaction enters the growing phase as soon as it acquires the first lock it wants. From now on it has
no option but to keep acquiring all the locks it would need. It cannot release any lock at this phase even if it
has finished working with a locked data item. Ultimately the transaction reaches a point where all the lock it
may need has been acquired. This point is called Lock Point.
 Shrinking Phase: After Lock Point has been reached, the transaction enters the shrinking phase.
In this phase the transaction can only release locks, but cannot acquire any new lock. The transaction enters
the shrinking phase as soon as it releases the first lock after crossing the Lock Point.
Strict Two-Phase Locking
The first phase of Strict-2PL is same as 2PL. After acquiring all the locks in the first phase, the transaction
continues to execute normally. But in contrast to 2PL, Strict-2PL does not release a lock after using it. Strict-
2PL holds all the locks until the commit point and releases all the locks at a time.
www.advanced.edu.in
Timestamp-based Protocols
A timestamp is a tag that can be attached to any transaction or any data item, which denotes a specific time on
which the transaction or data item had been activated in any way. This protocol uses either system time or logical
counter as a timestamp. Every transaction has a timestamp associated with it, and the ordering is determined by
the age of the transaction.
The timestamp of a data item can be of the following two types:
W-timestamp (Q): This means the latest time when the data item Q has been written into.
R-timestamp (Q): This means the latest time when the data item Q has been read from.
www.advanced.edu.in
How should timestamps be used?
For Read operations: If a transaction Ti issues a read(X) operation −
If TS(Ti) < W-timestamp(X)
Operation rejected.
If TS(Ti) >= W-timestamp(X)
Operation executed.
All data-item timestamps updated.
For Write operations: If a transaction Ti issues a write(X) operation −
If TS(Ti) < R-timestamp(X)
Operation rejected.
If TS(Ti) < W-timestamp(X)
Operation rejected and Ti rolled back.
Otherwise, operation executed.
www.advanced.edu.in
Thomas’s Write Rule Timestamp Ordering Protocol (TWRTSO Protocol)
When Transaction Ti issues READ Operation :
a) If TS(Ti) < WTS(Q), then
ROLLBACK Ti.
b) Otherwise,allowed to execute READ operation by transaction Ti and
Set RTS(Q) = max(RTS(Q), TS(Ti))
When Transaction Ti issues WRITE Operation :
a) If TS(Ti) < RTS(Q), then
ROLLBACK Ti.
a) If TS(Ti) < WTS(Q), then Ignore WRITE operation by Ti and
Continue the execution of Ti.
c) Otherwise, Allowed to execute the WRITE operation by transaction Ti and
Set WTS(Q) = TS(Ti)
www.advanced.edu.in
Neeti Arora
Assistant Professor
neetiarorapkl@gmail.com
Advanced Educational Institutions,
70 km Milestone,
Delhi-Mathura Road, Distt. Palwal, Haryana-121105
+91–1275–398400, 302222
www.advanced.edu.in
www.advanced.edu.in

Concurrency control ms neeti

  • 1.
    By NeetiArora Assistant Professor Advanced EducationalInstitute DBMS- Concurrency Control www.advanced.edu.in
  • 2.
    Definition Concurrency control isa database management system (DBMS) concept that is used to address conflicts with the simultaneous accessing or altering of data that can occur with a multi-user system. It ensures that database transactions are performed concurrently without violating the data integrity of the respective databases. Thus concurrency control is an essential element for correctness in any system where two or more database transactions, executed with time overlap, can access the same data. www.advanced.edu.in
  • 3.
    Descriptive Example To illustratethe concept of concurrency control, consider two travelers who go to electronic kiosks at the same time to purchase a train ticket to the same destination on the same train. There's only one seat left in the coach, but without concurrency control, it's possible that both travelers will end up purchasing a ticket for that one seat. However, with concurrency control, the database wouldn't allow this to happen. Both travelers would still be able to access the train seating database, but concurrency control would preserve data accuracy and allow only one traveler to purchase the seat. www.advanced.edu.in
  • 4.
    Concurrency Control Protocolscan be broadly divided into two categories − Lock based protocols Time stamp based protocols www.advanced.edu.in
  • 5.
    Lock Based Protocol Alock is nothing but a mechanism that tells the DBMS whether a particular data item is being used by any transaction for read/write purpose. Since there are two types of operations, i.e. read and write, whose basic nature are different, the locks for read and write operation may behave differently. Read operation performed by different transactions on the same data item. The value of the data item, if constant, can be read by any number of transactions at any given time. If a transaction is reading the content of a sharable data item, then any number of other processes can be allowed to read the content of the same data item. Write operation is something different. When a transaction writes some value into a data item, the content of that data item remains in an inconsistent state, starting from the moment when the writing operation begins up to the moment the writing operation is over. But if any transaction is writing into a sharable data item, then no other transaction will be allowed to read or write that same data item. www.advanced.edu.in
  • 6.
    Lock-based Protocols Database systemsequipped with lock-based protocols use a mechanism by which any transaction cannot read or write data until it acquires an appropriate lock on it. Locks are of two kinds − Binary Locks − A lock on a data item can be in two states; it is either locked or unlocked. Shared/Exclusive Lock− Shared Lock: A transaction may acquire shared lock on a data item in order to read its content. The lock is shared in the sense that any other transaction can acquire the shared lock on that same data item for reading purpose. Exclusive Lock: A transaction may acquire exclusive lock on a data item in order to both read/write into it. The lock is excusive in the sense that no other transaction can acquire any kind of lock (either shared or exclusive) on that same data item. www.advanced.edu.in
  • 7.
    There are fourtypes of lock protocols available − Simplistic Lock Protocol Simplistic lock-based protocols allow transactions to obtain a lock on every object before a 'write' operation is performed. Transactions may unlock the data item after completing the ‘write’ operation. Pre-claiming Lock Protocol Pre-claiming protocols evaluate their operations and create a list of data items on which they need locks. Before initiating an execution, the transaction requests the system for all the locks it needs beforehand. If all the locks are granted, the transaction executes and releases all the locks when all its operations are over. If all the locks are not granted, the transaction rolls back and waits until all the locks are granted. www.advanced.edu.in
  • 8.
    Two Phase LockingProtocol The Two Phase Locking Protocol assumes that a transaction can only be in one of two phases.  Growing Phase: In this phase the transaction can only acquire locks, but cannot release any lock. The transaction enters the growing phase as soon as it acquires the first lock it wants. From now on it has no option but to keep acquiring all the locks it would need. It cannot release any lock at this phase even if it has finished working with a locked data item. Ultimately the transaction reaches a point where all the lock it may need has been acquired. This point is called Lock Point.  Shrinking Phase: After Lock Point has been reached, the transaction enters the shrinking phase. In this phase the transaction can only release locks, but cannot acquire any new lock. The transaction enters the shrinking phase as soon as it releases the first lock after crossing the Lock Point. Strict Two-Phase Locking The first phase of Strict-2PL is same as 2PL. After acquiring all the locks in the first phase, the transaction continues to execute normally. But in contrast to 2PL, Strict-2PL does not release a lock after using it. Strict- 2PL holds all the locks until the commit point and releases all the locks at a time. www.advanced.edu.in
  • 9.
    Timestamp-based Protocols A timestampis a tag that can be attached to any transaction or any data item, which denotes a specific time on which the transaction or data item had been activated in any way. This protocol uses either system time or logical counter as a timestamp. Every transaction has a timestamp associated with it, and the ordering is determined by the age of the transaction. The timestamp of a data item can be of the following two types: W-timestamp (Q): This means the latest time when the data item Q has been written into. R-timestamp (Q): This means the latest time when the data item Q has been read from. www.advanced.edu.in
  • 10.
    How should timestampsbe used? For Read operations: If a transaction Ti issues a read(X) operation − If TS(Ti) < W-timestamp(X) Operation rejected. If TS(Ti) >= W-timestamp(X) Operation executed. All data-item timestamps updated. For Write operations: If a transaction Ti issues a write(X) operation − If TS(Ti) < R-timestamp(X) Operation rejected. If TS(Ti) < W-timestamp(X) Operation rejected and Ti rolled back. Otherwise, operation executed. www.advanced.edu.in
  • 11.
    Thomas’s Write RuleTimestamp Ordering Protocol (TWRTSO Protocol) When Transaction Ti issues READ Operation : a) If TS(Ti) < WTS(Q), then ROLLBACK Ti. b) Otherwise,allowed to execute READ operation by transaction Ti and Set RTS(Q) = max(RTS(Q), TS(Ti)) When Transaction Ti issues WRITE Operation : a) If TS(Ti) < RTS(Q), then ROLLBACK Ti. a) If TS(Ti) < WTS(Q), then Ignore WRITE operation by Ti and Continue the execution of Ti. c) Otherwise, Allowed to execute the WRITE operation by transaction Ti and Set WTS(Q) = TS(Ti) www.advanced.edu.in
  • 12.
    Neeti Arora Assistant Professor neetiarorapkl@gmail.com AdvancedEducational Institutions, 70 km Milestone, Delhi-Mathura Road, Distt. Palwal, Haryana-121105 +91–1275–398400, 302222 www.advanced.edu.in www.advanced.edu.in