Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Concurrency Control Techniques: By, Dr. P. Gayathri Associate Professor Scope VIT University

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

Concurrency Control

Techniques

By,
Dr. P. Gayathri
Associate Professor
SCOPE
VIT University

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Database Concurrency Control
 Purpose of Concurrency Control
 To enforce non-interference or isolation property of
concurrently executing operations.
 To preserve database consistency through consistency
preserving execution of transactions.
 To resolve conflicts between concurrently executing
transactions over data items i.e. 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.
Dr. P. Gayathri, Associate Professor, SCOPE, VIT
Concurrency control Protocols
 Concurrency control techniques ensures serializability
of schedules by using procotols (set of rules)
 Locking method
 Timestamp method
 Multi version method
 Validation method

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Locking - Two-Phase Locking
Techniques
 Locking data items
 Lock – variable associated with data item. It describes
the status of the data item with respect to possible
operations that can be applied on it.
 Generally one lock for each data item in the DB

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Locking - Two-Phase Locking
Techniques
 Two phases – lock and unlock
 Locking is an operation which secures
 (a) permission to Read
 (b) 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.

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Two-Phase Locking Techniques
 Types:
 Binary locks – not used in practice
 Shared/exclusive locks – practical DB locking scheme

Essential components
 Lock Manager:
 Controls or Manages locks on data items.
 Lock table:
 Lock manager uses it to store the identity of transaction
locking a data item.
 Items not in the lock table are considered to be unlocked.

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Binary locks
 Lock variable has 2 states – locked (1); unlocked (0)
 Distinct lock is associated with each DB item X
 If value of lock on X is 1, X has been lock by some
transaction and it cannot be accessed by other
transactions
 If value is 0, then it can be accessed by one of the other
transaction by issuing lock request.

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Binary locks
 Two operations – lock_item and unlock_item
 Lock_item(X) – Transaction T has to first issue lock_item(X)
operation in order to gain access to X
 If lock(X) = 1, transaction is made to wait.
 If lock(X) = 0, it is set to 1 and transaction is allowed to access
X
 If it completes its access on X, it issues unlock_item(X)
operation, which sets lock(X) = 0. so that X can be accessed by
other transactions.

 Binary lock enforces mutual exclusion on data items.

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Lock and unlock operations for
Binary locks

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Binary locks – locking rules
 Lock manger enforces these rules
 Rules:
 Transaction T issue lock_item(X) before any read or write
operation on X
 T issue unlock_item(X) after all operations are completed
 T will not issue lock_item(X) it already it holds lock on
item X
 T will not issue unlock_item(X) unless it holds lock on item
X

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Binary locks – lock table
 Contains <dataitem name, LOCK,
Locking_Transaction>
 Also maintains queue to put the transactions that are
waiting to access the data item.

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Shared / Exclusive locks
 Binary locks – atmost one transaction can hold the lock on
data item

 Alternate option – allow several transaction to access data


item X for reading purpose and if transaction has to write X,
it must have exclusive access to X.

 For this purpose type of lock called multiple mode lock


(shared/ exclusive lock or read/write lock) is used

 Three operations – read_lock(X), write_lock(X), unlock(X)

 Lock associated with X i.e. Lock(X) – has three possible


states – read-locked, write-locked or unlocked.

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Shared / Exclusive locks
 Two locks modes:
 (a) shared (read) (b) exclusive (write).
 Shared mode: read 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
Write

N N

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Shared / Exclusive locks
 Lock table:

Transaction ID Data item id lock mode Ptr to next data item


T1 X1 Read Next

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Shared / Exclusive lock and
unlock operations
 The following code performs the read operation:

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Shared / Exclusive lock and
unlock operations
 The following code performs the write lock operation:

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Shared / Exclusive lock and
unlock operations
 The following code performs the unlock operation:

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Two-Phase Locking
Techniques: Lock Conversion
 Lock conversion
 Converting lock from one locked state to another

 Lock upgrade: existing read lock to write lock


if Ti has a read-lock (X) and Tj has no read-lock (X) (i  j) then
convert read-lock (X) to write-lock (X)
else
force Ti to wait until Tj unlocks X

 Lock downgrade: existing write lock to read lock


Ti has a write-lock (X) (*no transaction can have any lock on X*)
convert write-lock (X) to read-lock (X)

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Two-Phase Locking –
Guaranteeing serializability
 Two Phases:
 (a) Locking (Growing)
 (b) Unlocking (Shrinking).
 Locking (Growing) Phase:
 A transaction applies locks (read or write) on desired data items one
at a time.
 New locks on items can be acquired but none can be released
 Unlocking (Shrinking) Phase:
 A transaction unlocks its locked data items one at a time.
 Existing locks can be released but no new locks can be acquired
 Requirement: 2PL policy
 For a transaction these two phases must be mutually exclusively,
that is, during locking phase unlocking phase must not start and
during unlocking phase locking phase must not begin.

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Two-Phase Locking Techniques: no
guaranteed serializability
Example: serial schedule

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);
Reason: Y in T1 and X in T2 were unlocked too early

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Two-Phase Locking Techniques: no
guaranteed serializability
Example – non serial schedule

