SlideShare a Scribd company logo
1 of 55
Download to read offline
1/11/2012 1
3. Concurrency Control
for Transactions
Part One
CSEP 545 Transaction Processing
Philip A. Bernstein
Copyright ©2012 Philip A. Bernstein
1/11/2012 2
Outline
1. A Simple System Model
2. Serializability Theory
3. Synchronization Requirements
for Recoverability
4. Two-Phase Locking
5. Preserving Transaction Handshakes
6. Implementing Two-Phase Locking
7. Deadlocks
1/11/2012 3
3.1 A Simple System Model
• Goal - Ensure serializable (SR) executions
• Implementation technique - Delay operations
that may lead to non-SR results (e.g. set locks
on shared data)
• For good performance minimize overhead and
delay from synchronization operations
• First, we’ll study how to get correct (SR) results
• Then, we’ll study performance implications
(mostly in Part Two)
1/11/2012 4
Assumption - Atomic Operations
• We will synchronize Reads and Writes.
• We must therefore assume they’re atomic
– else we’d have to synchronize the finer-grained
operations that implement Read and Write
• Read(x) - returns the current value of x in the DB
• Write(x, val) overwrites all of x (the whole page)
• This assumption of atomic operations allows us
to abstract executions as sequences of reads and
writes (without loss of information).
– Otherwise, what would wk[x] ri[x] mean?
• Also, commit (ci) and abort (ai) are atomic
1/11/2012 5
System Model
Transaction 1 Transaction N
Start, Commit, Abort
Read(x), Write(x)
Data
Manager
Database
Transaction 2
1/11/2012 6
3.2 Serializability Theory
• The theory is based on modeling executions as
histories, such as
H1 = r1[x] r2[x] w1[x] c1 w2[y] c2
• First, characterize a concurrency control
algorithm by the properties of histories it allows
• Then prove that any history having these
properties is SR
• Why bother? It helps you understand why
concurrency control algorithms work
1/11/2012 7
Equivalence of Histories
• Two operations conflict if their execution order
affects their return values or the DB state.
– A read and write on the same data item conflict.
– Two writes on the same data item conflict.
– Two reads (on the same data item) do not conflict.
• Two histories are equivalent if they have the
same operations and conflicting operations are
in the same order in both histories.
– Because only the relative order of conflicting
operations can affect the result of the histories.
1/11/2012 8
Examples of Equivalence
• The following histories are equivalent
H1 = r1[x] r2[x] w1[x] c1 w2[y] c2
H2 = r2[x] r1[x] w1[x] c1 w2[y] c2
H3 = r2[x] r1[x] w2[y] c2 w1[x] c1
H4 = r2[x] w2[y] c2 r1[x] w1[x] c1
• But none of them are equivalent to
H5 = r1[x] w1[x] r2[x] c1 w2[y] c2
which reverses the order of r2[x] w1[x] in H1,
because r2[x] and w1[x] conflict and
r2[x] precedes w1[x] in H1 - H4, but
r2[x] follows w1[x] in H5.
1/11/2012 9
Serializable Histories
• Definition: A history is serializable (SR) if it is
equivalent to a serial history
• For example,
H1 = r1[x] r2[x] w1[x] c1 w2[y] c2
is equivalent to
H4 = r2[x] w2[y] c2 r1[x] w1[x] c1
(Because H1 and H4 have the same operations and
the only conflicting operations, r2[x] and w1[x], are
in the same order in H1 and H4.)
• Therefore, H1 is serializable.
1/11/2012 10
Another Example
• H6 = r1[x] r2[x] w1[x] r3[x] w2[y] w3[x] c3 w1[y] c1 c2
is equivalent to a serial execution of T2 T1 T3,
H7 = r2[x] w2[y] c2 r1[x] w1[x] w1[y] c1 r3[x] w3[x] c3
• Each conflict implies a constraint on any equivalent
serial history:
H6 = r1[x] r2[x] w1[x] r3[x] w2[y] w3[x] c3 w1[y] c1 c2
T2T1 T1T3 T2T1
T2T3
1/11/2012 11
Serialization Graphs
• A serialization graph, SG(H), for history H tells the
effective execution order of transactions in H.
• Given history H, SG(H) is a directed graph whose
nodes are the committed transactions and whose
edges are all Ti  Tk such that at least one of Ti’s
operations precedes and conflicts with at least one
of Tk’s operations.
H6 = r1[x] r2[x] w1[x] r3[x] w2[y] w3[x] c3 w1[y] c1 c2
SG(H6) = T2 T1 T3
1/11/2012 12
The Serializability Theorem
A history is SR if and only if SG(H) is acyclic.
Proof: (if) SG(H) is acyclic. So let Hs be a serial
history consistent with SG(H). Each pair of
conflicting ops in H induces an edge in SG(H).
Since conflicting ops in Hs and H are in the same
order, HsH, so H is SR.
(only if) H is SR. Let Hs be a serial history equivalent
to H. We claim that if Ti  Tk in SG(H), then Ti
precedes Tk in Hs (else Hs ≢ H). If SG(H) had a
cycle, T1T2…TnT1, then T1 would precede
T1 in Hs, a contradiction. So SG(H) is acyclic.
1/11/2012 13
How to Use
the Serializability Theorem
• Characterize the set of histories that a
concurrency control algorithm allows.
• Prove that any such history must have an
acyclic serialization graph.
• Therefore, the algorithm guarantees SR
executions.
• We’ll use this soon to prove that locking
produces serializable executions.
1/11/2012 14
3.3 Synchronization Requirements
for Recoverability
• In addition to ensuring serializability, synchroni-
zation is needed to implement abort easily.
• When a transaction T aborts, the data manager
wipes out all of T’s effects, including
– Undoing T’s writes that were applied to the DB
• Remember before-images of writes
– Aborting transactions that read values written by T
(these are called cascading aborts)
• Remember which transactions read T’s writes
1/11/2012 15
Recoverability Example
• Example - w1[x] r2[x] w2[y]
– To abort T1, we must undo w1[x] and abort T2
(a cascading abort).
– System should keep before image of x in case T1 aborts
• We may even need to remember other before images.
– System should make T2 dependent on T1
• If T1 aborts T2 aborts.
• We want to avoid some of this bookkeeping.
1/11/2012 16
Recoverability
• If Tk reads from Ti and Ti aborts, then Tk must abort
– Example - w1[x] r2[x] a1 implies T2 must abort
• But what if Tk already committed? We’d be stuck.
– Example - w1[x] r2[x] c2 a1
– T2 can’t abort after it commits
• Executions must be recoverable:
A transaction T’s commit operation must follow the
commit of every transaction from which T read.
– Recoverable - w1[x] r2[x] c1 c2
– Not recoverable - w1[x] r2[x] c2 a1
• Recoverability requires synchronizing operations.
1/11/2012 17
Avoiding Cascading Aborts
• Cascading aborts are worth avoiding to
– Avoid complex bookkeeping, and
– Avoid an uncontrolled number of forced aborts
• To avoid cascading aborts, a data manager should
ensure transactions read only committed data
• Example
– Avoids cascading aborts: w1[x] c1 r2[x]
– Allows cascading aborts: w1[x] r2[x] a1
• A system that avoids cascading aborts also
guarantees recoverability.
1/11/2012 18
Strictness
• It’s convenient to undo a write, w[x], by restoring
its before image (x’s value before w[x] executed)
• Example - w1[x,1] writes the value “1” into x.
– w1[x,1] w1[y,3] c1 w2[y,1] r2[x] a2
– Abort T2 by restoring the before image of w2[y,1] (i.e. 3)
• But this isn’t always possible.
– For example, consider w1[x,2] w2[x,3] a1 a2
– a1 & a2 can’t be implemented by restoring before images
– Notice that w1[x,2] w2[x,3] a2 a1 would be OK
• A system is strict if it only reads or overwrites
committed data.
1/11/2012 19
Strictness (cont’d)
• More precisely, a system is strict if it only executes
ri[x] or wi[x] if all previous transactions that wrote x
committed or aborted.
• Examples (“…” marks a non-strict prefix)
– Strict: w1[x] c1 w2[x] a2
– Not strict: w1[x] w2[x] … c1 a2
– Strict: w1[x] w1[y] c1 r2[x] w2[y] a2
– Not strict: w1[x] w1[y] r2[x] … c1 w2[y] a2
– To see why strictness matters in the above histories,
consider what happens if T1 aborts.
• “Strict” implies “avoids cascading aborts.”
1/11/2012 20
3.4 Two-Phase Locking
• Basic locking - Each transaction sets a lock on each
data item before accessing the data
– The lock is a reservation
– There are read locks and write locks
– If one transaction has a write lock on x, then no other
transaction can have any lock on x
• Example
– rli[x], rui[x], wli[x], wui[x] denote lock/unlock operations
– wl1[x] w1[x] rl2[x] r2[x] is impossible
– wl1[x] w1[x] wu1[x] rl2[x] r2[x] is OK
1/11/2012 21
Basic Locking Isn’t Enough
• Basic locking doesn’t guarantee serializability
• rl1[x] r1[x] ru1[x] wl1[y] w1[y] wu1[y]c1
rl2[y] r2[y] wl2[x] w2[x] ru2[y] wu2[x] c2
• Eliminating the lock operations, we have
r1[x] r2[y] w2[x] c2 w1[y] c1 which isn’t SR
• The problem is that locks aren’t being released
properly.
1/11/2012 22
Two-Phase Locking (2PL) Protocol
• A transaction is two-phase locked if:
– Before reading x, it sets a read lock on x
– Before writing x, it sets a write lock on x
– It holds each lock until after it executes the
corresponding operation
– After its first unlock operation, it requests no new locks.
• Each transaction sets locks during a growing phase
and releases them during a shrinking phase.
• Example - on the previous page T2 is two-phase
locked, but not T1 since ru1[x] < wl1[y]
– use “<” for “precedes”.
1/11/2012 23
2PL Theorem: If all transactions in an execution are
two-phase locked, then the execution is SR.
Proof: Let H be a 2PL history and Ti  Tk in SG.
– Then Ti read x and Tk later wrote x,
– Or Ti wrote x and Tk later read or wrote x
• If Ti  Tk, then Ti released a lock before Tk
obtained some lock.
• If Ti  Tk  Tm, then Ti released a lock before Tm
obtained some lock (because Tk is two-phase).
• If Ti ...  Ti, then Ti released a lock before Ti
obtained some lock, breaking the 2-phase rule.
• So there cannot be a cycle in SG(H). By the
Serializability Theorem, H is SR.
1/11/2012 24
2PL and Recoverability
• 2PL does not guarantee recoverability
• This non-recoverable execution is 2-phase locked
wl1[x] w1[x] wu1[x] rl2[x] r2[x] c2 … c1
– Hence, it is not strict and allows cascading aborts
• However, holding write locks until after commit or
abort guarantees strictness
– Hence avoids cascading aborts and is recoverable
– In the above example, T1 must commit before its first
unlock-write (wu1): wl1[x] w1[x] c1 wu1[x] rl2[x] r2[x] c2
1/11/2012 25
Automating Locking
• 2PL can be hidden from the application.
• When a data manager gets a Read or Write
operation from a transaction, it sets a read or write
lock.
• How does the data manager know it’s safe to
release locks (and be two-phase)?
• Ordinarily, the data manager holds a transaction’s
locks until it commits or aborts. A data manager
– Can release read locks after it receives commit
– Releases write locks only after it processes commit,
to ensure strictness.
1/11/2012 26
3.5 Preserving Transaction Handshakes
• Read and Write are the only operations the
system will control to attain serializability.
• So, if transactions communicate via messages,
then implement SendMsg as Write, and
ReceiveMsg as Read.
• Else, you could have the following:
w1[x] r2[x] send2[M] receive1[M]
– Data manager didn’t know about send/receive and
thought the execution was SR.
• Also watch out for brain transport.
1/11/2012 27
Transactions Can Communicate via Brain
Transport
T1: Start
. . .
Display output
Commit
T2: Start
Get input from display
. . .
Commit
User reads output
…
User enters input
Brain
transport
1/11/2012 28
Brain Transport (cont’d)
• For practical purposes, if the user waits for T1 to
commit before starting T2, then the data manager
can ignore brain transport.
• This is called a transaction handshake
(T1 commits before T2 starts).
• Reason - Locking preserves the order imposed by
transaction handshakes
– e.g., it serializes T1 before T2.
1/11/2012 29
2PL Preserves Transaction Handshakes
• 2PL serializes transactions consistent with all
transaction handshakes. I.e. there’s an equivalent
serial execution that preserves the transaction order
in all transaction handshakes.
• This isn’t true for arbitrary SR executions. E.g.
– r1[x] w2[x] c2 r3[y] c3 w1[y] c1
– T2 commits before T3 starts, but the only equivalent
serial execution is T3 T1 T2
– The history can’t occur using 2PL. Try adding lock ops:
rl1[x] r1[x] wl1[y] ru1[x] wl2[x] w2[x] c2 wu2[x]
but now we’re stuck, since we can’t set rl3[y] r3[y].
How to show whether a given
history H was produced by 2PL?
• H could have been produced via 2PL iff you can
add lock operations to H, following 2PL protocol.
• First add rl1[x]: rl1[x] r1[x] w2[x] c2 r3[y] c3 w1[y] c1
– Next, T2 must have set wl2[x] before executing w2[x]
• So r1[x] must have released rl1[x] before w2[x] ran
• Since T1 is 2PL, it must have write-locked y before unlocking x
– rl1[x] r1[x] wl1[y] ru1[x] wl2[x] w2[x] c2 wu2[x]
• Now we’re stuck, since T3 could not have set rl3[y] before r3[y],
since T1 could not have unlocked y until after w1[y].
– Hence, H could not have been produced by 2PL.
1/18/2012 30
1/11/2012 31
2PL Preserves Transaction
Handshakes (cont’d)
• Stating this more formally …
• Theorem:
For any 2PL execution H,
there is an equivalent serial execution Hs,
such that for all Ti, Tk,
if Ti committed before Tk started in H,
then Ti precedes Tk in Hs.
1/11/2012 32
Brain Transport  One Last Time
• If a user reads displayed output of Ti and
wants to use that output as input to transaction Tk,
then he/she should wait for Ti to commit before
starting Tk.
• The user can then rely on transaction handshake
preservation to ensure Ti is serialized before Tk.
1/11/2012 33
3.6 Implementing Two-Phase Locking
• Even if you never implement a DB system, it’s
valuable to understand locking implementation,
because it can have a big effect on performance.
• A data manager implements locking by
– Implementing a lock manager
– Setting a lock for each Read and Write
– Handling deadlocks.
1/11/2012 34
System Model
Transaction 1 Transaction N
Database
System
Start,
SQL Ops
Commit, Abort
Query Optimizer
Query Executor
Access Method
(record-oriented files)
Page-oriented Files
Database
1/11/2012 35
How to Implement SQL
• Query Optimizer - translates SQL into an ordered
expression of relational DB operators (Select,
Project, Join)
• Query Executor - executes the ordered expression
by running a program for each operator, which in
turn accesses records of files
• Access methods - provides indexed record-at-a-
time access to files (OpenScan, GetNext, …)
• Page-oriented files - Read or Write (page address)
1/11/2012 36
Which Operations Get Synchronized?
Record-oriented operations
Page-oriented operations
SQL operations
Query Optimizer
Query Executor
Access Method
(record-oriented files)
Page-oriented Files
• It’s a tradeoff between
– Amount of concurrency and
– Runtime expense and programming complexity
of synchronization
1/11/2012 37
Lock Manager
• A lock manager services the operations
– Lock(trans-id, data-item-id, mode)
– Unlock(trans-id, data-item-id)
– Unlock(trans-id)
• It stores locks in a lock table. Lock op inserts
[trans-id, mode] in the table. Unlock deletes it.
Data Item List of Locks Wait List
x [T1,r] [T2,r] [T3,w]
y [T4,w] [T5,w] [T6, r]
1/11/2012 38
Lock Manager (cont’d)
• Caller generates data-item-id, e.g. by hashing data
item name
• The lock table is hashed on data-item-id
• Lock and Unlock must be atomic, so access to the
lock table must be “locked”
• Lock and Unlock are called frequently. They must
be very fast. Average < 100 instructions.
– This is hard, in part due to slow compare-and-swap
operations needed for atomic access to lock table.
1/11/2012 39
Lock Manager (cont’d)
• In MS SQL Server
– Locks are approx 32 bytes each.
– Each lock contains a Database-Id, Object-Id, and other
resource-specific lock information such as record id
(RID) or key.
– Each lock is attached to lock resource block (64 bytes)
and lock owner block (32 bytes).
1/11/2012 40
Locking Granularity
• Granularity - size of data items to lock
– e.g., files, pages, records, fields
• Coarse granularity implies
– Very few locks, so little locking overhead
– Must lock large chunks of data, so high chance of
conflict, so concurrency may be low
• Fine granularity implies
– Many locks, so high locking overhead
– Locking conflict occurs only when two transactions try
to access the exact same data concurrently
• High performance TP requires record locking
1/11/2012 41
Multigranularity Locking (MGL)
• Allow different txns to lock at different granularity
– Big queries should lock coarse-grained data (e.g. tables)
– Short transactions lock fine-grained data (e.g. rows)
• Lock manager can’t detect these conflicts
– Each data item (e.g., table or row) has a different id
• Multigranularity locking “trick”
– Exploit the natural hierarchy of data containment
– Before locking fine-grained data, set intention locks on coarse
grained data that contains it
– e.g., before setting a read-lock on a row, get an
intention-read-lock on the table that contains the row
– An intention-read-lock conflicts with a write lock on the same item
1/11/2012 42
3.7 Deadlocks
• A set of transactions (txns) is deadlocked if every
transaction in the set is blocked and will remain
blocked unless the system intervenes
– Example rl1[x] granted
rl2[y] granted
wl2[x] blocked
wl1[y] blocked and deadlocked
• Deadlock is 2PL’s way to avoid non-SR executions
– rl1[x] r1[x] rl2[y] r2[y] … can’t run w2[x] w1[y] and be SR
• To repair a deadlock, you must abort a transaction
– Releasing a txn T’s lock without aborting T breaks 2PL
1/11/2012 43
Deadlock Prevention
• Never grant a lock that can lead to deadlock
• Often advocated in operating systems
• Useless for TP, because it would require running
transactions serially
– Example to prevent the previous deadlock,
rl1[x] rl2[y] wl2[x] wl1[y], the system can’t grant rl2[y]
• Avoiding deadlock by resource ordering is unusable
in general, since it overly constrains applications
– But may help for certain high frequency deadlocks
• Setting all locks when txn begins requires too much
advance knowledge and reduces concurrency
1/11/2012 44
Deadlock Detection
• Detection approach: Detect deadlocks automatically
and abort a deadlocked transactions (the victim)
• It’s the preferred approach, because it
– Allows higher resource utilization and
– Uses cheaper algorithms
• Timeout-based deadlock detection - If a transaction
is blocked for too long, then abort it
– Simple and easy to implement
– But aborts unnecessarily and
– Some deadlocks persist for too long
1/11/2012 45
Detection Using Waits-For Graph
• Explicit deadlock detection - Use a Waits-For Graph
– Nodes = {transactions}
– Edges = {Ti  Tk | Ti is waiting for Tk to release a lock}
– Example (previous deadlock) T1 T2
• Theorem: If there’s a deadlock, then the waits-for
graph has a cycle
1/11/2012 46
Detection Using Waits-For Graph
(cont’d)
• So, to find deadlocks
– When a transaction blocks, add an edge to the graph.
– Periodically check for cycles in the waits-for graph.
• Need not test for deadlocks too often.
– A cycle won’t disappear until you detect it and break it.
• When a deadlock is detected, select a victim from
the cycle and abort it.
• Select a victim that hasn’t done much work
– E.g., has set the fewest locks.
1/11/2012 47
Cyclic Restart
• Transactions can cause each other to abort forever.
– T1 starts running. Then T2 starts running.
– They deadlock and T1 (the oldest) is aborted.
– T1 restarts, bumps into T2 and again deadlocks
– T2 (the oldest) is aborted ...
• Choosing the youngest in a cycle as victim avoids
cyclic restart, since the oldest running transaction is
never the victim.
• Can combine with other heuristics, e.g. fewest-locks
1/11/2012 48
MS SQL Server
• Aborts the transaction that is “cheapest” to roll
back.
– “Cheapest” is determined by the amount of log
generated.
– Allows transactions that you’ve invested a lot in to
complete.
• SET DEADLOCK_PRIORITY LOW
(vs. NORMAL) causes a transaction to sacrifice
itself as a victim.
1/11/2012 49
Distributed Locking
• Suppose a transaction can access data at many
data managers
• Each data manager sets locks in the usual way
• When a transaction commits or aborts, it runs
two-phase commit to notify all data managers it
accessed
• The only remaining issue is distributed deadlock
1/11/2012 50
Distributed Deadlock
• The deadlock spans two nodes.
Neither node alone can detect it.
• Timeout-based detection is popular. Its weaknesses
are less important in the distributed case:
– Aborts unnecessarily and some deadlocks persist too long
– Possibly abort younger unblocked transaction to avoid
cyclic restart
rl1[x]
wl2[x] (blocked)
Node 1
rl2[y]
wl1[y] (blocked)
Node 2
1/11/2012 51
Oracle Deadlock Handling
• Uses a waits-for graph for single-server
deadlock detection.
• The transaction that detects the deadlock is
the victim.
• Uses timeouts to detect distributed
deadlocks.
1/11/2012 52
Fancier Dist’d Deadlock Detection
• Use waits-for graph cycle detection with a central
deadlock detection server
– More work than timeout-based detection, and
there’s no evidence it performs better
– Phantom deadlocks? - No, because each waits-for edge
is an SG edge. So, WFG cycle => SG cycle
(modulo spontaneous aborts)
• Path pushing (a.k.a. flooding) - Send paths Ti …
 Tk to each node where Tk might be blocked.
