Docsity

Concurrency Control: Preventing Interference in Database Transactions, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download Concurrency Control: Preventing Interference in Database Transactions and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Concurrency

Need for concurrency control

  • Previous lecture: transactions running concurrently may interfere with each other, causing various problems (lost updates etc.)
  • Concurrency control: the process of managing simultaneous operations on the database without having them interfere with each other.

Uncommitted Update

(“dirty read”)

T1 T

Read(X) X = X - 5 Write(X) Read(X) X = X + 5 Write(X) ROLLBACK COMMIT

This reads the value of X which it should not have seen

Inconsistent analysis

T1 T

Read(X) X = X - 5 Write(X) Read(X) Read(Y) Sum = X+Y Read(Y) Y = Y + 5 Write(Y)

Summing up data while it is being updated

Serial schedules

  • Serial schedules are guaranteed to avoid interference and keep the database consistent
  • However databases need concurrent access which means interleaving operations from different transactions

Serialisability

  • Two schedules are equivalent if they always have the same effect.
  • A schedule is serialisable if it is equivalent to some serial schedule.
  • For example:
    • if two transactions only read some data items, then the order is which they do it is not important
    • If T1 reads and updates X and T2 reads and updates a different data item Y, then again they can be scheduled in any order.

Conflict Serialisable Schedule

Serial Schedule T1 Read(X) T1 Write(X) T1 Read(Y) T1 Write(Y)

T2 Read(X) T2 Write(X) T2 Read(Y) T2 Write(Y)

Interleaved Schedule T1 Read(X) T1 Write(X) T2 Read(X) T2 Write(X) T1 Read(Y) T1 Write(Y) T2 Read(Y) T2 Write(Y)

This schedule is serialisable, even though T1 and T2 read and write the same resources X and Y: they have a conflict

Conflict Serialisability

  • Two transactions have a conflict: - NO If they refer to different resources - NO If they are reads - YES If at least one is a write and they use the same resource - A schedule is conflict serialisable if transactions in the schedule have a conflict but the schedule is still serialisable

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 T1 to T2 if T1 must happen before T2 in any equivalent serial schedule - Edge T1 → T2 if in the schedule we have: - T1 Read(R) followed by T2 Write(R) for the same resource R - T1 Write(R) followed by T2 Read(R) - T1 Write(R) followed by T2 Write(R) - The schedule is serialisable if there are no cycles

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 Write(X) followed by T2 Write(X)

T2 Read(X) followed by T1 Write(X)

Locking

  • Locking is a procedure used to control concurrent access to data (to ensure serialisability of concurrent transactions)
  • In order to use a ‘resource’ (table, row, etc) a transaction must first acquire a lock on that resource
  • This may deny access to other transactions to prevent incorrect results

Two types of locks

  • Two types of lock
    • Shared lock (S-lock or read-lock)
    • Exclusive lock (X-lock or write-lock)
  • Read lock allows several transactions simultaneously to read a resource (but no transactions can change it at the same time)
  • Write lock allows one transaction exclusive access to write to a resource. No other transaction can read this resource at the same time.
  • The lock manager in the DBMS assigns locks and records them in the data dictionary

Two-Phase Locking

  • A transaction follows the two-phase locking protocol (2PL) if all locking operations precede the first unlock operation in the transaction - Two phases - Growing phase where locks are acquired on resources - Shrinking phase where locks are released

Example

  • T1 follows 2PL protocol - All of its locks are acquired before it releases any of them
  • T2 does not
    • It releases its lock on X and then goes on to later acquire a lock on Y

T1 T

read-lock(X) read-lock(X) Read(X) Read(X) write-lock(Y) unlock(X) unlock(X) write-lock(Y) Read(Y) Read(Y) Y = Y + X Y = Y + X Write(Y) Write(Y) unlock(Y) unlock(Y)