Professional Documents
Culture Documents
General Overview
s Relational model - SQL
15.2
Review: AC[I]D
s Isolation
Ensure concurrency
Prevent bad concurrency and allow only good concurrency
through analysis of schedules s Allow only conflict serializable schedules: schedules that are
15.3
CC Scheduler DB
Database System Concepts 15.4
T1: T2:
T1: T2:
R(A), W(A), C
15.5
Anomalies (Continued)
s Overwriting Uncommitted Data (WW Conflicts):
T1: T2:
W(B), C
15.6
Agenda
s 2PL and variants s Timestamp-based s Optimistic CC: Validation-based protocols s Multiple granularity s Multi-version s Weaker Consistency (other than serializability) s Dealing with Deadlocks
15.7
transactions can be serialized in the order of their lock points (i.e. the point where a transaction acquired its final lock). Locks can be either X, or S/X.
15.8
Each Xact must obtain a S (shared) lock on object before reading, and an X (exclusive) lock on object before writing. All locks held by a transaction are released when the transaction completes If an Xact holds an X lock on an object, no other Xact can get a lock (S or X) on that object.
Has no cascading rollbacks (as locks are released only when txn completes)
15.9
Agenda
s 2PL and variants s Timestamp-based s Optimistic CC: Validation-based protocols s Multiple granularity s Multi-version s Weaker Consistency (other than serializability) s Dealing with Deadlocks
15.10
Timestamp-Based Protocols
s Idea:
Decide in advance ordering of xctions Ensure concurrent schedule serializes to serial order decided
Timestamps
15.11
Timestamp CC
s Idea: If action pi of Xact Ti conflicts with action qj of Xact Tj, and TS(Ti) < TS(Tj), then pi must occur before qj. Otherwise, restart violating Xact.
15.12
of O.
So, abort T and restart it with a new, larger TS. (If restarted with same TS, T will fail again!)
s If TS(T) > W-TS(O):
T start
Database System Concepts
U start
15.13
needed previously, and the system assumed that that value would never be produced. write rejected, T is rolled back.
s If TS(T) < W-TS(Q), then T is attempting to write an obsolete value
TS(T).
U reads Q T writes Q
T start
Database System Concepts
U start
15.14
15.15
Ti Read(A),Write(B)
lock table
Scheduler, part I
l(A),Read(A),l(B),Write(B)
Scheduler, part II
Read(A),Write(B) DB
15.16
Agenda
s 2PL and variants s Timestamp-based s Optimistic CC: Validation-based protocols s Multiple granularity s Multi-version s Weaker Consistency (other than serializability) s Dealing with Deadlocks
15.17
Optimistic CC (Kung-Robinson)
s Locking is a conservative approach in which conflicts are
prevented. Disadvantages:
Lock management overhead. Deadlock detection/resolution. Lock contention for heavily used objects.
s If conflicts are rare, we might be able to gain concurrency by not
15.18
READ: Xacts read from the database, but make changes to private copies of objects. VALIDATE: Check for conflicts. WRITE: Make local copies of changes public.
ROOT new
Validation
s Test conditions that are sufficient to ensure that no conflict
occurred.
s Each Xact is assigned a numeric id.
15.20
Test 1
s For all i and j such that Ti < Tj, check that Ti completes before Tj
begins.
Ti
R V W R
Tj
V W
15.21
Test 1
s For all i and j such that Ti < Tj, check that Ti completes before Tj
begins.
Ti
R V W R
Tj
V W
15.22
Test 2
Ti
V R
W V W
Tj
Test 3
Ti
R R
V V
W W
Tj
RS(T2)={B} WS(T2)={B,D}
RS(T3)={A,B} WS(T3)={C}
=
T3
validated
T2
start
T3
start
T2
validated
time
15.25
RS(T3)={A,B} WS(T3)={C}
T2
start
T3
start
T2
validated
T3
validated
T2 finish phase 3
T3
start
time
15.26
T2
validated
T3
validated finish
T2
time
15.27
T2
validated finish
T3
validated
T2
finish
T2
time
15.28
a critical section!
I.e., Nothing else goes on concurrently. If Write phase is long, major drawback.
s Optimization for Read-only Xacts:
15.29
Overheads in Optimistic CC
s Must record read/write activity in ReadSet and WriteSet per Xact.
writes ``global.
Critical section can reduce concurrency. Scheme for making writes global can reduce clustering of objects.
s Optimistic CC restarts Xacts that fail validation.
15.30
``Optimistic 2PL
s If desired, we can do the following:
Set S locks as usual. Make changes to private copies of objects. Obtain all X locks at end of Xact, make writes global, then release all locks.
s In contrast to Optimistic CC as in Kung-Robinson, this scheme
15.31
Agenda
s 2PL and variants s Timestamp-based s Optimistic CC: Validation-based protocols s Multiple granularity s Multi-version s Weaker Consistency (other than serializability) s Dealing with Deadlocks
15.32
Multiple Granularity
s Allow data items to be of various sizes and define a hierarchy of
data granularities, where the small granularities are nested within larger ones
s When a transaction locks a node in the hierarchy explicitly, it
15.33
Multiple Granularity
s If we lock large objects (e.g., Relations)
15.34
The highest level in the example hierarchy is the entire database. The levels below are of type area, file or relation and record in that order.
Database System Concepts 15.35
Multiple-Granularity Locks
tables).
s Shouldnt have to decide! s Data containers are nested:
Before locking an item, Xact must set intention locks on all its ancestors. For unlock, go from specific to general (i.e., bottom-up). SIX mode: Like S & IX at the same time.
15.37
--
IS IX S
IS IX S X
s Each Xact starts from the root of the hierarchy. s To get S or IS lock on a node, must hold IS or IX on parent node.
node.
s Must release locks in bottom-up order.
Protocol is correct in that it is equivalent to directly setting locks at the leaf levels of the hierarchy.
Database System Concepts 15.38
IS IS IX
holder
IX
S IX
S S IX X
15.39
Parent locked in
P C
IS IX S SIX X
IS, S IS, S, IX, X, SIX [S, IS] not necessary X, IX, [SIX] none
15.40
R1 t1 t2
T1(S)
t3
t4
T2(X)
15.41
if parent(Q) locked by Ti in IX,SIX (2) Ti is two-phase (2PL) (3) Ti can unlock node Q only if none of Qs children are locked by Ti Observe that locks are acquired in root-to-leaf order, whereas they are released in leaf-to-root order.
Database System Concepts 15.42
T1(IX)
Examples
R
T1(IS)
R t3 t4
T1(S)
T1(IX)
t1
T1(X)
f2.1
t2
t1
t2
t3
t4
f2.2
f4.2
T1(SIX)
Child IS,S IS,S, IX, X, SIX [S, IS] not necessary X, IX, [SIX] none
15.43
R
T1(IX)
t1
t2
t3
T1(X)
f4.2
t4
f2.1
f2.2
f4.2
Agenda
s 2PL and variants s Timestamp-based s Optimistic CC: Validation-based protocols s Multiple granularity s Multi-version s Weaker Consistency (other than serializability) s Dealing with Deadlocks
15.44
Multiversion Schemes
s Multiversion schemes keep old versions of data item to increase
concurrency.
Multiversion Timestamp Ordering Multiversion Two-Phase Locking
s Each successful write results in the creation of a new version of
version of Q based on the timestamp of the transaction, and return the value of the selected version.
s reads never have to wait as an appropriate version is returned
immediately.
15.45
More on Consistency
s We have seen thus far: Serializability -- 2PL and timestamp s Weaker levels of consistency 1. Degree-two consistency: differs from two-phase locking in that
S-locks may be released at any time, and locks may be acquired at any time
X-locks must be held till end of transaction Serializability is not guaranteed, programmer must ensure that no erroneous database state will occur
1. Cursor stability:
For reads, each tuple is locked, read, and lock is immediately released X-locks are held till end of transaction Special case of degree-two consistency
15.46
Repeatable read: allows only committed records to be read, and repeating a read should return the same value (so read locks should be retained) However, the phantom phenomenon need not be prevented T1 may see some records inserted by T2, but may not see others inserted by T2 Read committed: same as degree two consistency, but most systems implement it as cursor-stability Read uncommitted: allows even uncommitted data to be read
s In many database systems (Oracle), Read Committed is the
15.47
Agenda
s 2PL and variants s Timestamp-based s Optimistic CC: Validation-based protocols s Multiple granularity s Multi-version s Weaker Consistency (other than serializability) s Dealing with Deadlocks
15.48
T2 T4 T1
T1
T2
T3 X(Z)
T4
T3
Detecting Deadlocks
s Wait-for graph has a cycle deadlock
T2 T4 T1 T3
Build wait-for graph, check for cycle How often? - Tunable Expect many deadlocks or many xctions involved Run often to avoid aborts Else run less often to reduce overhead
15.50
Which one?
Rollback the cheapest ones Cheapest ill-defined
Was it almost done? How much will it have to redo? Will it cause other rollbacks?
How far?
15.51
15.52