Chapter 4-Concrruncy Controling Techniques
Chapter 4-Concrruncy Controling Techniques
nt Sy
e m e
n a g
M a S)
b a s e B M
a t a e d D
e d D a n c 1 )
a nc A d v c 3 0 7
Ad v ( e
(I T
Chapter Four
Lock Scheduler
Table
Read and Writes
Buffers
1. 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.
A typical scheduler does its work by maintaining locks on certain
pieces of the database. These locks prevent two transactions from
accessing the same piece of data at the same time.
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.
Example:
A = 500
Account B = 500
Balances
C = 500
Property: A + B + C = 1500
t = t - 100 s = s - 100
Write (A, t) Write (A, s)
Read (B, t) Read (C, s)
t = t + 100 s = s + 100
Write (B, t) Write (C, s)
Transaction T1 Transaction T2 A B C
Read (C, s)
s = s + 100
Write (C, s) 400 600 600
Schedule
400 + 600 + 600 = 1600
Transaction T1 Transaction T2 A B C
Read (C, s)
s = s + 100
Alternative Write (C, s) 300 600 600
Schedule
300 + 600 + 600 = 1500
So What ?
2. Concurrency Control Techniques
Y N
N N
Write
Lock Manager:
Managing locks on data items.
Lock table:
Lock manager uses it to store the identity of transaction
locking a data item, the data item, lock mode and pointer
to the next data item locked. One simple way to implement a
lock table is through linked list.
T1 T2
Schedule: A Write Lock( X)
Read (X)
X=X+100
Write(X)
Unlock(X)
Write Lock( X)
Read (X)
X= X*1.1
Write(X)
Unlock(X)
Write
Lock( Y)
Read (Y)
Y= Y*1.1
Write(Y)
Unlock(Y)
Commit
Write Lock( Y)
Read(Y)
Y= Y-100
Write(Y)
Unlock(Y)
Commit
Cont’d..
# locks
held by
Ti
read_lock (Y);
read_item (Y);
Result
unlock (Y); X=50; Y=50
Nonserializable because it
read_lock violated two-phase policy.
(X);
read_item
(X);
unlock
(X);
Time write_lock
(Y);
read_item
(Y);
Y:=X+Y;
write_item
(Y);
unlock
(Y);
write_lock (X);
read_item (X);
X:=X+Y;
write_item (X);
unlock (X);
T’1 T’2
read_lock (Y); read_lock (X);
read_item (Y);read_item (X); T’1 and T’2 follow two-phase
write_lock (X); Write_lock (Y); policy but they are subject to
deadlock, which must be dealt
unlock (Y); unlock (X); . with ;
read_item (X);read_item (Y); Refere :- page 784
X:=X+Y; Y:=X+Y;
write_item (X); write_item (Y);
unlock (X); unlock (Y);
Deadlock
Starvation
Dealing with Deadlock and Starvation
• Deadlock
– It is a state that may result when two or more transaction are
each waiting for locks held by the other to be released
– Example :
T1 T2
read_lock (Y);
read_item (Y);
read_lock (X);
read_item (X);
write_lock (X);
write_lock (Y);
wait wait
wait
T1(ts =25)
wait wait
T2 (ts =20)
wait
T3 (ts =10) 27
Remark:
− Both methods ended by aborting the younger of the two transaction that
may be involved in the deadlock
− Limitation:
−Both techniques may cause some transaction to be aborted and
restarted unnecessarily
ii. Deadlock Detection and resolution
– In this approach, deadlocks are allowed to happen
– The scheduler maintains a wait-for-graph for detecting cycle.
– When a chain like: Ti waits for Tj waits for Tk waits for Ti or Tj occurs,
then this creates a cycle.
– When the system is in the state of deadlock , some of the transaction
should be aborted by selected (victim) and rolled-back
– This can be done by aborting those transaction: that have made the least
work, the one with the lowest locks, and that have the least # of abortion
and so on
Example:
iii.Timeouts
– It uses the period of time that several transaction have been
waiting to lock items
– It has lower overhead cost and it is simple
– If the transaction wait for a longer time than the predefined time
out period, the system assume that may be deadlocked and aborted
it
Starvation
– Starvation occurs when a particular transaction consistently waits
or restarted and never gets a chance to proceed further while other
transaction continue normally
– This may occur , if the waiting method for item locking:
• Gave priority for some transaction over others
• Problem in Victim selection algorithm- it is possible that the same
transaction may consistently be selected as victim and rolled-
back .example In Wound-Wait
Solution :-
FIFO
Allow for transaction that wait for a longer time
Give higher priority for transaction that have been aborted for
many time
2. 2.Timestamp based concurrency control algorithm
ʘ Timestamp
– In lock based concurrency control , conflicting actions of different
transactions are ordered by the order in which locks are obtained.
– But here, Timestamp values are assigned based on time in which the
transaction are submitted to the system using the current date & time of the
system
– 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.
– It doesn’t use lock, thus deadlock cannot be occurred
– In the timestamp ordering, conflicting operation in the schedule shouldn’t
violate serilazable ordering
– This can be achieved by associating timestamp value (TS) to each database
item which is denoted as follow:
a) Read_Ts(x): the read timestamp of x – this is the largest time among all the
time stamps of transaction that have successfully read item X
b) Write_TS(X): the largest of all the timestamps of transaction that have
successfully written item X
The concurrency control algorithm check whether conflict operation violate
the timestamp ordering in the following manner: three options
i. Basic Timestamp Ordering
Transaction T issues a write_item(X) operation:
•If read_TS(X) > TS(T) or if write_TS(X) > TS(T), then an younger
transaction has already read/write the values of the data item x before
T had a chance to write X . so abort and roll-back T and restarted
with a new, larger timestamp. Why is with new timestamp?, is there a difference
b/n this timestamp protocol and the 2PL for dead lock prevention?
•If the condition above does not exist, then execute write_item(X)
of T and set write_TS(X) to TS(T).
Transaction T issues a read_item(X) operation:
•If write_TS(X) > TS(T), then a 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)
– Limitation: cyclic restart/starvation may occur when a transaction is
continuously aborted and restarted
ii. Strict Timestamp Ordering
1. Transaction T issues a write_item(X) operation:
• If TS(T) > read_TS(X), then delay T until the transaction T’
that wrote or read X has terminated (committed or aborted).
2. Transaction T issues a read_item(X) operation:
• If TS(T) > write_TS(X), then delay T until the transaction T’
that wrote X has terminated (committed or aborted).
iii. Thomas’s Write Rule : Modification of Basic Time ordering Algo
A transaction T issue a write_Item(X) Operation:
1. If read_TS(X) > TS(T) then abort and roll-back T and reject the
operation.
2. If write_TS(X) > TS(T), then just ignore the write operation and
continue execution. This is because the most recent writes counts in
case of two consecutive writes. Example
3. If the conditions given in 1 and 2 above do not occur, then execute
write_item(X) of T and set write_TS(X) to TS(T).
B
A
The Thomas Write Rule relies on the observation that T2's write is never seen
by any transaction
2.3 VALIDATION (OPTIMISTIC) CONCURRENCY CONTROL
TECHNIQUES
• In all the concurrency control techniques we have discussed so far, a certain
degree of checking is done before a database operation can be executed.
For example, in locking, a check is done to determine whether the item being
accessed is locked.
• In timestamp ordering, the transaction timestamp is checked against the read
and write timestamps of the item.
• In optimistic concurrency control techniques, also known as validation or
certification techniques, no checking is done while the transaction is executing.
• In this scheme, updates in the transaction are not applied directly to the
database items until the transaction reaches its end.
• During transaction execution, all updates are applied to local copies of the data
items that are kept for the transaction.
Cont’d...
There are three phases for this concurrency control protocol:
Read phase: A transaction can read values of committed data items from
the database. However, updates are applied only to local copies (versions)
of the data items kept in the transaction workspace.
Validation phase: Checking is performed to ensure that serializability will
not be violated if the transaction updates are applied to the database.
Write phase: If the validation phase is successful, the transaction updates
are applied to the data base; otherwise, the updates are discarded and the
transaction is restarted.
2. 4. Multiversion Concurrency Control Techniques
– This approach maintains a number of versions of a data item and allocates
the right version to a read operation of a transaction.
Thus unlike other mechanisms a read operation in this mechanism is never
rejected.
This algorithm uses the concept of view serilazabilty than conflict
serialiazabilty
Side effect:
Significantly more storage (RAM and disk) is required to maintain
multiple versions.
To check unlimited growth of versions, a garbage collection is run
when some criteria is satisfied.
GROUP ASSIGNMENT(20%)(Group 1,3,)(W2)
Basic, Conservative, Strict, and Rigorous Two-Phase Locking.
Granularity of Data Items and Multiple Granularity Locking
Using Locks for Concurrency Control in Indexes
Other Concurrency Control Issues
:- Insertion, Deletion, and Phantom Records
:- Interactive Transactions
:-Latches, When are latches used?
Discuss two multiversion techniques for concurrency control.
What is a certify lock?
What are the advantages and disadvantages of using certify locks?