Docsity

Database System Concepts: Lecture 37 - Concurrency Control and Locking Protocols, Slides of Database Management Systems (DBMS)

Toggle sidebar
bg1
1
Computing & Information Sciences
Kansas State University
Monday, 20 Nov 2006CIS 560: Database System Concepts
Lecture 37 of 42
Monday, 20 November 2006
William H. Hsu
Department of Computing and Information Sciences, KSU
KSOL course page: http://snipurl.com/va60
Course web site: http://www.kddresearch.org/Courses/Fall-2006/CIS560
Instructor home page: http://www.cis.ksu.edu/~bhsu
Reading for Next Class:
First half of Chapter 16, Silberschatz et al., 5th edition
Concurrency
Discussion: Term Projects
Computing & Information Sciences
Kansas State University
Monday, 20 Nov 2006CIS 560: Database System Concepts
Chapter 16: Concurrency Control
Chapter 16: Concurrency Control
zLock-Based Protocols
zTimestamp-Based Protocols
zValidation-Based Protocols
zMultiple Granularity
zMultiversion Schemes
zDeadlock Handling
zInsert and Delete Operations
zConcurrency in Index Structures
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Database System Concepts: Lecture 37 - Concurrency Control and Locking Protocols and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Computing & Information Sciences CIS 560: Database System Concepts Monday, 20 Nov 2006 Kansas State University

Lecture 37 of 42

Monday, 20 November 2006

William H. Hsu Department of Computing and Information Sciences, KSU

KSOL course page: http://snipurl.com/va Course web site: http://www.kddresearch.org/Courses/Fall-2006/CIS Instructor home page: http://www.cis.ksu.edu/~bhsu

Reading for Next Class: First half of Chapter 16, Silberschatz et al. , 5th^ edition

Concurrency

Discussion: Term Projects

Computing & Information Sciences CIS 560: Database System Concepts Monday, 20 Nov 2006 Kansas State University

Chapter 16: Concurrency ControlChapter 16: Concurrency Control

z Lock-Based Protocols z Timestamp-Based Protocols z Validation-Based Protocols z Multiple Granularity z Multiversion Schemes z Deadlock Handling z Insert and Delete Operations z Concurrency in Index Structures

Computing & Information Sciences CIS 560: Database System Concepts Monday, 20 Nov 2006 Kansas State University

Lock-Based Protocols Lock-Based Protocols

z A lock is a mechanism to control concurrent access to a data item z Data items can be locked in two modes : 1_. exclusive (X) mode_. Data item can be both read as well as written. X-lock is requested using lock-X instruction. 2_. shared (S) mode_. Data item can only be read. S-lock is requested using lock-S instruction. z Lock requests are made to concurrency-control manager. Transaction can proceed only after request is granted.

Computing & Information Sciences CIS 560: Database System Concepts Monday, 20 Nov 2006 Kansas State University

Lock-Based Protocols (Cont.)Lock-Based Protocols (Cont.)

z Lock-compatibility matrix

z A transaction may be granted a lock on an item if the requested lock is compatible with locks already held on the item by other transactions z Any number of transactions can hold shared locks on an item, ’ but if any transaction holds an exclusive on the item no other transaction may hold any lock on the item. z If a lock cannot be granted, the requesting transaction is made to wait till all incompatible locks held by other transactions have been released. The lock is then granted.

Computing & Information Sciences CIS 560: Database System Concepts Monday, 20 Nov 2006 Kansas State University

Pitfalls of Lock-Based Protocols (Cont.) Pitfalls of Lock-Based Protocols (Cont.)

z The potential for deadlock exists in most locking protocols. Deadlocks are a necessary evil. z Starvation is also possible if concurrency control manager is badly designed. For example: ’ A transaction may be waiting for an X-lock on an item, while a sequence of other transactions request and are granted an S-lock on the same item. ’ The same transaction is repeatedly rolled back due to deadlocks. z Concurrency control manager can be designed to prevent starvation.

Computing & Information Sciences CIS 560: Database System Concepts Monday, 20 Nov 2006 Kansas State University

The Two-Phase Locking ProtocolThe Two-Phase Locking Protocol

z This is a protocol which ensures conflict-serializable schedules. z Phase 1: Growing Phase ’ transaction may obtain locks ’ transaction may not release locks z Phase 2: Shrinking Phase ’ transaction may release locks ’ transaction may not obtain locks z The protocol assures serializability. It can be proved that the transactions can be serialized in the order of their lock points (i.e. the point where a transaction acquired its final lock).

Computing & Information Sciences CIS 560: Database System Concepts Monday, 20 Nov 2006 Kansas State University

