![Page 1: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/1.jpg)
Intro to Database Systems
15-445/15-645
Fall 2019
Andy PavloComputer Science Carnegie Mellon UniversityAP
17 Two-Phase Locking
![Page 2: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/2.jpg)
CMU 15-445/645 (Fall 2019)
L AST CL ASS
Conflict Serializable→ Verify using either the "swapping" method or
dependency graphs.→ Any DBMS that says that they support "serializable"
isolation does this.
View Serializable→ No efficient way to verify.→ Andy doesn't know of any DBMS that supports this.
2
![Page 3: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/3.jpg)
CMU 15-445/645 (Fall 2019)
EXAMPLE
3
BEGINR(A)
W(A)
R(A)COMMIT
BEGINR(A)
W(A)COMMIT
TIM
ESchedule
T1 T2
![Page 4: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/4.jpg)
CMU 15-445/645 (Fall 2019)
OBSERVATION
We need a way to guarantee that all execution schedules are correct (i.e., serializable) without knowing the entire schedule ahead of time.
Solution: Use locks to protect database objects.
4
![Page 5: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/5.jpg)
CMU 15-445/645 (Fall 2019)
Lock Manager
EXECUTING WITH LOCKS
5
Granted (T1→A)
TIM
E
BEGINLOCK(A)R(A)
W(A)R(A)UNLOCK(A)
COMMIT
BEGINLOCK(A)
R(A)W(A)UNLOCK(A)COMMIT
Schedule
T1 T2
![Page 6: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/6.jpg)
CMU 15-445/645 (Fall 2019)
Lock Manager
EXECUTING WITH LOCKS
5
Granted (T1→A)
Denied!
TIM
E
BEGINLOCK(A)R(A)
W(A)R(A)UNLOCK(A)
COMMIT
BEGINLOCK(A)
R(A)W(A)UNLOCK(A)COMMIT
Schedule
T1 T2
![Page 7: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/7.jpg)
CMU 15-445/645 (Fall 2019)
Lock Manager
EXECUTING WITH LOCKS
5
Granted (T1→A)
Denied!
TIM
E
BEGINLOCK(A)R(A)
W(A)R(A)UNLOCK(A)
COMMIT
BEGINLOCK(A)
R(A)W(A)UNLOCK(A)COMMIT
Schedule
T1 T2
![Page 8: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/8.jpg)
CMU 15-445/645 (Fall 2019)
Lock Manager
EXECUTING WITH LOCKS
5
Granted (T1→A)
Denied!
Released (T1→A)TIM
E
BEGINLOCK(A)R(A)
W(A)R(A)UNLOCK(A)
COMMIT
BEGINLOCK(A)
R(A)W(A)UNLOCK(A)COMMIT
Schedule
T1 T2
![Page 9: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/9.jpg)
CMU 15-445/645 (Fall 2019)
Lock Manager
EXECUTING WITH LOCKS
5
Granted (T1→A)
Denied!
Granted (T2→A)
Released (T1→A)
Released (T2→A)
TIM
E
BEGINLOCK(A)R(A)
W(A)R(A)UNLOCK(A)
COMMIT
BEGINLOCK(A)
R(A)W(A)UNLOCK(A)COMMIT
Schedule
T1 T2
![Page 10: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/10.jpg)
CMU 15-445/645 (Fall 2019)
TODAY'S AGENDA
Lock Types
Two-Phase Locking
Deadlock Detection + Prevention
Hierarchical Locking
Isolation Levels
6
![Page 11: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/11.jpg)
CMU 15-445/645 (Fall 2019)
LOCKS VS. L ATCHES
7
Locks Latches
Separate… User transactions Threads
Protect… Database Contents In-Memory Data Structures
During… Entire Transactions Critical Sections
Modes… Shared, Exclusive, Update, Intention
Read, Write
Deadlock Detection & Resolution Avoidance
…by… Waits-for, Timeout, Aborts Coding Discipline
Kept in… Lock Manager Protected Data StructureSource: Goetz Graefe
![Page 12: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/12.jpg)
CMU 15-445/645 (Fall 2019)
BASIC LOCK T YPES
S-LOCK: Shared locks for reads.
X-LOCK: Exclusive locks for writes.
8
Shared Exclusive
Shared ✔ X
Exclusive X X
Compatibility Matrix
![Page 13: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/13.jpg)
CMU 15-445/645 (Fall 2019)
EXECUTING WITH LOCKS
Transactions request locks (or upgrades).
Lock manager grants or blocks requests.
Transactions release locks.
Lock manager updates its internal lock-table.→ It keeps track of what transactions hold what locks and
what transactions are waiting to acquire any locks.
9
![Page 14: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/14.jpg)
CMU 15-445/645 (Fall 2019)
Schedule Lock Manager
BEGINX-LOCK(A)R(A)W(A)UNLOCK(A)
S-LOCK(A)R(A)UNLOCK(A)COMMIT
BEGINX-LOCK(A)W(A)UNLOCK(A)
COMMIT
EXECUTING WITH LOCKS
10
Granted (T1→A)
TIM
ET1 T2
![Page 15: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/15.jpg)
CMU 15-445/645 (Fall 2019)
Schedule Lock Manager
BEGINX-LOCK(A)R(A)W(A)UNLOCK(A)
S-LOCK(A)R(A)UNLOCK(A)COMMIT
BEGINX-LOCK(A)W(A)UNLOCK(A)
COMMIT
EXECUTING WITH LOCKS
10
Granted (T1→A)
Released (T1→A)
TIM
ET1 T2
![Page 16: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/16.jpg)
CMU 15-445/645 (Fall 2019)
Schedule Lock Manager
BEGINX-LOCK(A)R(A)W(A)UNLOCK(A)
S-LOCK(A)R(A)UNLOCK(A)COMMIT
BEGINX-LOCK(A)W(A)UNLOCK(A)
COMMIT
EXECUTING WITH LOCKS
10
Granted (T1→A)
Granted (T2→A)
Released (T1→A)
Released (T2→A)
TIM
ET1 T2
![Page 17: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/17.jpg)
CMU 15-445/645 (Fall 2019)
Schedule Lock Manager
BEGINX-LOCK(A)R(A)W(A)UNLOCK(A)
S-LOCK(A)R(A)UNLOCK(A)COMMIT
BEGINX-LOCK(A)W(A)UNLOCK(A)
COMMIT
EXECUTING WITH LOCKS
10
Granted (T1→A)
Granted (T2→A)
Released (T1→A)
Released (T2→A)
Granted (T1→A)
Released (T1→A)
TIM
ET1 T2
![Page 18: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/18.jpg)
CMU 15-445/645 (Fall 2019)
Schedule Lock Manager
BEGINX-LOCK(A)R(A)W(A)UNLOCK(A)
S-LOCK(A)R(A)UNLOCK(A)COMMIT
BEGINX-LOCK(A)W(A)UNLOCK(A)
COMMIT
EXECUTING WITH LOCKS
10
Granted (T1→A)
Granted (T2→A)
Released (T1→A)
Released (T2→A)
Granted (T1→A)
Released (T1→A)
TIM
ET1 T2
![Page 19: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/19.jpg)
CMU 15-445/645 (Fall 2019)
CONCURRENCY CONTROL PROTOCOL
Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn can access an object in the database on the fly.
The protocol does not need to know all the queries that a txn will execute ahead of time.
11
![Page 20: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/20.jpg)
CMU 15-445/645 (Fall 2019)
TWO-PHASE LOCKING
Phase #1: Growing→ Each txn requests the locks that it needs from the DBMS’s
lock manager.→ The lock manager grants/denies lock requests.
Phase #2: Shrinking→ The txn is allowed to only release locks that it previously
acquired. It cannot acquire new locks.
12
![Page 21: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/21.jpg)
CMU 15-445/645 (Fall 2019)
TWO-PHASE LOCKING
The txn is not allowed to acquire/upgrade locks after the growing phase finishes.
13
# o
f Lo
cks
TIME
Growing Phase Shrinking Phase
Transaction Lifetime
![Page 22: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/22.jpg)
CMU 15-445/645 (Fall 2019)
TWO-PHASE LOCKING
The txn is not allowed to acquire/upgrade locks after the growing phase finishes.
14
TIME
Transaction Lifetime
# o
f Lo
cks
2PL Violation!
Growing Phase Shrinking Phase
![Page 23: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/23.jpg)
CMU 15-445/645 (Fall 2019)
Lock Manager
BEGINX-LOCK(A)R(A)W(A)
R(A)UNLOCK(A)COMMIT
BEGINX-LOCK(A)
W(A)UNLOCK(A)COMMIT
EXECUTING WITH 2PL
15
Granted (T1→A)
TIM
ESchedule
T1 T2
![Page 24: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/24.jpg)
CMU 15-445/645 (Fall 2019)
Lock Manager
BEGINX-LOCK(A)R(A)W(A)
R(A)UNLOCK(A)COMMIT
BEGINX-LOCK(A)
W(A)UNLOCK(A)COMMIT
EXECUTING WITH 2PL
15
Granted (T1→A)
Denied!
TIM
ESchedule
T1 T2
![Page 25: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/25.jpg)
CMU 15-445/645 (Fall 2019)
Lock Manager
BEGINX-LOCK(A)R(A)W(A)
R(A)UNLOCK(A)COMMIT
BEGINX-LOCK(A)
W(A)UNLOCK(A)COMMIT
EXECUTING WITH 2PL
15
Granted (T1→A)
Denied!
Released (T1→A)
TIM
ESchedule
T1 T2
![Page 26: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/26.jpg)
CMU 15-445/645 (Fall 2019)
Lock Manager
BEGINX-LOCK(A)R(A)W(A)
R(A)UNLOCK(A)COMMIT
BEGINX-LOCK(A)
W(A)UNLOCK(A)COMMIT
EXECUTING WITH 2PL
15
Granted (T1→A)
Denied!
Released (T2→A)
Released (T1→A)
Granted (T2→A)
TIM
ESchedule
T1 T2
![Page 27: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/27.jpg)
CMU 15-445/645 (Fall 2019)
TWO-PHASE LOCKING
2PL on its own is sufficient to guarantee conflict serializability.→ It generates schedules whose precedence graph is acyclic.
But it is subject to cascading aborts.
16
![Page 28: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/28.jpg)
CMU 15-445/645 (Fall 2019)
Schedule
T1 T2
2PL CASCADING ABORTS
17
BEGINX-LOCK(A)X-LOCK(B)R(A)W(A)UNLOCK(A)
R(B)W(B)ABORT
BEGIN
X-LOCK(A)R(A)W(A)
⋮
TIM
E
![Page 29: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/29.jpg)
CMU 15-445/645 (Fall 2019)
Schedule
T1 T2
2PL CASCADING ABORTS
17
This is a permissible schedule in 2PL, but the DBMS has to also abort T2 when T1 aborts.→ Any information about T1 cannot
be "leaked" to the outside world.
BEGINX-LOCK(A)X-LOCK(B)R(A)W(A)UNLOCK(A)
R(B)W(B)ABORT
BEGIN
X-LOCK(A)R(A)W(A)
⋮This is all wasted work!
TIM
E
![Page 30: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/30.jpg)
CMU 15-445/645 (Fall 2019)
2PL OBSERVATIONS
There are potential schedules that are serializable but would not be allowed by 2PL.→ Locking limits concurrency.
May still have "dirty reads".→ Solution: Strong Strict 2PL (aka Rigorous 2PL)
May lead to deadlocks.→ Solution: Detection or Prevention
18
![Page 31: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/31.jpg)
CMU 15-445/645 (Fall 2019)
STRONG STRICT TWO -PHASE LOCKING
The txn is not allowed to acquire/upgrade locks after the growing phase finishes.
Allows only conflict serializable schedules, but it is often stronger than needed for some apps.
19
TIME
# o
f Lo
cks
Release all locks at end of txn.
Growing Phase Shrinking Phase
![Page 32: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/32.jpg)
CMU 15-445/645 (Fall 2019)
STRONG STRICT TWO-PHASE LOCKING
A schedule is strict if a value written by a txn is not read or overwritten by other txns until that txn finishes.
Advantages:→ Does not incur cascading aborts.→ Aborted txns can be undone by just restoring original
values of modified tuples.
20
![Page 33: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/33.jpg)
CMU 15-445/645 (Fall 2019)
EXAMPLES
T1 – Move $100 from Andy’s account (A) to his bookie’s account (B).
T2 – Compute the total amount in all accounts and return it to the application.
21
BEGINA=A-100B=B+100COMMIT
BEGINECHO A+BCOMMIT
T1 T2
![Page 34: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/34.jpg)
CMU 15-445/645 (Fall 2019)
Schedule
T1 T2
NON-2PL EXAMPLE
22
A=1000, B=1000
Initial Database State
BEGINX-LOCK(A)R(A)
A=A-100W(A)UNLOCK(A)
X-LOCK(B)
R(B)B=B+100W(B)UNLOCK(B)COMMIT
BEGIN
S-LOCK(A)
R(A)UNLOCK(A)S-LOCK(B)
R(B)UNLOCK(B)ECHO A+BCOMMIT
TIM
E
![Page 35: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/35.jpg)
CMU 15-445/645 (Fall 2019)
Schedule
T1 T2
NON-2PL EXAMPLE
22
A=1000, B=1000
Initial Database State
BEGINX-LOCK(A)R(A)
A=A-100W(A)UNLOCK(A)
X-LOCK(B)
R(B)B=B+100W(B)UNLOCK(B)COMMIT
BEGIN
S-LOCK(A)
R(A)UNLOCK(A)S-LOCK(B)
R(B)UNLOCK(B)ECHO A+BCOMMIT
TIM
E
![Page 36: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/36.jpg)
CMU 15-445/645 (Fall 2019)
Schedule
T1 T2
NON-2PL EXAMPLE
22
A=1000, B=1000
Initial Database State
BEGINX-LOCK(A)R(A)
A=A-100W(A)UNLOCK(A)
X-LOCK(B)
R(B)B=B+100W(B)UNLOCK(B)COMMIT
BEGIN
S-LOCK(A)
R(A)UNLOCK(A)S-LOCK(B)
R(B)UNLOCK(B)ECHO A+BCOMMIT
TIM
E
![Page 37: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/37.jpg)
CMU 15-445/645 (Fall 2019)
Schedule
T1 T2
NON-2PL EXAMPLE
22
A=1000, B=1000
Initial Database State
A+B=1100
T2 Output
BEGINX-LOCK(A)R(A)
A=A-100W(A)UNLOCK(A)
X-LOCK(B)
R(B)B=B+100W(B)UNLOCK(B)COMMIT
BEGIN
S-LOCK(A)
R(A)UNLOCK(A)S-LOCK(B)
R(B)UNLOCK(B)ECHO A+BCOMMIT
TIM
E
![Page 38: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/38.jpg)
CMU 15-445/645 (Fall 2019)
2PL EXAMPLE
23
BEGINX-LOCK(A)R(A)
A=A-100W(A)X-LOCK(B)UNLOCK(A)
R(B)B=B+100W(B)UNLOCK(B)COMMIT
BEGIN
S-LOCK(A)
R(A)S-LOCK(B)
R(B)UNLOCK(A)UNLOCK(B)ECHO A+BCOMMIT
TIM
ESchedule
T1 T2 A=1000, B=1000
Initial Database State
![Page 39: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/39.jpg)
CMU 15-445/645 (Fall 2019)
2PL EXAMPLE
23
BEGINX-LOCK(A)R(A)
A=A-100W(A)X-LOCK(B)UNLOCK(A)
R(B)B=B+100W(B)UNLOCK(B)COMMIT
BEGIN
S-LOCK(A)
R(A)S-LOCK(B)
R(B)UNLOCK(A)UNLOCK(B)ECHO A+BCOMMIT
TIM
ESchedule
T1 T2 A=1000, B=1000
Initial Database State
![Page 40: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/40.jpg)
CMU 15-445/645 (Fall 2019)
2PL EXAMPLE
23
BEGINX-LOCK(A)R(A)
A=A-100W(A)X-LOCK(B)UNLOCK(A)
R(B)B=B+100W(B)UNLOCK(B)COMMIT
BEGIN
S-LOCK(A)
R(A)S-LOCK(B)
R(B)UNLOCK(A)UNLOCK(B)ECHO A+BCOMMIT
TIM
ESchedule
T1 T2 A=1000, B=1000
Initial Database State
A+B=2000
T2 Output
![Page 41: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/41.jpg)
CMU 15-445/645 (Fall 2019)
STRONG STRICT 2PL EXAMPLE
24
BEGINX-LOCK(A)R(A)A=A-100W(A)X-LOCK(B)R(B)B=B+100W(B)UNLOCK(A)UNLOCK(B)COMMIT
BEGIN
S-LOCK(A)
R(A)S-LOCK(B)R(B)ECHO A+BUNLOCK(A)UNLOCK(B)COMMIT
TIM
ESchedule
T1 T2 A=1000, B=1000
Initial Database State
![Page 42: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/42.jpg)
CMU 15-445/645 (Fall 2019)
STRONG STRICT 2PL EXAMPLE
24
BEGINX-LOCK(A)R(A)A=A-100W(A)X-LOCK(B)R(B)B=B+100W(B)UNLOCK(A)UNLOCK(B)COMMIT
BEGIN
S-LOCK(A)
R(A)S-LOCK(B)R(B)ECHO A+BUNLOCK(A)UNLOCK(B)COMMIT
TIM
ESchedule
T1 T2 A=1000, B=1000
Initial Database State
A+B=2000
T2 Output
![Page 43: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/43.jpg)
CMU 15-445/645 (Fall 2019)
All Schedules
UNIVERSE OF SCHEDULES
25
View Serializable
Conflict Serializable
No CascadingAborts Serial
![Page 44: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/44.jpg)
CMU 15-445/645 (Fall 2019)
2PL OBSERVATIONS
There are potential schedules that are serializable but would not be allowed by 2PL.→ Locking limits concurrency.
May still have "dirty reads".→ Solution: Strong Strict 2PL (Rigorous)
May lead to deadlocks.→ Solution: Detection or Prevention
26
![Page 45: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/45.jpg)
CMU 15-445/645 (Fall 2019)
Schedule
T1 T2
Lock Manager
BEGINX-LOCK(A)
R(A)X-LOCK(B)
BEGIN
S-LOCK(B)R(B)S-LOCK(A)
SHIT JUST GOT REAL, SON
27
Granted (T1→A)
TIM
E
![Page 46: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/46.jpg)
CMU 15-445/645 (Fall 2019)
Schedule
T1 T2
Lock Manager
BEGINX-LOCK(A)
R(A)X-LOCK(B)
BEGIN
S-LOCK(B)R(B)S-LOCK(A)
SHIT JUST GOT REAL, SON
27
Granted (T1→A)
Granted (T2→B)
TIM
E
![Page 47: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/47.jpg)
CMU 15-445/645 (Fall 2019)
Schedule
T1 T2
Lock Manager
BEGINX-LOCK(A)
R(A)X-LOCK(B)
BEGIN
S-LOCK(B)R(B)S-LOCK(A)
SHIT JUST GOT REAL, SON
27
Granted (T1→A)
Denied!
Granted (T2→B)
TIM
E
![Page 48: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/48.jpg)
CMU 15-445/645 (Fall 2019)
Schedule
T1 T2
Lock Manager
BEGINX-LOCK(A)
R(A)X-LOCK(B)
BEGIN
S-LOCK(B)R(B)S-LOCK(A)
SHIT JUST GOT REAL, SON
27
Granted (T1→A)
Denied!
Granted (T2→B)
Denied!TIM
E
![Page 49: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/49.jpg)
CMU 15-445/645 (Fall 2019)
Schedule
T1 T2
Lock Manager
BEGINX-LOCK(A)
R(A)X-LOCK(B)
BEGIN
S-LOCK(B)R(B)S-LOCK(A)
SHIT JUST GOT REAL, SON
27
Granted (T1→A)
Denied!
Granted (T2→B)
Denied!TIM
E
![Page 50: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/50.jpg)
CMU 15-445/645 (Fall 2019)
2PL DEADLOCKS
A deadlock is a cycle of transactions waiting for locks to be released by each other.
Two ways of dealing with deadlocks:→ Approach #1: Deadlock Detection→ Approach #2: Deadlock Prevention
28
![Page 51: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/51.jpg)
CMU 15-445/645 (Fall 2019)
DEADLOCK DETECTION
The DBMS creates a waits-for graph to keep track of what locks each txn is waiting to acquire:→ Nodes are transactions→ Edge from Ti to Tj if Ti is waiting for Tj to release a lock.
The system periodically checks for cycles in waits-for graph and then decides how to break it.
29
![Page 52: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/52.jpg)
CMU 15-445/645 (Fall 2019)
DEADLOCK DETECTION
30
T1 T2
T3
BEGINS-LOCK(A)
S-LOCK(B)
BEGIN
X-LOCK(B)
X-LOCK(C)
BEGIN
S-LOCK(C)
X-LOCK(A)
TIM
ESchedule
T1 T2 T3
Waits-For Graph
![Page 53: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/53.jpg)
CMU 15-445/645 (Fall 2019)
DEADLOCK DETECTION
30
T1 T2
T3
BEGINS-LOCK(A)
S-LOCK(B)
BEGIN
X-LOCK(B)
X-LOCK(C)
BEGIN
S-LOCK(C)
X-LOCK(A)
TIM
ESchedule
T1 T2 T3
Waits-For Graph
![Page 54: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/54.jpg)
CMU 15-445/645 (Fall 2019)
DEADLOCK DETECTION
30
T1 T2
T3
BEGINS-LOCK(A)
S-LOCK(B)
BEGIN
X-LOCK(B)
X-LOCK(C)
BEGIN
S-LOCK(C)
X-LOCK(A)
TIM
ESchedule
T1 T2 T3
Waits-For Graph
![Page 55: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/55.jpg)
CMU 15-445/645 (Fall 2019)
DEADLOCK HANDLING
When the DBMS detects a deadlock, it will select a "victim" txn to rollback to break the cycle.
The victim txn will either restart or abort(more common) depending on how it was invoked.
There is a trade-off between the frequency of checking for deadlocks and how long txns have towait before deadlocks are broken.
31
![Page 56: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/56.jpg)
CMU 15-445/645 (Fall 2019)
DEADLOCK HANDLING: VICTIM SELECTION
Selecting the proper victim depends on a lot of different variables….→ By age (lowest timestamp)→ By progress (least/most queries executed)→ By the # of items already locked→ By the # of txns that we have to rollback with it
We also should consider the # of times a txn has been restarted in the past to prevent starvation.
32
![Page 57: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/57.jpg)
CMU 15-445/645 (Fall 2019)
DEADLOCK HANDLING: ROLLBACK LENGTH
After selecting a victim txn to abort, the DBMS can also decide on how far to rollback the txn'schanges.
Approach #1: Completely
Approach #2: Minimally
33
![Page 58: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/58.jpg)
CMU 15-445/645 (Fall 2019)
DEADLOCK PREVENTION
When a txn tries to acquire a lock that is held by another txn, the DBMS kills one of them to prevent a deadlock.
This approach does not require a waits-for graph or detection algorithm.
34
![Page 59: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/59.jpg)
CMU 15-445/645 (Fall 2019)
DEADLOCK PREVENTION
Assign priorities based on timestamps:→ Older Timestamp = Higher Priority (e.g., T1 > T2)
Wait-Die ("Old Waits for Young")→ If requesting txn has higher priority than holding txn, then
requesting txn waits for holding txn. → Otherwise requesting txn aborts.
Wound-Wait ("Young Waits for Old")→ If requesting txn has higher priority than holding txn, then
holding txn aborts and releases lock.→ Otherwise requesting txn waits.
35
![Page 60: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/60.jpg)
CMU 15-445/645 (Fall 2019)
DEADLOCK PREVENTION
36
BEGIN
X-LOCK(A)⋮
BEGINX-LOCK(A)
⋮
BEGINX-LOCK(A)
⋮ BEGINX-LOCK(A)
⋮
Wait-Die
T1 waits
Wound-Wait
T2 aborts
Wait-Die
T2 aborts
Wound-Wait
T2 waits
T1 T2
T1 T2
![Page 61: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/61.jpg)
CMU 15-445/645 (Fall 2019)
DEADLOCK PREVENTION
Why do these schemes guarantee no deadlocks?
Only one "type" of direction allowed when waiting for a lock.
When a txn restarts, what is its (new) priority?
Its original timestamp. Why?
37
![Page 62: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/62.jpg)
CMU 15-445/645 (Fall 2019)
OBSERVATION
All of these examples have a one-to-one mapping from database objects to locks.
If a txn wants to update one billion tuples, then it has to acquire one billion locks.
38
![Page 63: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/63.jpg)
CMU 15-445/645 (Fall 2019)
LOCK GRANUL ARITIES
When we say that a txn acquires a “lock”, what does that actually mean?→ On an Attribute? Tuple? Page? Table?
Ideally, each txn should obtain fewest number of locks that is needed…
39
![Page 64: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/64.jpg)
CMU 15-445/645 (Fall 2019)
DATABASE LOCK HIERARCHY
40
Database
Table 1 Table 2
Tuple 1
Attr 1
Tuple 2
Attr 2
Tuple n
Attr n
T1
![Page 65: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/65.jpg)
CMU 15-445/645 (Fall 2019)
DATABASE LOCK HIERARCHY
40
Database
Table 1 Table 2
Tuple 1
Attr 1
Tuple 2
Attr 2
Tuple n
Attr n
T1
![Page 66: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/66.jpg)
CMU 15-445/645 (Fall 2019)
EXAMPLE
T1 – Get the balance of Andy’s shady off-shore bank account.
T2 – Increase Matt's bank account balance by 1%.
What locks should these txns obtain?
41
![Page 67: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/67.jpg)
CMU 15-445/645 (Fall 2019)
EXAMPLE
T1 – Get the balance of Andy’s shady off-shore bank account.
T2 – Increase Matt's bank account balance by 1%.
What locks should these txns obtain?
Multiple:→ Exclusive + Shared for leafs of lock tree.→ Special Intention locks for higher levels.
41
![Page 68: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/68.jpg)
CMU 15-445/645 (Fall 2019)
INTENTION LOCKS
An intention lock allows a higher level node to be locked in shared or exclusive mode without having to check all descendent nodes.
If a node is in an intention mode, then explicit locking is being done at a lower level in the tree.
42
![Page 69: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/69.jpg)
CMU 15-445/645 (Fall 2019)
INTENTION LOCKS
Intention-Shared (IS)→ Indicates explicit locking at a lower level with shared
locks.
Intention-Exclusive (IX)→ Indicates locking at lower level with exclusive or shared
locks.
43
![Page 70: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/70.jpg)
CMU 15-445/645 (Fall 2019)
INTENTION LOCKS
Shared+Intention-Exclusive (SIX)→ The subtree rooted by that node is locked explicitly in
shared mode and explicit locking is being done at a lower level with exclusive-mode locks.
44
![Page 71: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/71.jpg)
CMU 15-445/645 (Fall 2019)
COMPATIBILIT Y MATRIX
45
IS IX S SIX X
IS ✔ ✔ ✔ ✔ ×
IX ✔ ✔ × × ×
S ✔ × ✔ × ×
SIX ✔ × × × ×
X × × × × ×
T 1H
old
sT2 Wants
![Page 72: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/72.jpg)
CMU 15-445/645 (Fall 2019)
LOCKING PROTOCOL
Each txn obtains appropriate lock at highest level of the database hierarchy.
To get S or IS lock on a node, the txn must hold at least IS on parent node.
To get X, IX, or SIX on a node, must hold at least IX on parent node.
46
![Page 73: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/73.jpg)
CMU 15-445/645 (Fall 2019)
EXAMPLE TWO-LEVEL HIERARCHY
47
Table R
Tuple 2Tuple 1 Tuple n
T1
R.
![Page 74: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/74.jpg)
CMU 15-445/645 (Fall 2019)
EXAMPLE TWO-LEVEL HIERARCHY
47
Table R
Tuple 2Tuple 1 Tuple n
T1
Read
R.
![Page 75: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/75.jpg)
CMU 15-445/645 (Fall 2019)
EXAMPLE TWO-LEVEL HIERARCHY
47
Table R
Tuple 2Tuple 1 Tuple n
T1
ST1
IST1
Read
R.
![Page 76: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/76.jpg)
CMU 15-445/645 (Fall 2019)
EXAMPLE TWO-LEVEL HIERARCHY
47
Table R
Tuple 2Tuple 1 Tuple n
T1
ST1
IST1
T2
Write
Update Matt's record in R.
![Page 77: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/77.jpg)
CMU 15-445/645 (Fall 2019)
EXAMPLE TWO-LEVEL HIERARCHY
47
Table R
Tuple 2Tuple 1 Tuple n
T1
ST1
IST1
T2
XT2IX
T2
Write
Update Matt's record in R.
![Page 78: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/78.jpg)
CMU 15-445/645 (Fall 2019)
EXAMPLE THREESOME
Assume three txns execute at same time:→ T1 – Scan R and update a few tuples.→ T2 – Read a single tuple in R.→ T3 – Scan all tuples in R.
48
Table R
Tuple 2Tuple 1 Tuple n
![Page 79: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/79.jpg)
CMU 15-445/645 (Fall 2019)
EXAMPLE THREESOME
49
Table R
Tuple 1 Tuple n
T1
Read Read+Write
Tuple 2
Read
Scan R and update a few tuples.
![Page 80: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/80.jpg)
CMU 15-445/645 (Fall 2019)
EXAMPLE THREESOME
49
Table R
Tuple 1 Tuple n
T1
SIXT1
XT1
Tuple 2
Scan R and update a few tuples.
![Page 81: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/81.jpg)
CMU 15-445/645 (Fall 2019)
EXAMPLE THREESOME
49
Table R
Tuple 1 Tuple n
T1
SIXT1
T2
XT1
Read
Tuple 2
Read a single tuple in R.
![Page 82: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/82.jpg)
CMU 15-445/645 (Fall 2019)
EXAMPLE THREESOME
49
Table R
Tuple 1 Tuple n
T1
ST2
SIXT1
T2
XT1IS
T2
Tuple 2
Read a single tuple in R.
![Page 83: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/83.jpg)
CMU 15-445/645 (Fall 2019)
EXAMPLE THREESOME
49
Table R
Tuple 1 Tuple n
T1
ST2
SIXT1
T2
XT1IS
T2
Read
T3
Tuple 2
Read Read
Scan all tuples in R.
![Page 84: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/84.jpg)
CMU 15-445/645 (Fall 2019)
EXAMPLE THREESOME
49
Table R
Tuple 1 Tuple n
T1
ST2
SIXT1
T2
XT1IS
T2
T3
Tuple 2
Scan all tuples in R.
![Page 85: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/85.jpg)
CMU 15-445/645 (Fall 2019)
EXAMPLE THREESOME
49
Table R
Tuple 1 Tuple n
T1
ST2
SIXT1
T2
XT1IS
T2
T3
Tuple 2
ST3
Scan all tuples in R.
![Page 86: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/86.jpg)
CMU 15-445/645 (Fall 2019)
MULTIPLE LOCK GRANUL ARITIES
Hierarchical locks are useful in practice as each txnonly needs a few locks.
Intention locks help improve concurrency:→ Intention-Shared (IS): Intent to get S lock(s) at finer
granularity.→ Intention-Exclusive (IX): Intent to get X lock(s) at finer
granularity.→ Shared+Intention-Exclusive (SIX): Like S and IX at
the same time.
50
![Page 87: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/87.jpg)
CMU 15-445/645 (Fall 2019)
LOCK ESCAL ATION
Lock escalation dynamically asks for coarser-grained locks when too many low level locks acquired.
This reduces the number of requests that the lock manager has to process.
51
![Page 88: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/88.jpg)
CMU 15-445/645 (Fall 2019)
LOCKING IN PRACTICE
You typically don't set locks manually in txns.
Sometimes you will need to provide the DBMS with hints to help it to improve concurrency.
Explicit locks are also useful when doing major changes to the database.
52
![Page 89: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/89.jpg)
CMU 15-445/645 (Fall 2019)
LOCK TABLE
Explicitly locks a table.
Not part of the SQL standard.→ Postgres/DB2/Oracle Modes: SHARE, EXCLUSIVE→ MySQL Modes: READ, WRITE
53
LOCK TABLE <table> IN <mode> MODE;
LOCK TABLE <table> <mode>;
SELECT 1 FROM <table> WITH (TABLOCK, <mode>);
![Page 90: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/90.jpg)
CMU 15-445/645 (Fall 2019)
SELECT. . .FOR UPDATE
Perform a select and then sets an exclusive lock on the matching tuples.
Can also set shared locks:→ Postgres: FOR SHARE→ MySQL: LOCK IN SHARE MODE
54
SELECT * FROM <table>WHERE <qualification> FOR UPDATE;
![Page 91: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/91.jpg)
CMU 15-445/645 (Fall 2019)
CONCLUSION
2PL is used in almost DBMS.
Automatically generates correct interleaving:→ Locks + protocol (2PL, SS2PL ...)→ Deadlock detection + handling→ Deadlock prevention
55
![Page 92: CMU 15-445/645 Database Systems (Fall 2019) :: Two-Phase ... · CONCURRENCY CONTROL PROTOCOL Two-phase locking (2PL) is a concurrency control protocol that determines whether a txn](https://reader030.vdocuments.site/reader030/viewer/2022041120/5f3486d74dbd69286b68ad34/html5/thumbnails/92.jpg)
CMU 15-445/645 (Fall 2019)
NEXT CL ASS
Timestamp Ordering Concurrency Control
56