– Detects short cycles quickly
– Hard to know where to send paths
– Possibly too many messages
1/11/2012 53
Locking Performance
• The following is oversimplified. We’ll revisit it.
• Deadlocks are rare.
– Typically 1-2% of transactions deadlock.
• Locking performance problems are not rare.
• The problem is too much blocking.
• The solution is to reduce the “locking load”.
• Good heuristic – If more than 30% of transactions
are blocked, then reduce the number of concurrent
transactions.
1/11/2012 54
Lock Conversions
• Lock conversion - upgrading an r-lock to a w-lock
– e.g., Ti = read(x) … write(x)
• This is one place where deadlocks are an issue
– If two txns convert a lock concurrently, they’ll deadlock
(both get an r-lock on x before either gets a w-lock).
– To avoid the deadlock, a caller can get a w-lock first and
down-grade to an r-lock if it doesn’t need to write.
– We’ll see other solutions later.
• This is step 3 of the course project. Its main purpose
is to ensure you understand the lock manager code.
1/11/2012 55
What’s Coming in Part Two?
• Locking Performance
• More details on multigranularity locking
• Hot spot techniques
• Query-Update Techniques
• Phantoms
• B-Trees and Tree locking

More Related Content

What's hot

Concurrency Control in Database Management System
Concurrency Control in Database Management SystemConcurrency Control in Database Management System
Concurrency Control in Database Management SystemJanki Shah
 
