MATRUSRI ENGINEERING COLLEGE�DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
SUBJECT NAME: DataBase Management Systems
FACULTY NAME: K Sunil Manohar Reddy
Insert Your Photo here🡪
MATRUSRI
ENGINEERING COLLEGE
INTRODUCTION: ��THIS UNIT DEALS WITH CONCURRENCY CONTROL AND RECOVERY SYSTEM
UNIT-V
OUTCOMES:
Upon completion of this unit, student will be able to:
MATRUSRI
ENGINEERING COLLEGE
CONTENTS:�CONCURRENCY CONTROL: LOCK BASED PROTOCOLS, TIMESTAMP BASED PROTOCOLS, VALIDATION BASED PROTOCOLS, MULTIPLE GRANULARITY, MULTI VERSION SCHEMES, DEADLOCK HANDLING, INSERT AND DELETE OPERATIONS, WEAK LEVELS OF CONSISTENCY, CONCURRENCY OF INDEX STRUCTURES.���
OUTCOMES:
Upon completion of this module, student will be able to:
MODULE-I
MATRUSRI
ENGINEERING COLLEGE
Lock-Based Protocols
A lock is a mechanism to control concurrent access to a data item
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.
Lock requests are made to concurrency-control manager. Transaction can proceed only after request is granted.
Lock-compatibility matrix
MATRUSRI
ENGINEERING COLLEGE
Lock-Based Protocols (Cont.)
T2: lock-S(A);
read (A);
unlock(A);
lock-S(B);
read (B);
unlock(B);
display(A+B)
MATRUSRI
ENGINEERING COLLEGE
Timestamp-Based Protocols
Each transaction Ti is issued a timestamp TS(Ti) when it enters the system.
Each transaction has a unique timestamp
Newer transactions have timestamps strictly greater than earlier ones
Timestamp could be based on a logical counter
Real time may not be unique
Can use (wall-clock time, logical counter) to ensure
Timestamp-based protocols manage concurrent execution such that � time-stamp order = serializability order
Several alternative protocols based on timestamps
MATRUSRI
ENGINEERING COLLEGE
Timestamp-Based Protocols (Cont.)
Suppose that transaction Ti issues write(Q).
1. If TS(Ti) < R-timestamp(Q), then the value of Q that Ti is producing
was needed previously, and the system assumed that that value
would never be produced.
2. If TS(Ti) < W-timestamp(Q), then Ti is attempting to write an
obsolete value of Q.
3. Otherwise, the write operation is executed, and W-timestamp(Q) is
set to TS(Ti).
MATRUSRI
ENGINEERING COLLEGE
Timestamp-Ordering Protocol
Suppose a transaction Ti issues a read(Q)
1. If TS(Ti) ≤ W-timestamp(Q), then Ti needs to read a value of Q that
was already overwritten.
Hence, the read operation is rejected, and Ti is rolled back.
2. If TS(Ti) ≥ W-timestamp(Q), then the read operation is executed, and
R-timestamp(Q) is set to
max(R-timestamp(Q), TS(Ti)).
MATRUSRI
ENGINEERING COLLEGE
Multiversion Schemes
MATRUSRI
ENGINEERING COLLEGE
MVCC: Implementation Issues
MATRUSRI
ENGINEERING COLLEGE
Validation-Based Protocols
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, Ti is rolled back.
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.
Also called as optimistic concurrency control since transaction executes fully in the hope that all will go well during validation
MATRUSRI
ENGINEERING COLLEGE
The Two-Phase Locking Protocol
A protocol which ensures conflict-serializable schedules.
Phase 1: Growing Phase
Transaction may obtain locks
Transaction may not release locks
Phase 2: Shrinking Phase
Transaction may release locks
Transaction may not obtain locks
MATRUSRI
ENGINEERING COLLEGE
The Two-Phase Locking Protocol (Cont.)
condition for serializability
schedules that cannot be obtained
if the two-phase locking protocol
is used.
(e.g., ordering of access to data), two-phase
locking is necessary for conflict
serializability in the following sense:
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.
MATRUSRI
ENGINEERING COLLEGE
Locking Protocols
MATRUSRI
ENGINEERING COLLEGE
Implementation of Locking
MATRUSRI
ENGINEERING COLLEGE
Graph-Based Protocols
Graph-based protocols are an alternative to two-phase locking
Impose a partial ordering → on the set D = {d1, d2 ,..., 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.
The tree-protocol is a simple kind of graph protocol.
Tree Protocol :
Only exclusive locks are allowed.
The first lock by Ti may be on any data item. Subsequently, a data Q can be locked by Ti only if the parent of Q is currently locked by Ti.
Data items may be unlocked at any time.
A data item that has been locked and unlocked by Ti cannot subsequently be relocked by Ti
MATRUSRI
ENGINEERING COLLEGE
Graph-Based Protocols (Cont.)
MATRUSRI
ENGINEERING COLLEGE
Multiple Granularity
Allow data items to be of various sizes and define a hierarchy of data granularities, where the small granularities are nested within larger ones
Can be represented graphically as a tree (but don't confuse with tree-locking protocol)
When a transaction locks a node in the tree explicitly, it implicitly locks all the node's descendants in the same mode.
Granularity of locking (level in tree where locking is done):
Fine granularity (lower in tree): high concurrency, high locking overhead
Coarse granularity (higher in tree): low locking overhead, low concurrency
MATRUSRI
ENGINEERING COLLEGE
Intention Lock Modes
MATRUSRI
ENGINEERING COLLEGE
Intention Lock Modes
MATRUSRI
ENGINEERING COLLEGE
Multiple Granularity Locking Scheme
Transaction Ti can lock a node Q, using the following rules:
2. The root of the tree must be locked first, and may be locked in any
mode.
3. A node Q can be locked by Ti in S or IS mode only if the parent of Q is
currently locked by Ti in either IX or IS mode.
4. A node Q can be locked by Ti in X, SIX, or IX mode only if the parent
of Q is currently locked by Ti in either IX or SIX mode.
5. Ti can lock a node only if it has not previously unlocked any node (that
is, Ti is two-phase).
6. Ti can unlock a node Q only if none of the children of Q are currently
locked by Ti.
Observe that locks are acquired in root-to-leaf order, whereas they are released in leaf-to-root order.
Lock granularity escalation: in case there are too many locks at a particular level, switch to higher granularity S or X lock
MATRUSRI
ENGINEERING COLLEGE
Deadlock
Neither T3 nor T4 can make progress — executing lock-S(B) causes T4 to wait for T3 to release its lock on B, while executing lock-X(A) causes T3 to wait for T4 to release its lock on A.
Such a situation is called a deadlock.
To handle a deadlock one of T3 or T4 must be rolled back and its locks released.
MATRUSRI
ENGINEERING COLLEGE
Deadlock (Cont.)
MATRUSRI
ENGINEERING COLLEGE
Deadlock Handling
Deadlock prevention protocols ensure that the system will never enter into a deadlock state. Some prevention strategies:
MATRUSRI
ENGINEERING COLLEGE
Deadlock Detection
MATRUSRI
ENGINEERING COLLEGE
Deadlock Recovery
MATRUSRI
ENGINEERING COLLEGE
Insert/Delete Operations and Predicate Reads
MATRUSRI
ENGINEERING COLLEGE
Weak Levels of Consistency in SQL
MATRUSRI
ENGINEERING COLLEGE
Concurrency in Index Structures
MATRUSRI
ENGINEERING COLLEGE