The Two-Phase Locking Protocol (Cont.) The Two-Phase Locking Protocol (Cont.)

z Two-phase locking does not ensure freedom from deadlocks z Cascading roll-back is possible under two-phase locking. To avoid this, follow a modified protocol called strict two-phase locking. Here a transaction must hold all its exclusive locks till it commits/aborts. z Rigorous two-phase locking is even stricter: here all locks are held till commit/abort. In this protocol transactions can be serialized in the order in which they commit.

Computing & Information Sciences CIS 560: Database System Concepts Monday, 20 Nov 2006 Kansas State University

The Two-Phase Locking Protocol (Cont.) The Two-Phase Locking Protocol (Cont.)

z There can be conflict serializable schedules that cannot be obtained if two-phase locking is used. z However, in the absence of extra information (e.g., ordering of access to data), two-phase locking is needed for conflict serializability in the following sense: Given a transaction T i that does not follow two-phase locking, we can find a transaction Tj that uses two-phase locking, and a schedule for Ti and Tj that is not conflict serializable.

Computing & Information Sciences CIS 560: Database System Concepts Monday, 20 Nov 2006 Kansas State University

Automatic Acquisition of Locks (Cont.)Automatic Acquisition of Locks (Cont.)

z write (D) is processed as: if Ti has a lock-X on D then write( D ) else begin if necessary wait until no other trans. has any lock on D , if Ti has a lock-S on D then upgrade lock on D to lock-X else grant Ti a lock-X on D write( D ) end ; z All locks are released after commit or abort

Computing & Information Sciences CIS 560: Database System Concepts Monday, 20 Nov 2006 Kansas State University

Implementation of Locking Implementation of Locking

z A lock manager can be implemented as a separate process to which transactions send lock and unlock requests z The lock manager replies to a lock request by sending a lock grant messages (or a message asking the transaction to roll back, in case of a deadlock) z The requesting transaction waits until its request is answered z The lock manager maintains a data-structure called a lock table to record granted locks and pending requests z The lock table is usually implemented as an in-memory hash table indexed on the name of the data item being locked

Computing & Information Sciences CIS 560: Database System Concepts Monday, 20 Nov 2006 Kansas State University

Lock TableLock Table

z Black rectangles indicate granted locks, white ones indicate waiting requests z Lock table also records the type of lock granted or requested z New request is added to the end of the queue of requests for the data item, and granted if it is compatible with all earlier locks z Unlock requests result in the request being deleted, and later requests are checked to see if they can now be granted z If transaction aborts, all waiting or granted requests of the transaction are deleted ’ lock manager may keep a list of locks held by each transaction, to implement this efficiently

Computing & Information Sciences CIS 560: Database System Concepts Monday, 20 Nov 2006 Kansas State University

Graph-Based ProtocolsGraph-Based Protocols

z Graph-based protocols are an alternative to two-phase locking z Impose a partial ordering → on the set D = { d 1 , d 2 ,..., dh } of all data items. ’ If di → dj then any transaction accessing both di and dj must access di before accessing dj. ’ Implies that the set D may now be viewed as a directed acyclic graph, called a database graph. z The tree-protocol is a simple kind of graph protocol.

Computing & Information Sciences CIS 560: Database System Concepts Monday, 20 Nov 2006 Kansas State University

Timestamp-Based ProtocolsTimestamp-Based Protocols

z Each transaction is issued a timestamp when it enters the system. If an old transaction Ti has time-stamp TS( Ti ), a new transaction Tj is assigned time-stamp TS( Tj ) such that TS( Ti ) <TS( Tj ). z The protocol manages concurrent execution such that the time- stamps determine the serializability order. z In order to assure such behavior, the protocol maintains for each data Q two timestamp values: ’ W-timestamp ( Q ) is the largest time-stamp of any transaction that executed write ( Q ) successfully. ’ R-timestamp ( Q ) is the largest time-stamp of any transaction that executed read ( Q ) successfully.

Computing & Information Sciences CIS 560: Database System Concepts Monday, 20 Nov 2006 Kansas State University

Timestamp-Based Protocols (Cont.) Timestamp-Based Protocols (Cont.)

z The timestamp ordering protocol ensures that any conflicting read and write operations are executed in timestamp order. z Suppose a transaction T (^) i issues a read ( Q )

  1. If TS( T (^) i ) ≤ W -timestamp( Q ), then T (^) i needs to read a value of Q that was already overwritten. „ Hence, the read operation is rejected, and T (^) i is rolled back.
  2. If TS( T (^) i )≥ W -timestamp( Q ), then the read operation is executed, and R-timestamp( Q ) is set to the maximum of R-timestamp( Q ) and TS( T (^) i ).

