Lect 19
Lect 19
Lect 19
General Overview
Relational model - SQL
Transaction Concept
A transaction is a unit of program execution that
inconsistent.
be consistent.
State 1
State 2
ACID Properties
To preserve integrity of data, the database system must ensure:
Atomicity. Either all operations of the transaction are
properly reflected in the database or none are.
Consistency. Execution of a transaction in isolation
Review - Schedules
An interleaving of xction operations
T1
1
2
3
T2
A
B
C
D
T1
1
2
3
T2
A
B
C
D
Review - Schedules
T1
Serial Schedule
- All operations of each xction executed together,
xctions executed in succesion
Serializable schedule
- Any schedule whose final effect matches
that of any serial schedule
T1
1
2
1
2
3
T2
A
B
C
T2
A
B
C
Concurrency Control
Concurrency Control
How?
Tn
CC Scheduler
Serializable
schedule
Locks
Control access to a resource
Can block a xction until lock granted
Two modes:
Shared (read only)
eXclusive (read & write)
Granting Locks
Requesting locks
Compatible? Grant
Not? Block xction
shared
exclusive
shared
Yes
No
exclusive
No
No
Lock instructions
New instructions
- lock-S: shared lock request
- lock-X: exclusive lock request
- unlock: release previously held lock
Example:
T1
lock-X(B)
read(B)
B B-50
write(B)
unlock(B)
lock-X(A)
read(A)
A A + 50
write(A)
unlock(A)
T2
lock-S(A)
read(A)
unlock(A)
lock-S(B)
read(B)
unlock(B)
display(A+B)
Locking Issues
Starvation
Locking Issues
No xction proceeds:
Deadlock
T1
lock-X(B)
read(B)
T2
B B-50
write(B)
lock-S(A)
More later
read(A)
lock-S(B)
lock-X(A)
Locking Issues
Does not ensure serializability by itself:
T1
lock-X(B)
read(B)
B B-50
write(B)
unlock(B)
lock-X(A)
read(A)
A A + 50
write(A)
unlock(A)
T2
lock-S(A)
read(A)
unlock(A)
lock-S(B)
read(B)
unlock(B)
display(A+B)
T2 displays 50 less!!
2PL
Example: T1 in 2PL
Growing phase
Shrinking phase
T1
lock-X(B)
read(B)
B B - 50
write(B)
lock-X(A)
read(A)
A A - 50
write(A)
unlock(B)
unlock(A)
T2
T3
read(Q)
write(Q)
read(R)
write(R)
read(S)
T1
R/W(Q)
T2
T3
W
R/
)
(R
T2
T3
read(Q)
write(Q)
read(R)
write(R)
read(S)
write(S)
R/W(Q)
T1
W
R/
T2
)
(S
T3
W
R/
)
(R
Cycle Non-serializable
T1
T2
T3
2PL Issues
2PL does not prevent deadlock
T1
T2
lock-X(B)
read(B)
> 2 xctions involved?
- Rollbacks expensive
B B-50
write(B)
lock-S(A)
read(A)
lock-S(B)
lock-X(A)
2PL Variants
Strict two phase locking
Strict 2PL
T1
T2
T3
lock-X(A)
read(A)
lock-S(B)
read(B)
write(A)
unlock(A)
lock-X(A)
Strict 2PL
will not
allow that
read(A)
write(A)
unlock(A)
lock-S(A)
read(A)
<xction fails>
read by another
2PL Variants
Rigorous two-phase locking
Wait-for graph
Directed edge from Ti to Tj
Ti waiting for Tj
T1
T2
T3
T2
T4
T1
T4
T3
X(Z)
X(V)
X(W)
S(V)
S(W)
S(V)
Detecting Deadlocks
Wait-for graph has a cycle deadlock
T2
T4
T1
T3
How often?
- Tunable
Expect many deadlocks or many xctions involved
Run often to avoid aborts
Else run less often to reduce overhead
Which one?
Rollback the cheapest ones
Cheapest ill-defined
Deadlock Prevention
Assign priorities based on timestamps. Assume Ti wants a lock
Aborting a Transaction
If a transaction Ti is aborted, all its actions have to be undone.
The Log
The following actions are recorded in the log:
lock
table
Scheduler, part I
S(A),Read(A),X(B),Write(B)
Scheduler, part II
Read(A),Write(B)
Lock Management
Lock and unlock requests are handled by the lock manager
Lock table entry:
Latches, convoys
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.
Tree Protocol
Data items may be unlocked at any time. After T i unlocks Q, it cannot relock Q
Dynamic Databases
If we relax the assumption that the DB is a fixed collection of
The Problem
T1 implicitly assumes that it has locked the set of all sailor
Index Locking
If there is a dense index on the rating field using Alternative (2),
T1 should lock the index page containing the data entries with
rating = 1.
If there are no records with rating = 1, T1 must lock the index page
where such a data entry would be, if it existed!
If there is no suitable index, T1 must lock all pages, and lock the
Index
r=1
Data
Predicate Locking
Grant lock on all records that satisfy some logical predicate, e.g.
Locking in B+ Trees
How can we efficiently lock a particular leaf node?
Root node (and many higher level nodes) becomes bottleneck because
every tree access begins at the root.
unlock parent.
ROOT
20
35
23
G
20* 22*
H
23* 24*
Do:
1) Search 38
2) Insert 45
3) Insert 25
38
I
35* 36*
44
D
38* 41*
E
44*
Search: As before.
Insert/Delete:
Set locks as if for search, get to leaf, and set X lock on leaf.
If leaf is not safe, release all locks, and restart Xact using
previous Insert/Delete protocol.
Gambles that only leaf node will be modified; if not, S locks set on
the first pass to leaf are wasteful. In practice, better than previous
alg.