Lecture 24-25 Concurrency Control
Lecture 24-25 Concurrency Control
Control
8/4/2021 CS 245 2
Lock-Based Protocols
• A lock is a mechanism to control concurrent access
to a data item
• Data items can be locked in two modes :
1. exclusive (X) mode. Data item can be both read as
well as written. X-lock is requested using lock-X
instruction.
2. shared (S) mode. Data item can only be read. S-
lock is requested using lock-S instruction.
• Lock requests are made to concurrency-control
manager. Transaction can proceed only after request
is granted.
8/4/2021 CS 245 3
Lock-Based Protocols (Cont.)
• Lock-compatibility matrix
lock-S(B);
read (B);
unlock(B);
display(A+B)
• Locking as above is not sufficient to guarantee
serializability
8/4/2021 CS 245 5
Schedule With Lock Grants
• Grants omitted in later
schedules
– Assume grant happens
just before the next
instruction following
lock request
• A locking protocol is a
set of rules followed by
all transactions while
requesting and
releasing locks.
• Locking protocols
enforce serializability
by restricting the set of
possible schedules.
8/4/2021 CS 245 6
Deadlock
• Consider the partial schedule
8/4/2021 CS 245 8
The Two-Phase Locking Protocol
• A protocol which ensures
conflict-serializable schedules.
• Phase 1: Growing Phase
– Transaction may obtain locks
Locks
– Transaction may not release locks
• Phase 2: Shrinking Phase
– Transaction may release locks Time
– Transaction may not obtain locks
• The protocol assures
serializability. The transactions
can be serialized in the order of
their lock points (i.e., the point
where a transaction acquired
its final lock).
8/4/2021 CS 245 9
T5 T6 T7
lock-X(A)
read(A)
lock-S(B)
read(B)
write(A)
unlock(A)
lock-X(A)
read(A)
write(A)
unlock(A)
lock-S(A)
read(A)
8/4/2021 CS 245 10
The Two-Phase Locking Protocol (Cont.)
• Two-phase locking does not ensure freedom from
deadlocks
• Extensions to basic two-phase locking needed to
ensure recoverability of freedom from cascading roll-
back
– Strict two-phase locking: a transaction must hold all its
exclusive locks till it commits/aborts.
• Ensures recoverability and avoids cascading roll-backs
– Rigorous two-phase locking: a transaction must hold all
locks till commit/abort.
• Transactions can be serialized in the order in which they
commit.
• Most databases implement rigorous two-phase
locking, but refer to it as simply two-phase locking
8/4/2021 CS 245 11
Locking Protocols
• Given a locking protocol (such as 2PL)
– A schedule S is legal under a locking protocol if it can
be generated by a set of transactions that follow the
protocol
– A protocol ensures serializability if all legal schedules
under that protocol are serializable
8/4/2021 CS 245 12
Lock Conversions
• Two-phase locking protocol with lock conversions:
– Growing Phase:
– can acquire a lock-S on item
– can acquire a lock-X on item
– can convert a lock-S to a lock-X (upgrade)
– Shrinking Phase:
– can release a lock-S
– can release a lock-X
– can convert a lock-X to a lock-S (downgrade)
• This protocol ensures serializability
8/4/2021 CS 245 13
Lock Conversions
T8 T9
T8: read(a1); lock-S(a1)
read(a2); lock-S(a1)
... lock-S(a2)
read(an); lock-S(a2)
write(a1). lock-S(a3)
lock-S(a4)
T9: read(a1); unlock(a1)
read(a2); unlock(a2)
display(a1 + a2). lock-S(an)
upgrade(a1)
8/4/2021 CS 245 14
Automatic Acquisition of Locks
• A transaction Ti issues the standard read/write
instruction, without explicit locking calls.
• The operation read(D) is processed as:
if Ti has a lock on D
then
read(D)
else begin
if necessary wait until no other
transaction has a lock-X on D
grant Ti a lock-S on D;
read(D)
end
8/4/2021 CS 245 15
Automatic Acquisition of Locks (Cont.)
• The operation write(D) is processed as:
if Ti has a lock-X on D
then
write(D)
else begin
if necessary wait until no other trans. has any
lock on D,
if Ti has a lock-S on D
then
upgrade lock on D to lock-X
else
grant Ti a lock-X on D
write(D)
end;
• All locks are released after commit or abort
8/4/2021 CS 245 16
Implementation of Locking
• A lock manager can be implemented as a
separate process
• Transactions can send lock and unlock requests
as messages
• The lock manager replies to a lock request by
sending a lock grant messages (or a message
asking the transaction to roll back, in case of a
deadlock)
– The requesting transaction waits until its request is
answered
• The lock manager maintains an in-memory data-
structure called a lock table to record granted
locks and pending requests
8/4/2021 CS 245 17
Lock Table
• Dark rectangles indicate granted
locks, light colored ones indicate
waiting requests
• Lock table also records the type of
lock granted or requested
• New request is added to the end of
the queue of requests for the data
item, and granted if it is
compatible with all earlier locks
• Unlock requests result in the
request being deleted, and later
requests are checked to see if they
can now be granted
• If transaction aborts, all waiting or
granted requests of the transaction
are deleted
– lock manager may keep a list of locks
held by each transaction, to
implement this efficiently
8/4/2021 CS 245 18
Timestamp-Based Protocols
• Each transaction Ti is issued a timestamp TS(Ti)
when it enters the system.
– Each transaction has a unique timestamp
– Newer transactions have timestamps strictly greater
than earlier ones
– Timestamps can be implemented with
• System clock value
• logical counter
8/4/2021 CS 245 21
Timestamp-Based Protocols (Cont.)
• Suppose that transaction Ti issues write(Q).
1. If TS(Ti) < R-timestamp(Q), then the value of Q that Ti
is producing was needed previously, and the system
assumed that that value would never be produced.
➢ Hence, the write operation is rejected, and Ti is rolled back.
8/4/2021 CS 245 22
Example of Schedule Under TSO
▪ Is this schedule valid under
TSO?
Assume that initially:
R-TS(A) = W-TS(A) = 0
R-TS(B) = W-TS(B) = 0
8/4/2021 CS 245 23
Correctness of Timestamp-Ordering Protocol
• The timestamp-ordering protocol guarantees
serializability since all the arcs in the precedence
graph are of the form:
8/4/2021 CS 245 26
Snapshot Isolation
• Transactions are given two time stamps:
– StartTS(Ti) : time at which Ti started
– CommitTS(Ti):time at which Ti requested validation
• Updates performed by a transaction must be
validated before the transaction is allowed to
commit.
• Snapshot isolation is based on multiversioning
– each transaction that updates a data item creates a version
of the data item
8/4/2021 CS 245 27
Snapshot Isolation
• Versions have only one timestamp- write time stamp,
set to CommitTS(Ti) if it is created by Ti
• When Ti reads a data item, the latest version of data
item whose timestamp ≤ StartTS(Ti) is returned.
– Ti does not see the updates of any transactions that
committed after Ti started.
• Update transactions – validation-whether a
transaction should be allowed to commit
– Concurrent transactions operate in isolation, both trying to
update the same data item, neither of them sees the
update made by the other.
8/4/2021 CS 245 28
Snapshot Isolation
– If both transactions are allowed to commit, first update
overwritten by the second – lost update
• Variants of snapshot isolation that prevents lost
update
– First committer wins
– First updater wins
• If StartTS(Ti) ≤ StartTS(Tj) ≤ CommitTS(Ti) or
StartTS(Tj) ≤ StartTS(Ti) ≤ CommitTS(Tj) then
Tj is concurrent with Ti
8/4/2021 CS 245 29
Snapshot Isolation
• First committer wins
– For each data item d that Ti intends to write, if there is a
version of d whose time stamp is between StartTS(Ti) and
CommitTS(Ti) then Ti aborts.
– If no such data item is found , then Ti commits and its
updates are written to the database
• First updater wins
– Locking mechanisms is applied to updates
– Transaction requests write lock to update a data item
– If the lock is not held by a concurrent transaction, then
lock is acquired
8/4/2021 CS 245 30
Snapshot Isolation
– If the item has been updated by any concurrent
transaction, then Ti aborts.
– Otherwise Ti may proceed with its execution, including
possibly committing
• If the write lock is already held by another other
transaction Tj
– Ti waits until Tj aborts or commits
• If Tj aborts, the lock is released and Ti can acquire the lock and it is
checked if the data item is already updated by a concurrent
transaction. If yes then Ti aborts otherwise Ti proceeds.
• If Tj commits then Ti must abort.
8/4/2021 CS 245 31
Benefits of Snapshot Isolation
• Reads are never blocked,
– and also don’t block other txns activities
• Avoids several anomalies
– No dirty read, i.e. no read of uncommitted data
– No lost update
• I.e., update made by a transaction is overwritten by another
transaction that did not see the update)
– No non-repeatable read
• I.e., if read is executed again, it will see the same value
• Problems with Snapshot Isolation
– SI does not always give serializable executions
• Serializable: among two concurrent txns, one sees the effects
of the other
• In SI: neither sees the effects of the other
– Result: Integrity constraints can be violated
8/4/2021 CS 245 32
Snapshot Isolation
• Example of problem with SI
– Initially A = 3 and B = 17
• Serial execution: A = ??, B = ??
• if both transactions start at the same time,
with snapshot isolation: A = ?? , B = ??
• Called skew write
• SI breaks serializability when transactions modify
different items, each based on a previous state of
the item the other modified
• Another issue with SI is that integrity constraints cannot be
checked on snapshot-usually done outside of snapshot.
8/4/2021 CS 245 33
Serializable Snapshot Isolation
• Serializable snapshot isolation (SSI): extension of
snapshot isolation that ensures serializability
8/4/2021 CS 245 34
Summary
• We discussed the following:
– Concurrency control protocol-Locking protocol
– Two phase locking protocol (2PLP)- allows a transaction to
lock a new data item only if that transaction has not yet
unlocked any data item.
– 2 PLP guaratees serializability but is not deadlock free.
– Strict 2PLP and Rigorous 2PLP ensures recoverability and
cascadelessness.
8/4/2021 CS 245 35
Summary
• We discussed the following
– Timestamp-ordering protocol ensures serializability by
selecting an ordering in advance between every pair of
transactions.
– A unique fixed timestamp is associated with each
transaction and the timestamps determine the
serializability order.
– Snapshot isolation is a multiversion concurreny control
protocol based on validation
– Snapshot isolation does not guarantee serializability.
– Extension of SI, serializable SI guarantees serializability.
8/4/2021 CS 245 36