Concurrency Controls
Concurrency Controls
Systems
Course Instructor: Engr. Samina Bilquees
Concurrency
• In a multi-user environment sometime different users simultaneously try to
access same piece of data this is called concurrency. If there is no concurrency
control by the DBMS important data may be lost.
• In order to explain the concept of concurrency lets take an example of ATM
system in a banking environment.
• Suppose two students Ali and Kashan have a joint Bank account and both have
ATM cards to use this bank account. On the particular day both Ali and Kashan
try to use their ATM cards at different ATM terminals but exactly at the same
time.
Concurrency Controls
• Lock-Based Protocol
• Two Phase Locking Protocols
• Time Stamp-Based Protocols
• Validation Based Protocols
Lock-Based Protocol
• Denoted by lock-X(Q)
• The read lock gets upgraded to write lock after compatibility checking
(Both read and Write)
• A data can be read as well as written with exclusive lock.
• X-lock is requested using X-lock instructions
• Any other transactions can not be obtained either write/exclusive mode.
Lock Compatibility Matrix
• A transactions may be granted a lock on an item if the
requested lock is compatible with locks already held on the
S X
item by other transactions.
S S X
• Any number of transactions can hold shared locks on an X X X
item, but if any transactions hold an exclusive(X) on the
item no other transaction may hold any lock on the item.
• If a lock can not be granted, the requesting transaction is
made to wait till all incompatible locks held by other
transactions have been released. Then lock is granted.
Two phase Locking Protocols
Two-phase locking protocol which is also known as a 2PL.
Two phase locking is a concurrency control locking protocol .
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 the locks it needs.
• The second part is where the transaction obtains all the locks when the transaction release its first
lock the third phase starts.
• In the third phase, the transaction can not demand any new locks, instead it only release the acquired
lock.
Phases
• Growing Phase
• Shrinking Phase
Growing Phase
• New locks on items can be acquired
• In this phase transaction may obtain locks but may not
release any locks is known as growing phase.
• It is also called expanding phase.
Shrinking Phase
• Existing locks, but no new lock can be acquired
• In this phase transaction may release locks but not obtain any new lock.
Example
Transaction T1
• Growing Phase: from step 0-2
• Shrinking Phase: from step 4-6
• Lock point: at 3
Transaction T2:
• Growing phase: from step 1-5
• Shrinking Phase: fro step 7-8
• Lock Point: at 6
2 PL in Distributed Database
• Centralized 2PL
• Primary Copy 2PL
• Distributed 2PL
Centralized 2PL/ Single Lock Manager
• System maintains a single lock manager that resides in a single chosen site, say S3
• Transaction Coordinator at site S1 divides the transactions
• Transaction Coordinator at site S1 acts as global transaction manager or
transaction coordinator.
• DM(data manager) component at site S1 sends the sub transaction to the
appropriate sites.
• When a transaction needs to lock a data item, it send a lock request to S3 and lock
manager determines whether the lock can be granted immediately.
• If yes then the lock manager sends a message to the site which initiated the
request.
• If no then request is delayed until it can be granted at which time a message is ent
to the initiating site.
Centralized 2PL/ Single Lock Manager
Contd…
• If the transaction involves an update of the data item that is replicated, the
coordinator must ensure that all copies of data item are updated.
• The coordinator requests write locks on all the copies before updating
each copy and releasing the locks
• The coordinator can elect to use any copy of the item for needs generally
the copy at its site, if one exists,
Advantages and Disadvantages
Advantages
• Simple implementation
• Simple deadlock handling
Disadvantages
• Bottleneck: lock manager site become a bottleneck
• Vulnerability: system is vulnerable to lock manager site failure
Primary Copy 2PL
• Choose one replica of data item to be the primary copy.
• Site containing the replica is called the primary site for that
data item
• Other copies are called slaves copes
• Different data item can have different primary site
• When a transaction needs to lock a data item Q9update) it
requests a primary site of Q
• The response request is delayed until it ca be granted.
• Implicitly gets lock on all the replicas of data item
Primary Copy 2PL
• Once the primary copy has been updated the change can b propagated to
slaves copies.
• The propagation should be carried out as soon as possible to prevent other
transactions from reading out of data values.
• If the primary site Q fails, Q is inaccessible even though other site
containing a replica may be accessible
Distributed Lock Manager
• Lock managers are assigned at each site.
• Lock managers control access to local data items
• When a transaction wishes to lock a data item Q, which is not replicated and
resides at the site S3, a message is sent to the lock manager at the site S3
requesting a lock
• If Q is locked in a n incompatible mode, the request is delayed until it is granted
• Once the lock request is granted the lock manager messages back to the initiator
indicating that it has granted the lock request.
Distributed Lock Manager Contd..
• When a transaction wishes to lock a data item Q, which is replicated,
Read- One-Write-All is implemented
• Any copy can be used for reading a data item.
• All copies must be write locked before an item can be updated.
• Work is distributed and can be robust in failure
• Deadlock detection is more complicated due to multiple lock managers
Timestamp Based Protocols
• Time stamp= assigning time to a transaction or asigning logical counter to any new transaction
• Basic idea of Time stamping is to decide the order between the transactions before that enters into the
system. So that in case of conflict during execution, we can resolve the conflict using ordering.
• Time stamp-based protocols uses a timestamp to serialize the execution of concurrent transactions
• The reason we call timestamp not stamp because for stamping we use the value of system clock (as it will
always be unique or never repeat itself)
• This protocols ensure that every conflicting read and write operations are executed in time stamp order
• Every transaction is timestamp with its start time
Two ideas of time stamping
• Time stamp with transactions
• With each transaction Ti, we associated a time stamp denoted by T.S(Ti)
• It is the value of system clock when a transaction enters into the system,
so if a new transaction Tj enters after Ti then T.S(Ti) ,(Tj) always unique
will remain fixed through the execution
Two ideas of time stamping Contd…