Chapter 4 ADBMS P
Chapter 4 ADBMS P
Chapter 4 ADBMS P
CHAPTER 4
Concurrency Control Techniques
By: Shewakena G.
Slide Title
1. Locking Techniques for Concurrency Control
2. Concurrency Control Based ON Timestamp Ordering
3. Multi-version Concurrency Control Techniques
4. Validation (Optimistic) Concurrency Control
Techniques
5. Granularity of Data Items and Multiple Granularity
Locking
6. Using Locks for Concurrency Control in Indexes
Introduction
Concurrency control Techniques
Concurrency control protocols
Ensure the non interference or isolation property of concurrently executing
transactions.
Set of rules to guarantee serializability (refers sequence of actions to correct)
Two-phase locking protocols
Lock data items to prevent concurrent access
Timestamp
Unique identifier for each transaction
Multiversion currency control protocols
Use multiple versions of a data item
Validation or certification of a transaction called Optimistic
Concurrency control
Techniques
• Basic Concurrency techniques:
– Locking
– Time stamping
– Optimistic methods
• Locking and Time stamping are conservative approaches.
– Locking all data item before transaction begins.
– Delay transaction in case they conflict with other transaction.
• Optimistic methods are conflict is rare and only check for
conflicts at commit.
Database Concurrency Control
• Purpose of Concurrency Control
– To enforce Isolation (through mutual exclusion) among conflicting transactions.
– To preserve database consistency through consistency preserving execution of
transactions.
– To resolve read-write and write-write conflicts.
• Example:
– In concurrent execution environment if T1 conflicts with T2 over a data item A.
– Then the existing concurrency control decides if T1 or T2 should get the A and
if the other transaction is rolled-back or waits.
Concurrency Control Techniques
♦ Locking Permission to Read or T Write.
Variable associated with a data item.
Describing status for possible operations that can be applied
One lock for each item in the database
A transaction must claim a shared(read) or exclusive(write) lock on a data item before read
or write.
Lock prevents another transaction from modifying item or even reading it, in the case of a
write lock.
♦ Binary locks
Two states (values) and have Atomic operation.
Locked (1)
● Item cannot be accessed
Unlocked (0)
4.1Two-Phase Locking Techniques for Concurrency Control
Two-Phase Locking Techniques
– Locking is an operation which secures
• permission to Read
• permission to Write a data item for a transaction.
– Example:
• Lock (X). Data item X is locked in behalf of the requesting transaction.
– Unlocking is an operation which removes these permissions
from the data item.
– Example:
• Unlock (X): Data item X is made available to all other transactions.
– Lock and Unlock are Atomic operations.
Database Concurrency Control
Two-Phase Locking Techniques: Essential components
– Two locks modes:
• (a) shared (read) (b) exclusive (write).
– Shared mode: shared lock (X)
• More than one transaction can apply share lock on X for reading its value.
• but no write lock can be applied on X by any other transaction.
– Exclusive mode: Write lock (X)
• Only one write lock on X can exist at any time and no shared lock can be
applied by any other transaction on X.
– Conflict matrix Read Write
Read
Y N
No Conflict N N
Wr
4.1Two-Phase Locking Techniques for Concurrency
Control
• Wait-die.
– Protocols based on a timestamp
– In wait-die protocol the following will happen
◦ If Ti has higher priority, it is allowed to wait; otherwise it is
aborted.
◦ An older transaction is allowed to wait on a younger
transaction.
◦ A younger transaction requesting an item held by an older
transaction is aborted
Dealing with Deadlock and Starvation
(cont’d.)
• Wound-wait.
– The opposite of wait-die.
– If Ti has higher priority, aborted Tj, otherwise Ti waits.
– A younger transaction is allowed to wait on an older one.
– An older transaction requesting an item held by younger transaction preempts the
younger transaction by aborting it.
– If TS(Ti)<TS(Tj), then (Ti older than Tj), Abort Tj(Ti wounds Tj) and restart Tj later
with the same timestamp. Otherwise (Ti younger than Tj) Ti is allowed to wait.
T1(ts =25)
wait
wait wait T2 (ts =20)
T3 (ts =10)
Dealing with Deadlock and Starvation
(cont’d.)
4. No waiting algorithm
– If transaction unable to obtain a lock, immediately aborted and restarted later.
– It restart after a certain time delay without checking whether a deadlock will actually occur or not
• No transaction wait no deadlock
• Make transaction to abort and restart needlessly
5. Cautious waiting algorithm
• Suppose T1 wait item X that is locked by Tj, and if Tj is not locked (waiting for some other
locked item), then Ti is blocked and allowed to wait otherwise abort Ti
– Deadlock-free
– Reduce the number of needless abort and restart
6. Deadlock detection
– System checks to see if a state of deadlock exists
– Wait-for graph
Dealing with Deadlock and
Starvation (cont’d.)
Deadlock Detection
– Simple way to detect a state of deadlock is to draw wait-for-graph
– The system construct and maintain a wait-for graph
• G(V, E)
Where: V is nodes describing transaction
E is directed edge
– Ti Tj
Directed edge { Ti is waiting for a data item held by Tj}
• Then the system check if there is a cycle in the wait-for graph
Dealing with Deadlock and Starvation
(cont’d.)
Deadlock Detection
– The other method to detect a deadlock is use of timeouts
– The system defined a timeout period
– If a transaction waits for a period longer than a system defined
time out period, the system assume that the transaction may be
deadlock and abort it. (May be even there is no deadlock )
– It is practical because of low overhead and simplicity
Dealing with Deadlock and
Starvation (cont’d.)
Victim selection
– Deciding which transaction to abort in case of deadlock
– While it select the transaction to be abort it looks the transaction that
cost minimum to decide that we can analyze with the following
points:
• Length of transaction timestamp – the younger transaction that has big
timestamp will be selected
• Data item used by transaction- Transaction that used less data item used is
selected
• Data item that are need to be locked – a transaction that needs more data item
to lock is selected
• How many transaction to be rolled back – a transaction that cause minimum
Dealing with Deadlock and Starvation
(cont’d.)
Starvation
– Occurs if a transaction cannot proceed for an indefinite
period of time while other transactions continue normally
– An algorithm dealing with deadlock prevent select same
transaction victim repeatedly, thus causing it abort and
never finish execution
– This problem can be solved by
• Modifying the time stamp of the starved transaction
• Increasing the priority of the transaction
4.2 Concurrency Control Based on
Timestamp Ordering
Timestamp
– Unique identifier assigned by the DBMS to identify a transaction
– Assigned the value that show the order in which the transaction is
submitted
– Transaction start time – TS(T)
– For example – incremental timestamps
• Date/time value of the system
Concurrency control techniques based on timestamps do not use
locks; hence,
• Deadlocks cannot occur
Concurrency Control Based on
Timestamp Ordering (cont’d.)
Generating timestamps
– Counter incremented each time its value is assigned to
a transaction
– Current date/time value of the system clock
• Ensure no two timestamps are generated during the same tick
of the clock
General approach
– Enforce equivalent serial order on the transactions
based on their timestamps
Concurrency Control Based on
Timestamp Ordering (cont’d.)
Timestamp ordering (TO)
– Allows interleaving of transaction operations
– Must ensure timestamp order is followed for each pair of conflicting operations
– In time stamp ordering, the schedule is equivalent to the particular serial order
corresponding to the one of the transaction timestamps
Each database item assigned two timestamp values
– read_TS(X) – read timestamp of the x , largest timestamp from all
timestamps read X .
read _TS(X)= TS(S) younger transaction
– write_TS(X)- largest write timestamp
write_TS(x)= TS(T) younger transaction
Concurrency Control Based on
Timestamp Ordering (cont’d.)
Basic TO algorithm
– Whenever some transaction T tries to issue a read_item(x) or write-
item(x) operation, the basic OT algorithm compare the read_TS(X) and
Write_TS(x) to ensure that the timestamp of the transaction execution of
time is not violated
– If the order of the transaction is violated, then the transaction T is aborted
and resubmitted to the system as new transaction with new timestamp
– If conflicting operations detected, later operation rejected by aborting
transaction that issued it
– Schedules produced guaranteed to be conflict serializable
– Starvation may occur
Concurrency Control Based on Timestamp Ordering
(cont’d.)
Figure 21.6 Lock compatibility tables (a) Lock compatibility table for read/write locking
scheme (b) Lock compatibility table for read/write/certify locking scheme
4.4 Validation (Optimistic) Techniques and Snapshot
Isolation Concurrency Control
Optimistic techniques
– Also called validation or certification techniques
– No checking is done while the transaction is executing
– Updates not applied directly to the database until
finished transaction is validated
• All updates applied to local copies of data items
– Validation phase checks whether any of transaction’s
updates violate serializability
•
Concurrency Control Based on
Snapshot Isolation
Transaction sees data items based on committed values of
the items in the database snapshot
– Does not see updates that occur after transaction starts
Read operations do not require read locks
– Write operations require write locks
Temporary version store keeps track of older versions of
updated items
Variation: serializable snapshot isolation (SSI)
4.5 Granularity of Data Items and
Multiple Granularity Locking
Figure 21.7 A granularity hierarchy for illustrating multiple granularity level locking
Slide 21- 48
Multiple Granularity Level Locking
(cont’d.)
Intention locks are needed
– Transaction indicates along the path from the root to the
desired node, what type of lock (shared or exclusive) it
will require from one of the node’s descendants
Intention lock types
– Intention-shared (IS)
• Shared locks will be requested on a descendant node
– Intention-exclusive (IX)
• Exclusive locks will be requested
Slide 21- 49
Multiple Granularity Level Locking
(cont’d.)
57