4. concurrency control
4. concurrency control4. concurrency control
4. concurrency controlAbDul ThaYyal
 
Dbms ii mca-ch9-transaction-processing-2013
Dbms ii mca-ch9-transaction-processing-2013Dbms ii mca-ch9-transaction-processing-2013
Dbms ii mca-ch9-transaction-processing-2013Prosanta Ghosh
 
16. Concurrency Control in DBMS
16. Concurrency Control in DBMS16. Concurrency Control in DBMS
16. Concurrency Control in DBMSkoolkampus
 
Concurrent transactions
Concurrent transactionsConcurrent transactions
Concurrent transactionsSajan Sahu
 
Concurrency Control Techniques
Concurrency Control TechniquesConcurrency Control Techniques
Concurrency Control TechniquesRaj vardhan
 
Databases: Locking Methods
Databases: Locking MethodsDatabases: Locking Methods
Databases: Locking MethodsDamian T. Gordon
 
Database concurrency control &amp; recovery (1)
Database concurrency control &amp; recovery (1)Database concurrency control &amp; recovery (1)
Database concurrency control &amp; recovery (1)Rashid Khan
 
A PRUDENT-PRECEDENCE CONCURRENCY CONTROL PROTOCOL FOR HIGH DATA CONTENTION DA...
A PRUDENT-PRECEDENCE CONCURRENCY CONTROL PROTOCOL FOR HIGH DATA CONTENTION DA...A PRUDENT-PRECEDENCE CONCURRENCY CONTROL PROTOCOL FOR HIGH DATA CONTENTION DA...
A PRUDENT-PRECEDENCE CONCURRENCY CONTROL PROTOCOL FOR HIGH DATA CONTENTION DA...ijdms
 
