Docsity

concurrency control in database, Study notes of Database Management Systems (DBMS)

Partial preview of the text

Download concurrency control in database and more Study notes Database Management Systems (DBMS) in PDF only on Docsity!

Concurrency Control^ •^ Lock-Based Protocols^ •^ Timestamp-Based Protocols^ •^ Validation-Based Protocols

Lock-Based Protocols^ •^ A lock is a mechanism to control concurrent access to a dataitem^ •^ 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.

Lock-Based Protocols (Cont.) • Example of a transaction performing locking:

T :^ lock-S^2

(A) ; read (A) ; unlock (A) ; lock-S (B) ; read (B) ; unlock (B) ; display (A+B)

-^ Locking as above is not sufficient to guarantee serializability — if

A^ and^ B

get updated in-between the read of

A^ and^ B , the displayed sum would be

wrong. • A^ locking protocol

is a set of rules followed by all transactions while requesting and releasing locks. Locking protocols restrict the set ofpossible schedules.

Pitfalls of Lock-Based Protocols^ •^ Consider the partial schedule^ •^ Neither

T nor^ T^3

can make progress — executing

lock-S (B)

causes^ T^4

to wait for

T to release its lock on^3

B , while executing

lock-X (A)

causes^ T^3

to wait for

T to release its lock on^4

A.

-^ Such a situation is called a

deadlock

.

-^ To handle a deadlock one of

T or^ T must be rolled back^3 and its locks released.

The Two-Phase Locking Protocol^ •^ This is a protocol which ensures conflict-serializableschedules.^ •^ 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 • The protocol assures serializability. It can be proved thatthe transactions can be serialized in the order of their

lock

points^ (i.e. the point where a transaction acquired itsfinal lock).

The Two-Phase Locking Protocol(Cont.)^ •^ 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

-^ Rigorous two-phase locking

is even stricter: here

all

locks are held till commit/abort

. In this protocol

transactions can be serialized in the order in which theycommit.

2PL with Lock Conversions • Two-phase locking with lock conversions:–^ First 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)–^ Second Phase:^ •^ can release a lock-S^ •^ can release a lock-X^ •^ can convert a lock-X to a lock-S (downgrade) • This protocol assures

serializability

. But still relies on the

programmer to insert the various locking instructions.

Automatic Acquisition of Locks^ •^ A transaction

T issues the standard read/write instruction,i^ without explicit locking calls. • The operation

read ( D ) is processed as: if T has a lock on i^

D

then^ read(

D )

else begin^ if necessary wait until no other

transaction has a

lock-X^

on^ D

grant^ T a i^

lock-S^

on^ D ; read( D ) end

Implementation of Locking •^ A^ lock manager

can be implemented as a

separate process

to which transactions send lock and unlock requests • 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 a data-structure called a

lock

table^ to record granted locks and pending requests • The lock table is usually implemented as an in-memory hashtable indexed on the name of the data item being locked

Lock Table

-^ Black rectangles indicate granted locks,^ white ones indicate waiting requests. •^ Lock table also

records the type of lock granted or requested

.

-^ New request is added to the end of thequeue^ of requests for the data item, andgranted^

if^ it^ is^ compatible

with^ all earlier locks. • Unlock requests result in the requestbeing deleted, and later requests arechecked^ to

see^ if^ they

can^ now^

be

granted. • If^ transaction

aborts,^ all

waiting^

or

granted requests of the transaction aredeleted.^ •^ lock manager may keep a list oflocks held by each transaction

, to

implement this efficiently GrantedWaiting

Tree Protocol

1.Only exclusive locks are allowed. 2.The first lock by

T may be on any data item. Subsequently, a i^ data^ Q^

can be locked by

T only if the parent of i^

Q^ is

currently locked by

T. i 3.Data items may be unlocked at any time. 4.A data item that has been locked and unlocked by

T cannot i^

subsequently be relocked by

Ti

Graph-Based Protocols (Cont.)^ •^ The^ tree

protocol

ensures

conflict

serializability

as^ well

as

freedom from deadlock. • Unlocking may occur earlier in the tree-locking protocol than inthe two-phase locking protocol.^ •^ shorter waiting times, and increase in concurrency^ •^ protocol is deadlock-free, no rollbacks are required • Drawbacks^ •^ Protocol does not guarantee recoverability or cascade freedom^ •^ Need

to^ introduce

commit

dependencies

to^ ensure

recoverability • Transactions may have to lock data items that they do notaccess. • increased locking overhead, and additional waiting time • potential decrease in concurrency • Schedules not possible under two-phase locking are possibleunder tree protocol, and vice versa.

Example of Granularity Hierarchy

The levels, starting from the coarsest (top) level are • database • area • file • record

Intention Lock Modes • In addition to S and X lock

modes,

there are

three

additional lock modes with multiple granularity:^ •^ intention-shared

(IS): indicates explicit

locking at a

lower level

of the tree but only with shared locks.

-^ intention - exclusive

(IX): indicates explicit

locking at a

lower level

with exclusive or shared locks

-^ shared

and^ intention

- exclusive

(SIX):^

the^ sub-tree

rooted^ by that node is

locked explicitly in shared mode and explicit locking is being done at a

lower level with

exclusive-mode locks. • intention locks allow a higher level node to be locked in Sor X mode without having to check all descendent nodes.