Beruflich Dokumente
Kultur Dokumente
Example:
In concurrent execution environment if T1 conflicts with T2 over a data item A, then the existing concurrency control decides if T1 or T2 should get the A and if the other transaction is rolledback or waits.
Slide 18- 2
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
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
Locking
Before reading from a resource a transaction must acquire a readlock Before writing to a resource a transaction must acquire a writelock Locks are released on commit/rollback A transaction may not acquire a lock on any resource that is writelocked by another transaction A transaction may not acquire a write-lock on a resource that is locked by another transaction If the requested lock is not available, transaction waits
Slide 18- 7
Slide 18- 8
Lock Conversions
Second Phase:
can release a lock-S can release a lock-X can convert a lock-X to a lock-S (downgrade)
A lock manager can be implemented as a separate process to which transactions send lock and unlock requests 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) The requesting transaction waits until its request is answered The lock manager maintains a data-structure called a lock table to record granted locks and pending requests
The lock table is usually implemented as an in-memory hash table indexed on the name of the data item being locked
Lock Table
Implementation of Locking
Transaction ID Data item id lock mode Ptr to next data item T1 X1 Read Next
Lock Table
Granted Waiting
Black rectangles indicate granted locks, white ones indicate waiting requests 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 Unlock requests result in the request being deleted, and later requests are checked to see if they can now be granted 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
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).
All locking operations (read_lock, write_lock) precede the first unlock operation in the transactions. Two phases:
expanding phase: new locks on items can be acquired but none can be released shrinking phase: existing locks can be released but no new ones can be acquired
X=20, Y=30 T1 read_lock(Y); read_item(Y); write_lock(X); unlock(Y); read_item(X); X:=X+Y; write_item(X); unlock(X); T2 read_lock(X); read_item(X); write_lock(Y); unlock(X); read_item(Y); Y:=X+Y; write_item(Y); unlock(Y);
Basic 2PL
Two-Phasing Locking
lock point obtain lock release lock
number of locks
Phase 1 BEGIN
Phase 2 END
its locks until after it commits or aborts leads to a strict schedule for recovery
obtain lock
number of locks release lock
BEGIN
Transaction duration
Consider the following two transactions: T1: write (X) T2: write(Y) write(Y) write(X) Schedule with deadlock
Deadlock Handling
T2
Deadlocks
Deadlock: Cycle of transactions waiting for locks to be released by each other. Two ways of dealing with deadlocks:
Deadlock Prevention
Assign priorities based on timestamps. A timsestamp TS(T) which is a unique identifier assigned to transactions based on the order in which they start. If T1 starts before T2, then TS(T1) < TS(T2) (older transaction has the smaller timestamp value) Assume Ti wants a lock that Tj holds. Two policies are possible:
Wait-Die: It Ti has higher priority, Ti waits for Tj; otherwise Ti aborts Wound-wait: If Ti has higher priority, Tj aborts; otherwise Ti waits
The scheduler maintains a wait-for-graph for detecting cycle. If a cycle exists, then one transaction involved in the cycle is selected (victim) and rolled-back.
Another approach to deal with deadlock is the use of timeouts. In this method, if a transaction waits for a period of time longer than a system-defined timeout period, the system assumes that a transaction may be in a state of deadlock and aborts it regardless of whether a deadlock actually exists or not.
Chapter 18-23
Deadlock Detection
Deadlocks can be described as a wait-for graph, which consists of a pair G = (V,E), If Ti Tj is in E, then there is a directed edge from Ti to Tj, implying that Ti is waiting for Tj to release a data item. When Ti requests a data item currently being held by Tj, then the edge Ti Tj is inserted in the wait-for graph. This edge is removed only when Tj is no longer holding a data item needed by Ti. The system is in a deadlock state if and only if the wait-for graph has a cycle. Must invoke a deadlock-detection algorithm periodically to look for cycles.
V is a set of vertices (all the transactions in the system) E is a set of edges; each element is an ordered pair Ti Tj.
A timsestamp TS(T) which is a unique identifier assigned to transactions based on the order in which they start. If T1 starts before T2, then TS(T1) < TS(T2) (older transaction has the smaller timestamp value) The protocol manages concurrent execution such that the timestamps determine the serializability order.
Timestamp-Based Protocols
In order to assure such behavior, the protocol maintains for each data Q two timestamp values:
W-timestamp(Q) [ WTS (Q) ] is the largest time-stamp of any transaction that executed write(Q) successfully. R-timestamp(Q) [ RTS (Q) ] is the largest time-stamp of any transaction that executed read(Q) successfully.
T2
T3
T4
read(Y)
T5 read(X)
read(Z)
read(X)
write(Y) write(Z)
The timestamp-ordering protocol guarantees serializability since all the arcs in the precedence graph are of the form:
transaction with smaller timestamp transaction with larger timestamp
Thus, there will be no cycles in the precedence graph Timestamp protocol ensures freedom from deadlock as no transaction ever waits.