concurrency control

21
03/25/22 Concurrency Control Concurrency Control

Upload: lysandra-cannon

Post on 30-Dec-2015

41 views

Category:

Documents


0 download

DESCRIPTION

Concurrency Control. Concurrency Control. T1T2…Tn. DB (consistency constraints). Enforce Conflict Serializable Schedules. P revent cycles in precedence graph from occurring T 1 T 2 …..T n. Scheduler. DB. A locking protocol. - PowerPoint PPT Presentation

TRANSCRIPT

04/19/23

Concurrency Control Concurrency Control

Concurrency ControlConcurrency Control

T1T1 T2T2 …… TnTn

DB(consistencyconstraints)

PPrevent cycles in precedence graph revent cycles in precedence graph from occurring from occurring

TT1 1 TT22 ….. ….. TTnn

Scheduler

DB

Enforce Conflict Serializable Enforce Conflict Serializable SchedulesSchedules

A locking protocolA locking protocol

For transaction iFor transaction i UseUse li li to lock an item to lock an item Use Use uiui to unlock the lock enforced by to unlock the lock enforced by

transaction itransaction i

scheduler

T1 T2

locktable

Well behaved transactionsWell behaved transactions

TTii: … l: … lii(A) … p(A) … pii(A) … u(A) … uii(A) ...(A) ...

Example of a transaction Example of a transaction performing lockingperforming locking

TT11:: l1l1(A)(A);;

read read (A)(A);;

u1u1(A)(A);;

l1l1(B)(B);;

read read (B)(B);;

u1u1(B)(B);;

displaydisplay(A+B)(A+B)

Sufficient to guarantee serializability ?Sufficient to guarantee serializability ?

Example:Example:

T2:T2: Read(A)Read(A) T3:T3: Read(A)Read(A)

A A A+100 A+100 A A A A22

Write(A)Write(A) Write(A)Write(A)

Read(B)Read(B) Read(B)Read(B)

B B B+100 B+100 B B BB22

Write(B)Write(B) Write(B)Write(B)

Constraint: A=BConstraint: A=B

Schedule ASchedule A

T2T2 T3T3 25 25 25 25

l1(A);Read(A)l1(A);Read(A)

A A+100;Write(A);u1(A) A A+100;Write(A);u1(A) 125 125

l2(A);Read(A)l2(A);Read(A)

A Ax2;Write(A);u2(A) 250A Ax2;Write(A);u2(A) 250

l2(B);Read(B)l2(B);Read(B)

B Bx2;Write(B);u2(B) 50B Bx2;Write(B);u2(B) 50

l1(B);Read(B)l1(B);Read(B)

B B+100;Write(B);u1(B) B B+100;Write(B);u1(B) 150 150

250 150250 150

A B

Two-Phase Locking ProtocolTwo-Phase Locking Protocol

Phase 1: Growing PhasePhase 1: Growing Phase transaction may obtain locks transaction may obtain locks transaction may not release lockstransaction may not release locks

Phase 2: Shrinking PhasePhase 2: Shrinking Phase transaction may release lockstransaction may release locks transaction may not obtain lockstransaction may not obtain locks

TTi i = ……. l= ……. lii(A) ………... u(A) ………... uii(A) ……...(A) ……...

no unlocks no locks

# locks# locks

held byheld by

TiTi

TimeTime

GrowingGrowing Shrinking Shrinking

PhasePhase Phase Phase

What happens to a transaction which tries What happens to a transaction which tries to lock an item but failed?to lock an item but failed?

Schedule B

T2 T3

l1(A);Read(A)

A A+100;Write(A)

l1(B); u1(A)

l2(A);Read(A)

A Ax2;Write(A); l2(B)l2(B)

Read(B);B B+100

Write(B); u1(B)

l2(B); u2(A);Read(B)

B Bx2;Write(B);u2(B);

delayed

2PL 2PL conflict-serializable schedules? conflict-serializable schedules?

To help in proof:Definition Shrink(Ti) = SH(Ti) = first unlock action of Ti

First:First:

Ti Ti Tj in S Tj in S SH(Ti) < SH(Ti) <SS SH(Tj) SH(Tj)

Proof:Ti Tj means that

S = … pi(A) … ui(A) … lj(A) ... qj(A) …

Then:Then:

(1) Assume P(S) has cycle (1) Assume P(S) has cycle TT1 1 TT2 2 …. …. TTn n TT11

(2) By lemma: SH(T(2) By lemma: SH(T11) <) < SH(TSH(T22) <) < ... ... << SH(TSH(T11))

(3) Impossible, so P(S) acyclic(3) Impossible, so P(S) acyclic

(4) (4) S is conflict serializable S is conflict serializable

DeadlockDeadlock

To handle a deadlock one of To handle a deadlock one of TT44 or or TT55 must be must be rolled back and its locks released.rolled back and its locks released.

T4 T5

l3(B)

read(B)

write(B)

l4(A)

read(A)

l4(B)

l3(A)

StarvationStarvation

A transaction does not get its turn for a long timeA transaction does not get its turn for a long time Example:Example:

A transaction may be waiting for a lock on an A transaction may be waiting for a lock on an item, while a sequence of other transactions item, while a sequence of other transactions request and are granted an lock on the same request and are granted an lock on the same item. item.

The same transaction is repeatedly rolled back The same transaction is repeatedly rolled back due to deadlocks.due to deadlocks.

Concurrency control manager can be designed to Concurrency control manager can be designed to prevent starvation.prevent starvation.

2PL and Deadlock2PL and Deadlock

Are schedules from 2PL transactions Are schedules from 2PL transactions deadlock free?deadlock free?

2PL and Possible Schedules2PL and Possible Schedules

Does 2PL allow all possible conflict Does 2PL allow all possible conflict serializable schedules?serializable schedules?

Beyond this simple 2PL protocol, it is all Beyond this simple 2PL protocol, it is all a matter of improving performance and a matter of improving performance and allowing more concurrency….allowing more concurrency…. Shared locksShared locks Multiple granularityMultiple granularity Inserts, deletes and phantomsInserts, deletes and phantoms Other types of C.C. mechanismsOther types of C.C. mechanisms