Docsity

Deadlock Prevention and Timestamping in Database Systems, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download Deadlock Prevention and Timestamping in Database Systems and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

More Concurrency

Deadlocks

  • A deadlock is an

impasse that may

result when two or

more transactions

are waiting for locks

to be released which

are held by each

other.

  • For example: T1 has a lock on X and is waiting for a lock on Y, and T has a lock on Y and is waiting for a lock on X. - Given a schedule, we

can detect deadlocks

which will happen in

this schedule using a

wait-for graph

(WFG).

Example

T1 Read(X)

T2 Read(Y)

T1 Write(X)

T2 Read(X)

T3 Read(Z)

T3 Write(Z)

T1 Read(Y)

T3 Read(X)

T1 Write(Y)

T

T2 T

Wait for graph

T

T2 T

Precedence graph

Example

T1 Read(X)

T2 Read(Y)

T1 Write(X)

T2 Read(X)

T3 Read(Z)

T3 Write(Z)

T1 Read(Y)

T3 Read(X)

T1 Write(Y)

T

T2 T

Wait for graph

T

T2 T

Precedence graph

Example

T1 Read(X)

T2 Read(Y)

T1 Write(X)

T2 Read(X)

T3 Read(Z)

T3 Write(Z)

T1 Read(Y)

T3 Read(X)

T1 Write(Y)

T

T2 T

Wait for graph

T

T2 T

Precedence graph

Example

T1 Read(X) read-locks(X)

T2 Read(Y) read-locks(Y)

T1 Write(X) write-lock(X)

T2 Read(X ) tries read-lock(X)

T3 Read(Z)

T3 Write(Z)

T1 Read(Y)

T3 Read(X)

T1 Write(Y)

T

T2 T

Wait for graph

T

T2 T

Precedence graph

Example

T1 Read(X) read-locks(X)

T2 Read(Y) read-locks(Y)

T1 Write(X) write-lock(X)

T2 Read(X) tries read-lock(X)

T3 Read(Z) read-lock(Z)

T3 Write(Z) write-lock(Z)

T1 Read(Y) read-lock(Y)

T3 Read(X) tries read-lock(X)

T1 Write(Y) tries write-lock(Y)

T

T2 T

Wait for graph

T

T2 T

Precedence graph

Deadlock Prevention

  • Deadlocks can arise

with 2PL

  • Deadlock is less of a problem than an inconsistent DB
  • We can detect and recover from deadlock
  • It would be nice to avoid it altogether - Conservative 2PL - All locks must be acquired before the transaction starts - Hard to predict what locks are needed - Low ‘lock utilisation’ - transactions can hold on to locks for a long time, but not use them much

Example of resource ordering

  • Suppose resource

order is: X < Y

  • This means, if you

need locks on X and

Y, you first acquire a

lock on X and only

after that a lock on Y

  • (even if you want to write to Y before doing anything to X) - It is impossible to

end up in a situation

when T1 is waiting

for a lock on X held

by T2, and T2 is

waiting for a lock on

Y held by T1.

Timestamping

  • Transactions can be

run concurrently

using a variety of

techniques

  • We looked at using

locks to prevent

interference

  • An alternative is

timestamping

  • Requires less overhead in terms of tracking locks or detecting deadlock
  • Determines the order of transactions before they are executed

Timestamp Protocol

  • If T tries to read X
    • If TS(T) < W(X) T is rolled back and restarted with a later timestamp
    • If TS(T) ≥ W(X) then the read succeeds and we set R(X) to be max(R(X), TS(T)) - T tries to write X - If TS(T) < W(X) or TS(T) < R(X) then T is rolled back and restarted with a later timestamp - Otherwise the write succeeds and we set W(X) to TS(T)

Timestamping Example

  • Given T1 and T2 we

will assume

  • The transactions make alternate operations
  • Timestamps are allocated from a counter starting at 1
  • T1 goes first

T1 T

Read(X) Read(X)

Read(Y) Read(Y)

Y = Y + X Z = Y - X

Write(Y) Write(Z)

Timestamp Example

T1 T

Read(X) Read(X)

Read(Y) Read(Y)

Y = Y + X Z = Y - X

Write(Y) Write(Z)

X Y

R

W

Z

T1 T

TS

Timestamp Example

T1 T

Read(X) Read(X)

Read(Y) Read(Y)

Y = Y + X Z = Y - X

Write(Y) Write(Z)

X Y

R

W

Z

T1 T

TS