Chapter - 4 Concurency Control Techniques
Chapter - 4 Concurency Control Techniques
Chapter -4
Concurrency Control
Concurrency control — ensuring that each user appears
to execute in isolation.
It is the procedure in DBMS for managing simultaneous
operations without conflicting with each another.
Why?
Lost Updates
Temporary update (dirty read)
Non-Repeatable Read
Incorrect Summary issue
Concurrency Control Algorithms
– Pessimistic methods assume that many transactions will conflict, thus the
concurrent execution of transactions is synchronized early in their execution life
cycle
– Optimistic methods assume that not too many transactions will conflict, thus
delay the synchronization of transactions until their termination
∗ Locking-based
∗ Timestamp ordering-based
Locking Based Algorithms
A lock is a mechanism to control concurrent access to a data
item
Binary Locks - will have 2 values locked and unlocked
represented as 0 and 1.
T1 T2
LOCKX(A) LOCKX(SUM)
READ(A) A=1000 SUM:=0
A:=A-200 A=800 LOCKS(A)
WRITE(A) A=800 READ(A) A=800
UNLOCK(A) SUM:=SUM+A SUM=800
LOCKX(B) UNLOCK(A)
READ(B) B=900 LOCKS(B)
B:=B+200 B=1100 READ(B) B=1100
WRITE(B) B=1100 SUM:=SUM+B SUM=1900
UNLOCK(B) WRITE(SUM) SUM=1900
UNLOCK(B)
UNLOCK(SUM)
IF EXECUTED SERIALLY THE
OUTPUT WILL BE 1900
IF RUNNING CONCURREENTLY THEN SUM WILL BE 2100, LOSS
UPDATE
T2:LOCKX(SUM) T1:WRITE(B) B=1100
T2:SUM:=0 T1:UNLOCK(B)
T2:LOCKS(A) A=1000 T2:LOCKS(B)
T2:READ(A) T2:READ(B) B=1100
T2:SUM:=SUM+A SUM=1000 T2:SUM:=SUM+B SUM=2100
T2:UNLOCK(A) T2:WRITE(SUM) SUM=2100
T1:LOCKX(A) T2:UNLOCK(B)
T1:READ(A) A=1000 T2:UNLOCK(SUM)
T1:A:=A-200 A=800
AFTER TRANSACTION SUM+B IS
T1:WRITE(A) A=800
T1:UNLOCK(A) 2100 .This is inconsistent. This locking
T1:LOCKX(B) technique didn’t solve this problem because
T1:READ(B) B=900
value of a/c A was added to sum before
T1:B:=B+200 B=1100
modification was performed
Consider another example
T1
LOCKX(B)
UNLOCK(A)
READ(B) B=200 LOCKS(B)
B:=B-50B=150
WRITE(B) B=150 READ(B) B=150
UNLOCK(B)
LOCKX(A) UNLOCK(B)
READ(A) A=100
A:=A+50 A=150
DISPLAY(A+B)
WRITE(A) A=150 A+B=300
UNLOCK(A)
T2:
THIS IS CLEAR THAT IF
LOCKS(A) THEY RUN SEQUENTIALLY
READ(A) A=150 THE OUT PUT WILL BE 300
Now if we delay unlocking to the end of transaction then
T1 READ(A) A=150
LOCKX(B) LOCKS(B)
READ(B) B=200 READ(B) B=150
B:=B-50 B=150 UNLOCK(A)
WRITE(B) B=150 UNLOCK(B)
LOCKX(A) DISPLAY(A+B)
READ(A) A=100 HERE ALSO A+B WILL BE 300
A:=A+50A=150
WRITE(A) A=150
UNLOCK(B)
UNLOCK(A)
T2
LOCKS(A)
Two phase locking
Phase 1: Growing Phase
Transaction may obtain locks
Transaction may not release locks
Phase 2: Shrinking Phase
Transaction may release locks
Transaction may not obtain locks
Example:
T1 T1
READ(B)
READ(B)
B:=B-50
B:=B-50 WRITE(B)
WRITE(B) LOCKX(A)
UNLOCK(B) READ(A)
A:=A+50
LOCKX(A)
WRITE(A)
READ(A) UNLOCK(B)
A:=A+50 UNLOCK(A)
WRITE(A)
UNLOCK(A) This is 2 phase because unlocks
This is not 2 phase because unlock(b) appears appears after all lock operation
before lock(a)
Two-phase locking does not ensure freedom from deadlocks
Cascading roll-back is possible under two-phase locking.
To avoid this, follow a modified protocol called strict two-phase
locking. Here a transaction must hold all its exclusive locks till it
commits/ aborts.
This requirement ensures that any data written by uncommitted
transaction are locked in exclusive mode until the transaction commits.
Avoiding starvation:
Example