+ All Categories
Home > Documents > Concurrency Control I

Concurrency Control I

Date post: 06-Jan-2016
Category:
Upload: alena
View: 43 times
Download: 0 times
Share this document with a friend
Description:
Concurrency Control I. 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
21
3/9/2005 Yan Huang - CSCI5330 Database Implementation – Concurrency Control Concurrency Control I
Transcript
Page 1: Concurrency Control I

3/9/2005 Yan Huang - CSCI5330 Database Implementation – Concurrency Control

Concurrency Control I

Page 2: Concurrency Control I

3/9/2005 Yan Huang - CSCI5330 Database Implementation – Concurrency Control

Concurrency Control

T1 T2 … Tn

DB(consistencyconstraints)

Page 3: Concurrency Control I

3/9/2005 Yan Huang - CSCI5330 Database Implementation – Concurrency Control

Prevent cycles in precedence graph from occurring

T1 T2 ….. Tn

Scheduler

DB

Enforce Conflict Serializable Schedules

Page 4: Concurrency Control I

3/9/2005 Yan Huang - CSCI5330 Database Implementation – Concurrency Control

A locking protocol

For transaction i Use li to lock an item Use ui to unlock the lock enforced by transaction i

scheduler

T1 T2

locktable

Page 5: Concurrency Control I

3/9/2005 Yan Huang - CSCI5330 Database Implementation – Concurrency Control

Well behaved transactions

Ti: … li(A) … pi(A) … ui(A) ...

Page 6: Concurrency Control I

3/9/2005 Yan Huang - CSCI5330 Database Implementation – Concurrency Control

Example of a transaction performing locking

T1: l1(A);

read (A);

u1(A);

l1(B);

read (B);

u1(B);

display(A+B)

Sufficient to guarantee serializability ?

Page 7: Concurrency Control I

3/9/2005 Yan Huang - CSCI5330 Database Implementation – Concurrency Control

Example:

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

A A+100 A A2

Write(A) Write(A)

Read(B) Read(B)

B B+100 B B2

Write(B) Write(B)

Constraint: A=B

Page 8: Concurrency Control I

3/9/2005 Yan Huang - CSCI5330 Database Implementation – Concurrency Control

Schedule A

T2 T3 25 25

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

l2(A);Read(A)A Ax2;Write(A);u2(A) 250l2(B);Read(B)B Bx2;Write(B);u2(B) 50

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

250 150

A B

Page 9: Concurrency Control I

3/9/2005 Yan Huang - CSCI5330 Database Implementation – Concurrency Control

Two-Phase Locking Protocol

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

Page 10: Concurrency Control I

3/9/2005 Yan Huang - CSCI5330 Database Implementation – Concurrency Control

Ti = ……. li(A) ………... ui(A) ……...

no unlocks no locks

Page 11: Concurrency Control I

3/9/2005 Yan Huang - CSCI5330 Database Implementation – Concurrency Control

# locks

held by

Ti

Time

Growing Shrinking

Phase Phase

Page 12: Concurrency Control I

3/9/2005 Yan Huang - CSCI5330 Database Implementation – Concurrency Control

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

Page 13: Concurrency Control I

3/9/2005 Yan Huang - CSCI5330 Database Implementation – Concurrency Control

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

Page 14: Concurrency Control I

3/9/2005 Yan Huang - CSCI5330 Database Implementation – Concurrency Control

2PL conflict-serializable schedules?

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

Page 15: Concurrency Control I

3/9/2005 Yan Huang - CSCI5330 Database Implementation – Concurrency Control

First:

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

Proof:Ti Tj means that

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

Page 16: Concurrency Control I

3/9/2005 Yan Huang - CSCI5330 Database Implementation – Concurrency Control

Then:

(1) Assume P(S) has cycle T1 T2 …. Tn T1

(2) By lemma: SH(T1) < SH(T2) < ... < SH(T1)

(3) Impossible, so P(S) acyclic

(4) S is conflict serializable

Page 17: Concurrency Control I

3/9/2005 Yan Huang - CSCI5330 Database Implementation – Concurrency Control

Deadlock

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

T4 T5

l3(B)

read(B)

write(B)

l4(A)

read(A)

l4(B)

l3(A)

Page 18: Concurrency Control I

3/9/2005 Yan Huang - CSCI5330 Database Implementation – Concurrency Control

Starvation

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

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

The same transaction is repeatedly rolled back due to deadlocks.

Concurrency control manager can be designed to prevent starvation.

Page 19: Concurrency Control I

3/9/2005 Yan Huang - CSCI5330 Database Implementation – Concurrency Control

2PL and Deadlock

Are schedules from 2PL transactions deadlock free?

Page 20: Concurrency Control I

3/9/2005 Yan Huang - CSCI5330 Database Implementation – Concurrency Control

2PL and Possible Schedules

Does 2PL allow all possible conflict serializable schedules?

Page 21: Concurrency Control I

3/9/2005 Yan Huang - CSCI5330 Database Implementation – Concurrency Control

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


Recommended