Transactions and Concurrency Control
Transactions and Concurrency ControlTransactions and Concurrency Control
Transactions and Concurrency ControlDilum Bandara
 
Database management system chapter16
Database management system chapter16Database management system chapter16
Database management system chapter16Md. Mahedi Mahfuj
 

What's hot (20)

Concurrency control
Concurrency controlConcurrency control
Concurrency control
 
Concurrency Control in Database Management System
Concurrency Control in Database Management SystemConcurrency Control in Database Management System
Concurrency Control in Database Management System
 
4. concurrency control
4. concurrency control4. concurrency control
4. concurrency control
 
Concurrency
ConcurrencyConcurrency
Concurrency
 
concurrency-control
concurrency-controlconcurrency-control
concurrency-control
 
Dbms ii mca-ch9-transaction-processing-2013
Dbms ii mca-ch9-transaction-processing-2013Dbms ii mca-ch9-transaction-processing-2013
Dbms ii mca-ch9-transaction-processing-2013
 
Ch16
Ch16Ch16
Ch16
 
16. Concurrency Control in DBMS
16. Concurrency Control in DBMS16. Concurrency Control in DBMS
16. Concurrency Control in DBMS
 
Concurrent transactions
Concurrent transactionsConcurrent transactions
Concurrent transactions
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Concurrency control
Concurrency controlConcurrency control
Concurrency control
 
Concurrency Control Techniques
Concurrency Control TechniquesConcurrency Control Techniques
Concurrency Control Techniques
 
Unit 6
Unit 6Unit 6
Unit 6
 
Databases: Locking Methods
Databases: Locking MethodsDatabases: Locking Methods
Databases: Locking Methods
 
Database concurrency control &amp; recovery (1)
Database concurrency control &amp; recovery (1)Database concurrency control &amp; recovery (1)
Database concurrency control &amp; recovery (1)
 
A PRUDENT-PRECEDENCE CONCURRENCY CONTROL PROTOCOL FOR HIGH DATA CONTENTION DA...
A PRUDENT-PRECEDENCE CONCURRENCY CONTROL PROTOCOL FOR HIGH DATA CONTENTION DA...A PRUDENT-PRECEDENCE CONCURRENCY CONTROL PROTOCOL FOR HIGH DATA CONTENTION DA...
A PRUDENT-PRECEDENCE CONCURRENCY CONTROL PROTOCOL FOR HIGH DATA CONTENTION DA...
 
2 phase locking protocol DBMS
2 phase locking protocol DBMS2 phase locking protocol DBMS
2 phase locking protocol DBMS
 
Transactions and Concurrency Control
Transactions and Concurrency ControlTransactions and Concurrency Control
Transactions and Concurrency Control
 
Database management system chapter16
Database management system chapter16Database management system chapter16
Database management system chapter16
 
Unit 06 dbms
Unit 06 dbmsUnit 06 dbms
Unit 06 dbms
 

Viewers also liked

Viewers also liked (11)

Solution7.2012
Solution7.2012Solution7.2012
Solution7.2012
 
Project description2012
Project description2012Project description2012
Project description2012
 
10b rm
10b rm10b rm
10b rm
 
17 wics99 harkey
17 wics99 harkey17 wics99 harkey
17 wics99 harkey
 
2 shadowing
2 shadowing2 shadowing
2 shadowing
 
08 message and_queues_dieter_gawlick
08 message and_queues_dieter_gawlick08 message and_queues_dieter_gawlick
08 message and_queues_dieter_gawlick
 
16 greg hope_com_wics
16 greg hope_com_wics16 greg hope_com_wics
16 greg hope_com_wics
 
20 access paths
20 access paths20 access paths
20 access paths
 
9 queuing
9 queuing9 queuing
9 queuing
 
1 introduction
1 introduction1 introduction
1 introduction
 
14 scaleabilty wics
14 scaleabilty wics14 scaleabilty wics
14 scaleabilty wics
 

Similar to 3 concurrency controlone_v3

7. CSEN3101-MIV-TransactionProcessing-SolvedProblems-06.11.2023.pptx
7. CSEN3101-MIV-TransactionProcessing-SolvedProblems-06.11.2023.pptx7. CSEN3101-MIV-TransactionProcessing-SolvedProblems-06.11.2023.pptx
7. CSEN3101-MIV-TransactionProcessing-SolvedProblems-06.11.2023.pptxquantumiq448
 
Characteristics Schedule based on Recover-ability & Serial-ability
Characteristics Schedule based on Recover-ability & Serial-abilityCharacteristics Schedule based on Recover-ability & Serial-ability
Characteristics Schedule based on Recover-ability & Serial-abilityMeghaj Mallick
 
db unit 4 dbms protocols in transaction
db unit 4 dbms  protocols in transactiondb unit 4 dbms  protocols in transaction
db unit 4 dbms protocols in transactionKumari Naveen
 
