Beruflich Dokumente
Kultur Dokumente
Concurrency Control
Techniques
18.1 p 583-594
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Pearson Education, Inc.
Note
Chapter 18-4
18.1 Database Concurrency Control
Chapter 18-5
Database Concurrency Control
Chapter 18-6
Database Concurrency Control
Binary Locks
A binary lock is associated with each database item X.
A binary lock is either locked or unlocked.
If locked, no other operation can access X until unlocked
(the requesting transaction if forced to wait).
Hence, mutual exclusion is enforced on all data items.
Y N
Write
N N
Note: Y = OK p 586
Chapter 18-8
DBMS Components
DBMS
Buffer Manager
Recovery Manager
Lock Manager
Chapter 18-9
Database Concurrency Control
Chapter 18-10
Database Concurrency Control
Chapter 18-11
Database Concurrency Control
Chapter 18-13
Database Concurrency Control
Chapter 18-15
Database Concurrency Control
Two-Phase Locking Techniques: Essential components
The following code performs the unlock operation:
if LOCK (X) = “write-locked” then
begin LOCK (X) ← “unlocked”;
wakes up one of the transactions, if any
end
Chapter 18-17
18.1.2 Guaranteeing Serializability By
Two-Phase Locking
Two-Phase Locking Techniques: The algorithm
Chapter 18-18
Database Concurrency Control
T1 T2 Result
read_lock (Y); read_lock (X); Initial values: X=20; Y=30
read_item (Y); read_item (X); Result of serial execution
unlock (Y); unlock (X); T1 followed by T2
write_lock (X); Write_lock (Y); X=50, Y=80.
read_item (X); read_item (Y); Result of serial execution
X:=X+Y; Y:=X+Y; T2 followed by T1
write_item (X); write_item (Y); X=70, Y=50
unlock (X); unlock (Y);
Chapter 18-19
Database Concurrency Control
Two-Phase Locking Techniques: The algorithm
T1 T2
read_lock (Y); Result
read_item (Y); X=50; Y=50
unlock (Y); Nonserializable because
read_lock (X); it violates the two-phase
Time read_item (X); locking protocol.
unlock (X);
write_lock (Y); This schedule is not
read_item (Y); equivalent to a serial
Y:=X+Y; schedule where the
write_item (Y); operations of each
unlock (Y); transaction are executed
write_lock (X); independently.
read_item (X);
X:=X+Y;
write_item (X);
unlock (X);
Chapter 18-20
Definition Schedule
Schedule
When transactions are executing concurrently in
interleaved fashion, then the order of execution of
operations from the various transactions is known as
a schedule.
Serial Schedule
A schedule is serial if, for every transaction T participating
in the schedule, the operations of T are executed
consecutively
Serializable Schedule
A schedule S of n transactions is serializable if it is
equivalent to some serial schedule of the same n
transactions.
Chapter 18-21
Database Concurrency Control
Two-Phase Locking Techniques: The algorithm
T'1 T’2
read_lock (Y); read_lock (X); T1 and T2 follow two-phase
read_item (Y); read_item (X); policy but they are subject to
write_lock (X); write_lock (Y); deadlock, which must be
unlock (Y); unlock (X); dealt with.
read_item (X); read_item (Y);
X:=X+Y; Y:=X+Y;
write_item (X); write_item (Y);
unlock (X); unlock (Y);
Chapter 18-22
Database Concurrency Control
Two-Phase Locking Techniques: The algorithm
Two-phase policy generates two locking algorithms (a) Basic and (b)
Conservative.
Conservative: Prevents deadlock by locking all desired data items before
transaction begins execution.
Basic: Transaction locks data items incrementally. This may cause deadlock
which is dealt with.
Strict: A more strict version of Basic algorithm where unlocking is
performed after a transaction terminates (commits or aborts and rolled-back).
This is the most commonly used two-phase locking algorithm.
Chapter 18-23
18.1.3 Database Concurrency Control
Dealing with Deadlock and Starvation
Deadlock
T’1 T’2
read_lock (Y); T1 and T2 did follow two-phase
read_item (Y); policy but they are deadlocked
read_lock (X);
read_item (Y);
write_lock (X);
(waits for X) write_lock (Y);
(waits for Y)
Deadlock (T’1 and T’2)
Chapter 18-24
Database Concurrency Control
Dealing with Deadlock and Starvation
Deadlock prevention
A transaction locks all data items it refers to before it begins
execution.
This way of locking prevents deadlock since a transaction never
waits for a data item. The conservative two-phase locking uses
this approach.
Chapter 18-25
Database Concurrency Control
Dealing with Deadlock and Starvation
Chapter 18-26
Database Concurrency Control
Dealing with Deadlock and Starvation
Deadlock avoidance
There are many variations of two-phase locking algorithm. Some avoid
deadlock by not letting the cycle complete.
Chapter 18-27
Database Concurrency Control
Dealing with Deadlock and Starvation
Starvation
Starvation occurs when a particular transaction consistently waits or
restarted and never gets a chance to proceed further.
Chapter 18-28
Notes
Chapter 18-29
Schedule Challenge # 103
T1 T2
read_item (X);
X=X+Y;
write_item (X);
unlock (X);
unlock (Y)
Chapter 18-30
Schedule Challenge # 104
T1 T2
T1 T2
Chapter 18-33
Notes
Chapter 18-34
18.2 Database Concurrency
Control
Timestamp based concurrency control algorithm
Timestamp
A monotonically increasing variable (integer) indicating the age of an
operation or a transaction. A larger timestamp value indicates a more
recent event or operation.
Timestamp based algorithm uses timestamp to serialize the execution of
concurrent transactions.
Chapter 18-35
Database Concurrency Control
Timestamp based concurrency control algorithm
Basic Timestamp Ordering
1. Transaction T issues a write_item(X) operation:
a. If read_TS(X) > TS(T) or if write_TS(X) > TS(T), then an younger
transaction has already read the data item so abort and roll-back T
and reject the operation.
b. If the condition in part (a) does not exist, then execute write_item(X)
of T and set write_TS(X) to TS(T).
2. Transaction T issues a read_item(X) operation:
If write_TS(X) > TS(T), then an younger transaction has already
written to the data item so abort and roll-back T and reject the
operation.
If write_TS(X) ≤ TS(T), then execute read_item(X) of T and set
read_TS(X) to the larger of TS(T) and the current read_TS(X).
Chapter 18-36
Chapter 18
Chapter 18-39
FIGURE
18.2
Locking and
unlocking
operations
for two-
mode
(read-write
or shared-
exclusive)
locks.
Chapter 18-40
FIGURE 18.3 Transactions that do not obey
two-phase locking. (a) Two transactions T1 and T2. (b)
Results of possible serial schedules of T1 and T2.
Chapter 18-41
FIGURE 18.3 Transactions that do not obey
two-phase locking. (c) A nonserializable schedule S
that uses locks.
Chapter 18-42
FIGURE 18.4 Transactions T1′ and T2 ′, which are
the save as T1 and T2 of Figure 18.3 but which
allow the two-phase locking protocol. Not that they
can produce a deadlock.
Chapter 18-43
FIGURE 18.5 Illustrating the deadlock problem.
(a) A partial schedule of T1′ and T2 ′ that is in a
state of deadlock. (b) A wait-for graph for the
partial schedule in (a).
Chapter 18-44
FIGURE 18.6 Lock compatibility tables. (a) A
compatibility table for read/write locking scheme.
(b) A compatibility table for read/write/certify locking
scheme.
Chapter 18-45
FIGURE 18.7 A granularity
hierarchy for illustrating multiple
granularity level locking.
Chapter 18-46
FIGURE 18.8 Lock compatibility
matrix for multiple granularity
locking.
Chapter 18-47
FIGURE 18.9
Lock
operations to
illustrate a
serializable
schedule.
Chapter 18-48