Docsity

Database Systems: Concurrency Control and Serializability, Slides of Introduction to Database Management Systems

Toggle sidebar
bg1
1
G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
Concurrency So Far
• We want to run
transactions
concurrently
•Schedules
• Serial schedules
• Equivalence
• Correctness
• Serialisability
• To prevent some
problems we use
locking protocols
•S-locks
•X-locks
•Deadlock
• Deadlock detection
• Deadlock recovery
G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
Conflict Serialisability
• Based on whether or
not the order of two
operation s is
important
• NO If they refer to
different resources
• NO If they are reads
• YES If one is a write
and they use the
same resource
• If the order is not
important then the
operations can be
swapped
• If these swaps can
lead to a serial
schedule then the
original schedule is
‘conflict’ serialisable
G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
Precedence Graphs
• To deter mine if a
schedule is conflict
serialisa ble we use a
precedence graph
•Transactions are
vertices of the graph
• There is an edge from
Txto Tyif Txmust
happen before Tyin a
serial schedule
•T
xmust go first if
•T
xReads R before Ty
Writes R
•T
xWrites R before Ty
Reads R
•T
xWrites R before Ty
Writes R
• The schedule is
serialisa ble if there
are no cycles
G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
Precedence Graph Example
• The lost update
schedule has the
precedence graph
T1 T2
Read(X)
X = X - 5
Read(X)
X = X + 5
Write(X)
Write(X)
COMMIT
COMMIT
T1 T2
T1 reads X before T2 writes X
T2 reads X before T1 writes X
G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
Precedence Graph Example
• A serial version of
the transactions has
the graph
T1 T2
Read(X)
X = X - 5
Write(X)
Read(X)
X = X + 5
Write(X)
COMMIT
COMMIT
T1 T2
T1 reads X before T2 writes X
G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
Two-Phase Locking
• Two-phase locking
(2PL) protocol where
transactions
• Need an S-lock to
read a resource
• Need an X-lock to
write a resource
• Cannot acquire more
locks after releasing
any lock
•Two phases
• Growing phase where
locks are acquired on
resources
• Shrinking phase
where locks are
released
pf3
pf4

Partial preview of the text

Download Database Systems: Concurrency Control and Serializability and more Slides Introduction to Database Management Systems in PDF only on Docsity!

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Concurrency So Far

  • We want to run

transactions

concurrently

  • Schedules
  • Serial schedules
  • Equivalence
  • Correctness
  • Serialisability
    • To prevent some

problems we use

locking protocols

  • S-locks
  • X-locks
  • Deadlock
  • Deadlock detection
  • Deadlock recovery

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Conflict Serialisability

  • Based on whether or

not the order of two

operations is

important

  • NO If they refer to different resources
  • NO If they are reads
  • YES If one is a write and they use the same resource - If the order is not

important then the

operations can be

swapped

  • If these swaps can lead to a serial schedule then the original schedule is ‘conflict’ serialisable

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Precedence Graphs

  • To determine if a

schedule is conflict

serialisable we use a

precedence graph

  • Transactions are vertices of the graph
  • There is an edge from Tx to Ty if Tx must happen before Ty in a serial schedule - Tx must go first if - Tx Reads R before Ty Writes R - Tx Writes R before Ty Reads R - Tx Writes R before Ty Writes R - The schedule is

serialisable if there

are no cycles

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Precedence Graph Example

  • The lost update

schedule has the

precedence graph

T1 T

Read(X)

X = X - 5

Read(X)

X = X + 5

Write(X)

Write(X)

COMMIT

COMMIT

T1 T

T1 reads X before T2 writes X

T2 reads X before T1 writes X

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Precedence Graph Example

  • A serial version of

the transactions has

the graph

T1 T

Read(X)

X = X - 5

Write(X)

Read(X)

X = X + 5

Write(X)

COMMIT

COMMIT

T1 T

T1 reads X before T2 writes X

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Two-Phase Locking

  • Two-phase locking

(2PL) protocol where

transactions

  • Need an S-lock to read a resource
  • Need an X-lock to write a resource
  • Cannot acquire more locks after releasing any lock - Two phases - Growing phase where locks are acquired on resources - Shrinking phase where locks are released

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

2PL Example

  • T1 is two-phased
    • All of its locks are acquired before it releases any of them
  • T2 is not
    • It releases its lock on X and then goes on to later acquire a lock on Y

T1 T

S-lock(X) S-lock(X)

Read(X) Read(X)

X-lock(Y) Unlock(X)

Unlock(X) X-lock(Y)

Read(Y) Read(Y)

Y = Y + X Y = Y + X

Write(Y) Write(Y)

Unlock(Y) Unlock(Y)

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Serialisability Theorem

• A schedule of two-phased transactions

might not be deadlock free, but if it is…

Any schedule of two-phased

transactions is conflict serialisable

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Serialisability Theorem

  • A schedule of two-

phased transactions

is either conflict

serialisable or results

in deadlock

  • This relationship is

captured in the wait-

for and precedence

graphs

  • Wait-for graph
    • A cycle indicates deadlock
  • Precedence graph
    • A cycle indicates the schedule is not conflict serilisable
  • For two-phased

transactions these

are the same thing

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Precedence/Wait-For Graphs

  • Precedence graph
    • Each transaction is a vertex
    • Arcs from T1 to T2 if
      • T1 reads X before T writes X
      • T1 writes X before T reads X
      • T1 writes X before T writes X - Wait-for Graph - Each transaction is a vertex - Arcs from T2 to T1 if - T1 S-locks X then T tries to X-lock it - T1 X-locks X then T tries to S-lock it - T1 X-locks X then T tries to X-lock it

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

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

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Deadlock Prevention

  • Deadlock can still

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

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

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

T1T

TS

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Timestamping

  • The protocol means

that transactions

with higher times

take precedence

  • Equivalent to running transactions in order of their final time values
  • Transactions don’t wait - no deadlock - Problems - Long transactions might keep getting restarted by new transactions - starvation - Rolls back old transactions, which may have done a lot of work

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Transaction Summary

ACID Properties

Concurrency

Locking

Serialisability 2PL Deadlock

Recovery

Schedules

Timestamping

Transaction

Manager