recoverability and serializability dbms
recoverability and serializability  dbmsrecoverability and serializability  dbms
recoverability and serializability dbmsKumari Naveen
 
concurrencycontrol_databasemanagement_system
concurrencycontrol_databasemanagement_systemconcurrencycontrol_databasemanagement_system
concurrencycontrol_databasemanagement_systemDeepaliKonety
 
UNIT 2- TRANSACTION CONCEPTS AND CONCURRENCY CONCEPTS (1).pdf
UNIT 2- TRANSACTION CONCEPTS AND CONCURRENCY CONCEPTS (1).pdfUNIT 2- TRANSACTION CONCEPTS AND CONCURRENCY CONCEPTS (1).pdf
UNIT 2- TRANSACTION CONCEPTS AND CONCURRENCY CONCEPTS (1).pdfKavitaShinde26
 
5-Chapter Five - Concurrenc.ppt
5-Chapter Five - Concurrenc.ppt5-Chapter Five - Concurrenc.ppt
5-Chapter Five - Concurrenc.pptDadiTesfaye
 
Transactions in dbms
Transactions in dbmsTransactions in dbms
Transactions in dbmsNancy Gulati
 
Concurrency control iN Advanced Database
Concurrency control iN Advanced DatabaseConcurrency control iN Advanced Database
Concurrency control iN Advanced DatabaseDaniel181688
 
Concepts of Data Base Management Systems
Concepts of Data Base Management SystemsConcepts of Data Base Management Systems
Concepts of Data Base Management SystemsDinesh Devireddy
 
UNIT-IV: Transaction Processing Concepts
UNIT-IV: Transaction Processing ConceptsUNIT-IV: Transaction Processing Concepts
UNIT-IV: Transaction Processing ConceptsRaj vardhan
 
concurrencycontrol from power pint pdf a
concurrencycontrol  from power pint pdf aconcurrencycontrol  from power pint pdf a
concurrencycontrol from power pint pdf aMdAyanParwez
 

Similar to 3 concurrency controlone_v3 (20)

7. CSEN3101-MIV-TransactionProcessing-SolvedProblems-06.11.2023.pptx
7. CSEN3101-MIV-TransactionProcessing-SolvedProblems-06.11.2023.pptx7. CSEN3101-MIV-TransactionProcessing-SolvedProblems-06.11.2023.pptx
7. CSEN3101-MIV-TransactionProcessing-SolvedProblems-06.11.2023.pptx
 
Characteristics Schedule based on Recover-ability & Serial-ability
Characteristics Schedule based on Recover-ability & Serial-abilityCharacteristics Schedule based on Recover-ability & Serial-ability
Characteristics Schedule based on Recover-ability & Serial-ability
 
db unit 4 dbms protocols in transaction
db unit 4 dbms  protocols in transactiondb unit 4 dbms  protocols in transaction
db unit 4 dbms protocols in transaction
 
recoverability and serializability dbms
recoverability and serializability  dbmsrecoverability and serializability  dbms
recoverability and serializability dbms
 
concurrencycontrol_databasemanagement_system
concurrencycontrol_databasemanagement_systemconcurrencycontrol_databasemanagement_system
concurrencycontrol_databasemanagement_system
 
Concurrent Transactions.ppt
Concurrent Transactions.pptConcurrent Transactions.ppt
Concurrent Transactions.ppt
 
UNIT 2- TRANSACTION CONCEPTS AND CONCURRENCY CONCEPTS (1).pdf
UNIT 2- TRANSACTION CONCEPTS AND CONCURRENCY CONCEPTS (1).pdfUNIT 2- TRANSACTION CONCEPTS AND CONCURRENCY CONCEPTS (1).pdf
UNIT 2- TRANSACTION CONCEPTS AND CONCURRENCY CONCEPTS (1).pdf
 
5-Chapter Five - Concurrenc.ppt
5-Chapter Five - Concurrenc.ppt5-Chapter Five - Concurrenc.ppt
5-Chapter Five - Concurrenc.ppt
 
unit06-dbms-new.ppt
unit06-dbms-new.pptunit06-dbms-new.ppt
unit06-dbms-new.ppt
 
Transaction.pptx
Transaction.pptxTransaction.pptx
Transaction.pptx
 
Chapter17
Chapter17Chapter17
Chapter17
 
Transactions in dbms
Transactions in dbmsTransactions in dbms
Transactions in dbms
 
Question answer
Question answerQuestion answer
Question answer
 
Transactions.pptx
Transactions.pptxTransactions.pptx
Transactions.pptx
 
Unit-IV_transaction.pptx
Unit-IV_transaction.pptxUnit-IV_transaction.pptx
Unit-IV_transaction.pptx
 
Transactions
TransactionsTransactions
Transactions
 
Concurrency control iN Advanced Database
Concurrency control iN Advanced DatabaseConcurrency control iN Advanced Database
Concurrency control iN Advanced Database
 
Concepts of Data Base Management Systems
Concepts of Data Base Management SystemsConcepts of Data Base Management Systems
Concepts of Data Base Management Systems
 
UNIT-IV: Transaction Processing Concepts
UNIT-IV: Transaction Processing ConceptsUNIT-IV: Transaction Processing Concepts
UNIT-IV: Transaction Processing Concepts
 
concurrencycontrol from power pint pdf a
concurrencycontrol  from power pint pdf aconcurrencycontrol  from power pint pdf a
concurrencycontrol from power pint pdf a
 

More from ashish61_scs

More from ashish61_scs (20)

7 concurrency controltwo
7 concurrency controltwo7 concurrency controltwo
7 concurrency controltwo
 
Transactions
TransactionsTransactions
Transactions
 
22 levine
22 levine22 levine
22 levine
 
21 domino mohan-1
21 domino mohan-121 domino mohan-1
21 domino mohan-1
 
19 structured files
19 structured files19 structured files
19 structured files
 
18 philbe replication stanford99
18 philbe replication stanford9918 philbe replication stanford99
18 philbe replication stanford99
 
15 bufferand records
15 bufferand records15 bufferand records
15 bufferand records
 
14 turing wics
14 turing wics14 turing wics
14 turing wics
 
13 tm adv
13 tm adv13 tm adv
13 tm adv
 
11 tm
11 tm11 tm
11 tm
 
10a log
10a log10a log
10a log
 
09 workflow
09 workflow09 workflow
09 workflow
 
06 07 lock
06 07 lock06 07 lock
06 07 lock
 
05 tp mon_orbs
05 tp mon_orbs05 tp mon_orbs
05 tp mon_orbs
 
04 transaction models
04 transaction models04 transaction models
04 transaction models
 
03 fault model
03 fault model03 fault model
03 fault model
 
02 fault tolerance
02 fault tolerance02 fault tolerance
02 fault tolerance
 
01 whirlwind tour
01 whirlwind tour01 whirlwind tour
01 whirlwind tour
 
Solution5.2012
Solution5.2012Solution5.2012
Solution5.2012
 
Solution6.2012
Solution6.2012Solution6.2012
Solution6.2012
 

Recently uploaded

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 

Recently uploaded (20)

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 