Computing & Information Sciences CIS 560: Database System Concepts Monday, 20 Nov 2006 Kansas State University

Timestamp-Based Protocols (Cont.) Timestamp-Based Protocols (Cont.)

z Suppose that transaction Ti issues write ( Q ).

  1. If TS( T (^) i ) < R-timestamp( Q ), then the value of Q that T (^) i is producing was needed previously, and the system assumed that that value would never be produced. „ Hence, the write operation is rejected, and T (^) i is rolled back.
  2. If TS( T (^) i ) < W-timestamp( Q ), then T (^) i is attempting to write an obsolete value of Q. „ Hence, this write operation is rejected, and T (^) i is rolled back.
  3. Otherwise, the write operation is executed, and W-timestamp( Q ) is set to TS( T (^) i ).

Computing & Information Sciences CIS 560: Database System Concepts Monday, 20 Nov 2006 Kansas State University

Example Use of the Protocol Example Use of the Protocol

A partial schedule for several data items for transactions with timestamps 1, 2, 3, 4, 5

T 1 T 2 T 3 T 4 T 5

read( Y )

read( X ) read( Y ) write( Y ) write( Z ) read( Z ) read( X ) abort read( X ) write( Z ) abort write( Y ) write( Z )

Computing & Information Sciences CIS 560: Database System Concepts Monday, 20 Nov 2006 Kansas State University

Thomas’ Write RuleThomas’ Write Rule

z Modified version of the timestamp-ordering protocol in which obsolete write operations may be ignored under certain circumstances. z When Ti attempts to write data item Q , if TS( Ti ) < W- timestamp( Q ), then Ti is attempting to write an obsolete value of { Q }. ’ Rather than rolling back T (^) i as the timestamp ordering protocol would have done, this { write } operation can be ignored. z Otherwise this protocol is the same as the timestamp ordering protocol. z Thomas' Write Rule allows greater potential concurrency. ’ Allows some view-serializable schedules that are not conflict- serializable.

Computing & Information Sciences CIS 560: Database System Concepts Monday, 20 Nov 2006 Kansas State University

Validation-Based ProtocolValidation-Based Protocol

z Execution of transaction Ti is done in three phases.

1. Read and execution phase : Transaction Ti writes only to temporary local variables 2. Validation phase : Transaction Ti performs a ``validation test'' to determine if local variables can be written without violating serializability. 3. Write phase : If Ti is validated, the updates are applied to the database; otherwise, T (^) i is rolled back. z The three phases of concurrently executing transactions can be interleaved, but each transaction must go through the three phases in that order. ’ Assume for simplicity that the validation and write phase occur together, atomically and serially Ö I.e., only one transaction executes validation/write at a time. z Also called as optimistic concurrency control since transaction executes fully in the hope that all will go well during validation

Computing & Information Sciences CIS 560: Database System Concepts Monday, 20 Nov 2006 Kansas State University

Validation-Based Protocol (Cont.) Validation-Based Protocol (Cont.)

z Each transaction Ti has 3 timestamps ’ Start(T (^) i) : the time when T (^) i started its execution ’ Validation(T (^) i): the time when T (^) i entered its validation phase ’ Finish(T (^) i) : the time when T (^) i finished its write phase z Serializability order is determined by timestamp given at validation time, to increase concurrency. ’ Thus TS(T (^) i) is given the value of Validation(T (^) i). z This protocol is useful and gives greater degree of concurrency if probability of conflicts is low. ’ because the serializability order is not pre-decided, and ’ relatively few transactions will have to be rolled back.

Computing & Information Sciences CIS 560: Database System Concepts Monday, 20 Nov 2006 Kansas State University

Validation Test for TransactionValidation Test for Transaction^ TTjj

z If for all Ti with TS ( Ti ) < TS ( Tj ) either one of the following condition holds: ’ finish ( T (^) i ) < start ( T (^) j ) ’ start ( T (^) j ) < finish ( T (^) i ) < validation ( T (^) j ) and the set of data items written by T (^) i does not intersect with the set of data items read by T (^) j. then validation succeeds and Tj can be committed. Otherwise, validation fails and Tj is aborted. z Justification : Either the first condition is satisfied, and there is no overlapped execution, or the second condition is satisfied and „ the writes of T (^) j do not affect reads of T (^) i since they occur after T (^) i has finished its reads. „ the writes of T (^) i do not affect reads of T (^) j since T (^) j does not read any item written by T (^) i.