T1 T2 Result
read_lock (Y); X=50; Y=50
read_item (Y); Nonserializable
unlock (Y);
read_lock (X);
read_item (X);
unlock (X);
write_lock (Y);
Time 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);

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Two-Phase Locking Techniques:
guaranteed serializability
Example:

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);

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Variations of Two Phase Locking
(2PL)
 Basic:
 Discussed so far in previous slides
 Results in deadlock because transaction locks data items incrementally.

 Conservative (static):
 Prevents deadlock by locking all desired data items before transaction
begins execution.
 Pre-declaring read set (set of all items that transaction reads) and write set
(set of all items that transaction writes)
 If any of the pre-declared items cannot be locked, transaction does not lock
any item and it waits until all items are available for locking
 Deadlock free protocol
 Difficult to use in practice because pre-declaration of read set and write set
is not possible in most situations

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Variations of Two Phase Locking
(2PL)
 Strict
 Guarantees strict schedules
 Transaction T does not release any of its exclusive (write)
locks until after it commits or aborts
 So no other transaction can read or write an item written by T
unless T has committed
 Not deadlock free
 Most commonly used 2PL
 Rigorous
 Variation of strict 2PL
 Transaction T does not release any of its locks (shared or
exclusive) until after it commits or aborts

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Conservative Vs Rigorous
 Conservative:
 Locks all items before it starts
 So once transaction starts, it will be shrinking phase (existing
locks can be released but no new locks can be acquired)

 Rigorous:
 Does not unlock any of its item until after it terminates (by
committing or aborting)
 Transaction is in expanding phase (new locks can be acquired
but none can be released) until it ends

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Dealing with deadlock and
starvation
 Deadlock occurs when each transaction T in a set of two or
more transactions, is waiting for some item that is locked
by some other transaction T’ in the set.
 Hence each transaction in the set is waiting for one of the
other transaction in the set to release the lock on the item.
 Example: wait-for graph representation

 Deadlock (T’1 and T’2) – but follows 2PL policy


Dr. P. Gayathri, Associate Professor, SCOPE, VIT
Deadlock prevention
 A transaction locks all data items it refers to before it
begins execution.
 If any of the item cannot be obtained, none of the items
are locked
 Practically not possible to determine the access to
required data items
 This way of locking prevents deadlock since a
transaction never waits for a data item.
 The conservative two-phase locking uses this approach.
 Transaction tries again to lock all of the items it needs.
 This limits the concurrency.

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Deadlock detection
 In this approach, deadlocks are allowed to happen.
 The scheduler maintains a wait-for-graph for detecting cycle.
 If a cycle exists, then one transaction involved in the cycle is
selected (victim) and rolled-back.
 A wait-for-graph is created using the lock table.
 As soon as a transaction is blocked, it is added to the graph.
When a chain like: Ti waits for Tj waits for Tk waits for Ti or
Tj occurs, then this creates a cycle.

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Deadlock avoidance
 Some avoid deadlock by not letting the cycle to complete.
 That is as soon as the algorithm discovers that blocking a
transaction is likely to create a cycle, it rolls back the
transaction.
 Wound-Wait and Wait-Die algorithms use timestamps to
avoid deadlocks by rolling-back victim.
 Wait-die: if TS(Ti) < TS(Tj), Ti is allowed to wait (Ti is older)
Else Ti gets aborted i.e. dies (Ti is younger). Gets restarted
later with same timestamp
 Wound-Wait: if TS(Ti) < TS(Tj), Tj gets aborted (Ti is older –
Ti wounds Tj) Tj is restarted later with same timestamp
Else Ti is allowed to wait (Ti is younger)

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Starvation
 Starvation occurs when a particular transaction consistently
waits or restarted and never gets a chance to proceed further.
 In a deadlock resolution it is possible that the same
transaction may consistently be selected as victim and rolled-
back.
 This limitation is inherent in all priority based scheduling
mechanisms.
 In Wound-Wait scheme a younger transaction may always be
wounded (aborted) by a long running older transaction
which may create starvation.

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Timestamp based concurrency
control
 Timestamp
 A monotonically increasing variable (integer) indicating
the age of a transaction.
 A larger timestamp value indicates a more recent
transaction.
 Timestamp based algorithm uses timestamp to serialize
the execution of concurrent transactions.
 Do not use locks and hence no deadlocks.

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Time stamp based algorithms
 Associates two time stamp values with each DB item X

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Time stamp based algorithms
 Basic Timestamp Ordering
 1. 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 the data item so abort and roll-back
T and reject the operation.
 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).

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Time stamp based algorithms
 Strict Timestamp Ordering
Transaction T issues a write_item(X) or read_item(X)
request such that:
TS(T) > write_TS(X), then delay T until the transaction T’ that
wrote X has terminated (committed or aborted).

Dr. P. Gayathri, Associate Professor, SCOPE, VIT


Time stamp based algorithms
 Thomas’s Write Rule – modification of basic TO
algorithm
 If read_TS(X) > TS(T) then abort and roll-back T and
reject the operation.
 If write_TS(X) > TS(T), then just ignore the write
operation and continue execution. This is because the
most recent transactions has already written value of X.
X value written by T has become outdated.
 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).

Dr. P. Gayathri, Associate Professor, SCOPE, VIT

You might also like