3 concurrency controlone_v3

  • 1. 1/11/2012 1 3. Concurrency Control for Transactions Part One CSEP 545 Transaction Processing Philip A. Bernstein Copyright ©2012 Philip A. Bernstein
  • 2. 1/11/2012 2 Outline 1. A Simple System Model 2. Serializability Theory 3. Synchronization Requirements for Recoverability 4. Two-Phase Locking 5. Preserving Transaction Handshakes 6. Implementing Two-Phase Locking 7. Deadlocks
  • 3. 1/11/2012 3 3.1 A Simple System Model • Goal - Ensure serializable (SR) executions • Implementation technique - Delay operations that may lead to non-SR results (e.g. set locks on shared data) • For good performance minimize overhead and delay from synchronization operations • First, we’ll study how to get correct (SR) results • Then, we’ll study performance implications (mostly in Part Two)
  • 4. 1/11/2012 4 Assumption - Atomic Operations • We will synchronize Reads and Writes. • We must therefore assume they’re atomic – else we’d have to synchronize the finer-grained operations that implement Read and Write • Read(x) - returns the current value of x in the DB • Write(x, val) overwrites all of x (the whole page) • This assumption of atomic operations allows us to abstract executions as sequences of reads and writes (without loss of information). – Otherwise, what would wk[x] ri[x] mean? • Also, commit (ci) and abort (ai) are atomic
  • 5. 1/11/2012 5 System Model Transaction 1 Transaction N Start, Commit, Abort Read(x), Write(x) Data Manager Database Transaction 2
  • 6. 1/11/2012 6 3.2 Serializability Theory • The theory is based on modeling executions as histories, such as H1 = r1[x] r2[x] w1[x] c1 w2[y] c2 • First, characterize a concurrency control algorithm by the properties of histories it allows • Then prove that any history having these properties is SR • Why bother? It helps you understand why concurrency control algorithms work
  • 7. 1/11/2012 7 Equivalence of Histories • Two operations conflict if their execution order affects their return values or the DB state. – A read and write on the same data item conflict. – Two writes on the same data item conflict. – Two reads (on the same data item) do not conflict. • Two histories are equivalent if they have the same operations and conflicting operations are in the same order in both histories. – Because only the relative order of conflicting operations can affect the result of the histories.
  • 8. 1/11/2012 8 Examples of Equivalence • The following histories are equivalent H1 = r1[x] r2[x] w1[x] c1 w2[y] c2 H2 = r2[x] r1[x] w1[x] c1 w2[y] c2 H3 = r2[x] r1[x] w2[y] c2 w1[x] c1 H4 = r2[x] w2[y] c2 r1[x] w1[x] c1 • But none of them are equivalent to H5 = r1[x] w1[x] r2[x] c1 w2[y] c2 which reverses the order of r2[x] w1[x] in H1, because r2[x] and w1[x] conflict and r2[x] precedes w1[x] in H1 - H4, but r2[x] follows w1[x] in H5.
  • 9. 1/11/2012 9 Serializable Histories • Definition: A history is serializable (SR) if it is equivalent to a serial history • For example, H1 = r1[x] r2[x] w1[x] c1 w2[y] c2 is equivalent to H4 = r2[x] w2[y] c2 r1[x] w1[x] c1 (Because H1 and H4 have the same operations and the only conflicting operations, r2[x] and w1[x], are in the same order in H1 and H4.) • Therefore, H1 is serializable.
  • 10. 1/11/2012 10 Another Example • H6 = r1[x] r2[x] w1[x] r3[x] w2[y] w3[x] c3 w1[y] c1 c2 is equivalent to a serial execution of T2 T1 T3, H7 = r2[x] w2[y] c2 r1[x] w1[x] w1[y] c1 r3[x] w3[x] c3 • Each conflict implies a constraint on any equivalent serial history: H6 = r1[x] r2[x] w1[x] r3[x] w2[y] w3[x] c3 w1[y] c1 c2 T2T1 T1T3 T2T1 T2T3
  • 11. 1/11/2012 11 Serialization Graphs • A serialization graph, SG(H), for history H tells the effective execution order of transactions in H. • Given history H, SG(H) is a directed graph whose nodes are the committed transactions and whose edges are all Ti  Tk such that at least one of Ti’s operations precedes and conflicts with at least one of Tk’s operations. H6 = r1[x] r2[x] w1[x] r3[x] w2[y] w3[x] c3 w1[y] c1 c2 SG(H6) = T2 T1 T3
  • 12. 1/11/2012 12 The Serializability Theorem A history is SR if and only if SG(H) is acyclic. Proof: (if) SG(H) is acyclic. So let Hs be a serial history consistent with SG(H). Each pair of conflicting ops in H induces an edge in SG(H). Since conflicting ops in Hs and H are in the same order, HsH, so H is SR. (only if) H is SR. Let Hs be a serial history equivalent to H. We claim that if Ti  Tk in SG(H), then Ti precedes Tk in Hs (else Hs ≢ H). If SG(H) had a cycle, T1T2…TnT1, then T1 would precede T1 in Hs, a contradiction. So SG(H) is acyclic.
  • 13. 1/11/2012 13 How to Use the Serializability Theorem • Characterize the set of histories that a concurrency control algorithm allows. • Prove that any such history must have an acyclic serialization graph. • Therefore, the algorithm guarantees SR executions. • We’ll use this soon to prove that locking produces serializable executions.
  • 14. 1/11/2012 14 3.3 Synchronization Requirements for Recoverability • In addition to ensuring serializability, synchroni- zation is needed to implement abort easily. • When a transaction T aborts, the data manager wipes out all of T’s effects, including – Undoing T’s writes that were applied to the DB • Remember before-images of writes – Aborting transactions that read values written by T (these are called cascading aborts) • Remember which transactions read T’s writes
  • 15. 1/11/2012 15 Recoverability Example • Example - w1[x] r2[x] w2[y] – To abort T1, we must undo w1[x] and abort T2 (a cascading abort). – System should keep before image of x in case T1 aborts • We may even need to remember other before images. – System should make T2 dependent on T1 • If T1 aborts T2 aborts. • We want to avoid some of this bookkeeping.
  • 16. 1/11/2012 16 Recoverability • If Tk reads from Ti and Ti aborts, then Tk must abort – Example - w1[x] r2[x] a1 implies T2 must abort • But what if Tk already committed? We’d be stuck. – Example - w1[x] r2[x] c2 a1 – T2 can’t abort after it commits • Executions must be recoverable: A transaction T’s commit operation must follow the commit of every transaction from which T read. – Recoverable - w1[x] r2[x] c1 c2 – Not recoverable - w1[x] r2[x] c2 a1 • Recoverability requires synchronizing operations.
  • 17. 1/11/2012 17 Avoiding Cascading Aborts • Cascading aborts are worth avoiding to – Avoid complex bookkeeping, and – Avoid an uncontrolled number of forced aborts • To avoid cascading aborts, a data manager should ensure transactions read only committed data • Example – Avoids cascading aborts: w1[x] c1 r2[x] – Allows cascading aborts: w1[x] r2[x] a1 • A system that avoids cascading aborts also guarantees recoverability.
  • 18. 1/11/2012 18 Strictness • It’s convenient to undo a write, w[x], by restoring its before image (x’s value before w[x] executed) • Example - w1[x,1] writes the value “1” into x. – w1[x,1] w1[y,3] c1 w2[y,1] r2[x] a2 – Abort T2 by restoring the before image of w2[y,1] (i.e. 3) • But this isn’t always possible. – For example, consider w1[x,2] w2[x,3] a1 a2 – a1 & a2 can’t be implemented by restoring before images – Notice that w1[x,2] w2[x,3] a2 a1 would be OK • A system is strict if it only reads or overwrites committed data.
  • 19. 1/11/2012 19 Strictness (cont’d) • More precisely, a system is strict if it only executes ri[x] or wi[x] if all previous transactions that wrote x committed or aborted. • Examples (“…” marks a non-strict prefix) – Strict: w1[x] c1 w2[x] a2 – Not strict: w1[x] w2[x] … c1 a2 – Strict: w1[x] w1[y] c1 r2[x] w2[y] a2 – Not strict: w1[x] w1[y] r2[x] … c1 w2[y] a2 – To see why strictness matters in the above histories, consider what happens if T1 aborts. • “Strict” implies “avoids cascading aborts.”
  • 20. 1/11/2012 20 3.4 Two-Phase Locking • Basic locking - Each transaction sets a lock on each data item before accessing the data – The lock is a reservation – There are read locks and write locks – If one transaction has a write lock on x, then no other transaction can have any lock on x • Example – rli[x], rui[x], wli[x], wui[x] denote lock/unlock operations – wl1[x] w1[x] rl2[x] r2[x] is impossible – wl1[x] w1[x] wu1[x] rl2[x] r2[x] is OK
  • 21. 1/11/2012 21 Basic Locking Isn’t Enough • Basic locking doesn’t guarantee serializability • rl1[x] r1[x] ru1[x] wl1[y] w1[y] wu1[y]c1 rl2[y] r2[y] wl2[x] w2[x] ru2[y] wu2[x] c2 • Eliminating the lock operations, we have r1[x] r2[y] w2[x] c2 w1[y] c1 which isn’t SR • The problem is that locks aren’t being released properly.
  • 22. 1/11/2012 22 Two-Phase Locking (2PL) Protocol • A transaction is two-phase locked if: – Before reading x, it sets a read lock on x – Before writing x, it sets a write lock on x – It holds each lock until after it executes the corresponding operation – After its first unlock operation, it requests no new locks. • Each transaction sets locks during a growing phase and releases them during a shrinking phase. • Example - on the previous page T2 is two-phase locked, but not T1 since ru1[x] < wl1[y] – use “<” for “precedes”.
  • 23. 1/11/2012 23 2PL Theorem: If all transactions in an execution are two-phase locked, then the execution is SR. Proof: Let H be a 2PL history and Ti  Tk in SG. – Then Ti read x and Tk later wrote x, – Or Ti wrote x and Tk later read or wrote x • If Ti  Tk, then Ti released a lock before Tk obtained some lock. • If Ti  Tk  Tm, then Ti released a lock before Tm obtained some lock (because Tk is two-phase). • If Ti ...  Ti, then Ti released a lock before Ti obtained some lock, breaking the 2-phase rule. • So there cannot be a cycle in SG(H). By the Serializability Theorem, H is SR.
  • 24. 1/11/2012 24 2PL and Recoverability • 2PL does not guarantee recoverability • This non-recoverable execution is 2-phase locked wl1[x] w1[x] wu1[x] rl2[x] r2[x] c2 … c1 – Hence, it is not strict and allows cascading aborts • However, holding write locks until after commit or abort guarantees strictness – Hence avoids cascading aborts and is recoverable – In the above example, T1 must commit before its first unlock-write (wu1): wl1[x] w1[x] c1 wu1[x] rl2[x] r2[x] c2
  • 25. 1/11/2012 25 Automating Locking • 2PL can be hidden from the application. • When a data manager gets a Read or Write operation from a transaction, it sets a read or write lock. • How does the data manager know it’s safe to release locks (and be two-phase)? • Ordinarily, the data manager holds a transaction’s locks until it commits or aborts. A data manager – Can release read locks after it receives commit – Releases write locks only after it processes commit, to ensure strictness.
  • 26. 1/11/2012 26 3.5 Preserving Transaction Handshakes • Read and Write are the only operations the system will control to attain serializability. • So, if transactions communicate via messages, then implement SendMsg as Write, and ReceiveMsg as Read. • Else, you could have the following: w1[x] r2[x] send2[M] receive1[M] – Data manager didn’t know about send/receive and thought the execution was SR. • Also watch out for brain transport.
  • 27. 1/11/2012 27 Transactions Can Communicate via Brain Transport T1: Start . . . Display output Commit T2: Start Get input from display . . . Commit User reads output … User enters input Brain transport
  • 28. 1/11/2012 28 Brain Transport (cont’d) • For practical purposes, if the user waits for T1 to commit before starting T2, then the data manager can ignore brain transport. • This is called a transaction handshake (T1 commits before T2 starts). • Reason - Locking preserves the order imposed by transaction handshakes – e.g., it serializes T1 before T2.
  • 29. 1/11/2012 29 2PL Preserves Transaction Handshakes • 2PL serializes transactions consistent with all transaction handshakes. I.e. there’s an equivalent serial execution that preserves the transaction order in all transaction handshakes. • This isn’t true for arbitrary SR executions. E.g. – r1[x] w2[x] c2 r3[y] c3 w1[y] c1 – T2 commits before T3 starts, but the only equivalent serial execution is T3 T1 T2 – The history can’t occur using 2PL. Try adding lock ops: rl1[x] r1[x] wl1[y] ru1[x] wl2[x] w2[x] c2 wu2[x] but now we’re stuck, since we can’t set rl3[y] r3[y].
  • 30. How to show whether a given history H was produced by 2PL? • H could have been produced via 2PL iff you can add lock operations to H, following 2PL protocol. • First add rl1[x]: rl1[x] r1[x] w2[x] c2 r3[y] c3 w1[y] c1 – Next, T2 must have set wl2[x] before executing w2[x] • So r1[x] must have released rl1[x] before w2[x] ran • Since T1 is 2PL, it must have write-locked y before unlocking x – rl1[x] r1[x] wl1[y] ru1[x] wl2[x] w2[x] c2 wu2[x] • Now we’re stuck, since T3 could not have set rl3[y] before r3[y], since T1 could not have unlocked y until after w1[y]. – Hence, H could not have been produced by 2PL. 1/18/2012 30
  • 31. 1/11/2012 31 2PL Preserves Transaction Handshakes (cont’d) • Stating this more formally … • Theorem: For any 2PL execution H, there is an equivalent serial execution Hs, such that for all Ti, Tk, if Ti committed before Tk started in H, then Ti precedes Tk in Hs.
  • 32. 1/11/2012 32 Brain Transport  One Last Time • If a user reads displayed output of Ti and wants to use that output as input to transaction Tk, then he/she should wait for Ti to commit before starting Tk. • The user can then rely on transaction handshake preservation to ensure Ti is serialized before Tk.
  • 33. 1/11/2012 33 3.6 Implementing Two-Phase Locking • Even if you never implement a DB system, it’s valuable to understand locking implementation, because it can have a big effect on performance. • A data manager implements locking by – Implementing a lock manager – Setting a lock for each Read and Write – Handling deadlocks.
  • 34. 1/11/2012 34 System Model Transaction 1 Transaction N Database System Start, SQL Ops Commit, Abort Query Optimizer Query Executor Access Method (record-oriented files) Page-oriented Files Database
  • 35. 1/11/2012 35 How to Implement SQL • Query Optimizer - translates SQL into an ordered expression of relational DB operators (Select, Project, Join) • Query Executor - executes the ordered expression by running a program for each operator, which in turn accesses records of files • Access methods - provides indexed record-at-a- time access to files (OpenScan, GetNext, …) • Page-oriented files - Read or Write (page address)
  • 36. 1/11/2012 36 Which Operations Get Synchronized? Record-oriented operations Page-oriented operations SQL operations Query Optimizer Query Executor Access Method (record-oriented files) Page-oriented Files • It’s a tradeoff between – Amount of concurrency and – Runtime expense and programming complexity of synchronization
  • 37. 1/11/2012 37 Lock Manager • A lock manager services the operations – Lock(trans-id, data-item-id, mode) – Unlock(trans-id, data-item-id) – Unlock(trans-id) • It stores locks in a lock table. Lock op inserts [trans-id, mode] in the table. Unlock deletes it. Data Item List of Locks Wait List x [T1,r] [T2,r] [T3,w] y [T4,w] [T5,w] [T6, r]
  • 38. 1/11/2012 38 Lock Manager (cont’d) • Caller generates data-item-id, e.g. by hashing data item name • The lock table is hashed on data-item-id • Lock and Unlock must be atomic, so access to the lock table must be “locked” • Lock and Unlock are called frequently. They must be very fast. Average < 100 instructions. – This is hard, in part due to slow compare-and-swap operations needed for atomic access to lock table.
  • 39. 1/11/2012 39 Lock Manager (cont’d) • In MS SQL Server – Locks are approx 32 bytes each. – Each lock contains a Database-Id, Object-Id, and other resource-specific lock information such as record id (RID) or key. – Each lock is attached to lock resource block (64 bytes) and lock owner block (32 bytes).
  • 40. 1/11/2012 40 Locking Granularity • Granularity - size of data items to lock – e.g., files, pages, records, fields • Coarse granularity implies – Very few locks, so little locking overhead – Must lock large chunks of data, so high chance of conflict, so concurrency may be low • Fine granularity implies – Many locks, so high locking overhead – Locking conflict occurs only when two transactions try to access the exact same data concurrently • High performance TP requires record locking
  • 41. 1/11/2012 41 Multigranularity Locking (MGL) • Allow different txns to lock at different granularity – Big queries should lock coarse-grained data (e.g. tables) – Short transactions lock fine-grained data (e.g. rows) • Lock manager can’t detect these conflicts – Each data item (e.g., table or row) has a different id • Multigranularity locking “trick” – Exploit the natural hierarchy of data containment – Before locking fine-grained data, set intention locks on coarse grained data that contains it – e.g., before setting a read-lock on a row, get an intention-read-lock on the table that contains the row – An intention-read-lock conflicts with a write lock on the same item
  • 42. 1/11/2012 42 3.7 Deadlocks • A set of transactions (txns) is deadlocked if every transaction in the set is blocked and will remain blocked unless the system intervenes – Example rl1[x] granted rl2[y] granted wl2[x] blocked wl1[y] blocked and deadlocked • Deadlock is 2PL’s way to avoid non-SR executions – rl1[x] r1[x] rl2[y] r2[y] … can’t run w2[x] w1[y] and be SR • To repair a deadlock, you must abort a transaction – Releasing a txn T’s lock without aborting T breaks 2PL
  • 43. 1/11/2012 43 Deadlock Prevention • Never grant a lock that can lead to deadlock • Often advocated in operating systems • Useless for TP, because it would require running transactions serially – Example to prevent the previous deadlock, rl1[x] rl2[y] wl2[x] wl1[y], the system can’t grant rl2[y] • Avoiding deadlock by resource ordering is unusable in general, since it overly constrains applications – But may help for certain high frequency deadlocks • Setting all locks when txn begins requires too much advance knowledge and reduces concurrency
  • 44. 1/11/2012 44 Deadlock Detection • Detection approach: Detect deadlocks automatically and abort a deadlocked transactions (the victim) • It’s the preferred approach, because it – Allows higher resource utilization and – Uses cheaper algorithms • Timeout-based deadlock detection - If a transaction is blocked for too long, then abort it – Simple and easy to implement – But aborts unnecessarily and – Some deadlocks persist for too long
  • 45. 1/11/2012 45 Detection Using Waits-For Graph • Explicit deadlock detection - Use a Waits-For Graph – Nodes = {transactions} – Edges = {Ti  Tk | Ti is waiting for Tk to release a lock} – Example (previous deadlock) T1 T2 • Theorem: If there’s a deadlock, then the waits-for graph has a cycle
  • 46. 1/11/2012 46 Detection Using Waits-For Graph (cont’d) • So, to find deadlocks – When a transaction blocks, add an edge to the graph. – Periodically check for cycles in the waits-for graph. • Need not test for deadlocks too often. – A cycle won’t disappear until you detect it and break it. • When a deadlock is detected, select a victim from the cycle and abort it. • Select a victim that hasn’t done much work – E.g., has set the fewest locks.
  • 47. 1/11/2012 47 Cyclic Restart • Transactions can cause each other to abort forever. – T1 starts running. Then T2 starts running. – They deadlock and T1 (the oldest) is aborted. – T1 restarts, bumps into T2 and again deadlocks – T2 (the oldest) is aborted ... • Choosing the youngest in a cycle as victim avoids cyclic restart, since the oldest running transaction is never the victim. • Can combine with other heuristics, e.g. fewest-locks
  • 48. 1/11/2012 48 MS SQL Server • Aborts the transaction that is “cheapest” to roll back. – “Cheapest” is determined by the amount of log generated. – Allows transactions that you’ve invested a lot in to complete. • SET DEADLOCK_PRIORITY LOW (vs. NORMAL) causes a transaction to sacrifice itself as a victim.
  • 49. 1/11/2012 49 Distributed Locking • Suppose a transaction can access data at many data managers • Each data manager sets locks in the usual way • When a transaction commits or aborts, it runs two-phase commit to notify all data managers it accessed • The only remaining issue is distributed deadlock
  • 50. 1/11/2012 50 Distributed Deadlock • The deadlock spans two nodes. Neither node alone can detect it. • Timeout-based detection is popular. Its weaknesses are less important in the distributed case: – Aborts unnecessarily and some deadlocks persist too long – Possibly abort younger unblocked transaction to avoid cyclic restart rl1[x] wl2[x] (blocked) Node 1 rl2[y] wl1[y] (blocked) Node 2
  • 51. 1/11/2012 51 Oracle Deadlock Handling • Uses a waits-for graph for single-server deadlock detection. • The transaction that detects the deadlock is the victim. • Uses timeouts to detect distributed deadlocks.
  • 52. 1/11/2012 52 Fancier Dist’d Deadlock Detection • Use waits-for graph cycle detection with a central deadlock detection server – More work than timeout-based detection, and there’s no evidence it performs better – Phantom deadlocks? - No, because each waits-for edge is an SG edge. So, WFG cycle => SG cycle (modulo spontaneous aborts) • Path pushing (a.k.a. flooding) - Send paths Ti …  Tk to each node where Tk might be blocked. – Detects short cycles quickly – Hard to know where to send paths – Possibly too many messages
  • 53. 1/11/2012 53 Locking Performance • The following is oversimplified. We’ll revisit it. • Deadlocks are rare. – Typically 1-2% of transactions deadlock. • Locking performance problems are not rare. • The problem is too much blocking. • The solution is to reduce the “locking load”. • Good heuristic – If more than 30% of transactions are blocked, then reduce the number of concurrent transactions.
  • 54. 1/11/2012 54 Lock Conversions • Lock conversion - upgrading an r-lock to a w-lock – e.g., Ti = read(x) … write(x) • This is one place where deadlocks are an issue – If two txns convert a lock concurrently, they’ll deadlock (both get an r-lock on x before either gets a w-lock). – To avoid the deadlock, a caller can get a w-lock first and down-grade to an r-lock if it doesn’t need to write. – We’ll see other solutions later. • This is step 3 of the course project. Its main purpose is to ensure you understand the lock manager code.
  • 55. 1/11/2012 55 What’s Coming in Part Two? • Locking Performance • More details on multigranularity locking • Hot spot techniques • Query-Update Techniques • Phantoms • B-Trees and Tree locking