Docsity

2-Phase Locking - Data Base Management System - Lecture Slides, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download 2-Phase Locking - Data Base Management System - Lecture Slides and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Concurrent Control Using

2-Phase Locking

Locks

The most common way in which access to items is

controlled is by “locks.” Lock manager is the part

of a DBMS that records, for each item I, whether

one or more transactions are reading or writing

any part of I. If so, the manager will forbid

another transaction from gaining access to I,

provided the type of access (read or write) could

cause a conflict, such as the duplicate selling of an

airline seat.

Example of locks

Lets consider two transaction T1 and T2. Each

accesses an item A, which we assume has an integer

value, and adds one to A.

Read A;A:=A+1;Write A;

T1: Read A A:=A+1 Write A

T2: Read A A:=A+1 Write A

Example of locks (cont…)

The most common solution to this problem is to

provide a lock on A. Before reading A, a

transaction T must lock A, which prevents another

transaction from accessing A until T is finished

with A. Furthermore, the need for T to set a lock

on A prevents T from accessing A if some other

transaction is already using A. T must wait until

the other transaction unlocks A, which it should

do only after finishing with A.

Transaction Management with SQL

  • Transaction support is provided by two SQL

statements: COMMIT and ROLLBACK.

  • A COMMIT statement is reached, in which case

all changes are permanently recorded within the

database. The COMMIT statement automatically

ends the SQL transaction.

  • A ROLLBACK statement is reached in which

case all changes are aborted and the database is

rolled back to its previous consistent state.

Serializable schedules

  • A serializable schedule is a linear arrangement of the database calls from several transactions with the property: the final database state obtained by executing the calls in schedule order is the same as that obtained by running the transactions in some unspecified serial

order.

Shared locks permit reads but no

updates

  • Exclusive locks prevent any current access. A shared lock lets you read an object, but you need an exclusive lock to update it.

Deadlocks

  • A deadlocks involves a chain of transactions that are cyclically waiting for each other to release a lock. The DBMS detects deadlock with a transaction dependency graph. It resolves the impasse by sacrificing one of the transactions in

cycle.

Dirty-read

  • The Dirty-read, unrepeatable read, and phantom scenarios, represent inference among competing transactions that can jeopardize serializability. The strict two-phase locking protocol resolves these problems.

Serializability through timestamps

  • A timestamp is a centrally dispensed number assigned to each transaction in strictly increasing order. The DBMS guarantees that the final result from competing transactions will appear as if the transactions had executed serially in timestamp

order.

A checkpoint

  • A checkpoint is a

synchronization

record placed in the

log to note a point

when all concluded

transactions are safely

on disk. It limits the

log segment needed to

recover from a failure.

Recovery from a backup copy of the database

  • If a failure corrupts the database, you can reinstate a previous state from a backup copy. If some portion of the log remains intact, you can recover certain transactions that committed subsequent to the backup.

Schedules

Each transaction must specify as its final action either

c ommit (i.e., complete successfully) or abort (i.e., terminate

and undo all the actions carried out thus far).

Definition: a schedule is a list of actions (reading, writing,

aborting or committing) from a set of transactions, and the

order in which two actions of a transaction T appear in a

schedule must be the same as the order in which they appear

in T.

Notation: RT (O) means the action of a transaction T reading

an object O ; W T (O) means writing O.

An execution order for transactions T1 and T2 :

T1 T2 Intuitively, a schedule

R(A) represents an actual

W(A) R(B) or potential execution

W(B) sequence.

R(C)

W(C)

Figure 1