Transactions and TCL
November 2, 2023
Data 101, Fall 2023 @ UC Berkeley
Lisa Yan https://fa23.data101.org/
1
LECTURE 21
Join at slido.com�#transactions
ⓘ
Click Present with Slido or install our Chrome extension to display joining instructions for participants while presenting.
Parallelism is a Big Topic in data systems design!
When updating data, we want correctness as well as speed, particularly when users are accessing and modifying the same database system.
Our goal in this course is twofold:
3
#transactions
Two Main Features We Expect from our Database System
When updating data, we want correctness as well as speed, particularly when users are accessing and modifying the same database system.
Our goal in this course is twofold:
Concurrency Control:
Recovery:
4
We first need to understand the definition of a unit of work that should appear to happen all-at-once: a transaction.
#transactions
Transactions
Transactions
The ACID Principle
Isolation and Serializability
Strict 2-Phase Locking
Determining Serializability:
Formal Terminology: Conflict Serializable
Weak Isolation
Additional Slides
5
Lecture 21, Data 101 Fall 2023
What is a Transaction?
Colloquially, a transaction in a database is a unit of work�that should appear to “happen together.”
Classic example: Debit/Credit transaction�i.e., moving money from�one account (1111) to another (9999).
6
BEGIN
-- "debit" one account
UPDATE checking � SET amount = amount – 1000 � WHERE acctId = 1111;
-- "credit" the other account
UPDATE savings
SET amount = amount + 1000
WHERE acctId = 9999;
COMMIT
SQL commands that need to happen together
#transactions
What is a Transaction?
Colloquially, a transaction in a database is a unit of work�that should appear to happen “together.”
Classic example: Debit/Credit transaction�i.e., moving money from�one account (1111) to another (9999).
7
BEGIN
-- "debit" one account
UPDATE checking � SET amount = amount – 1000 � WHERE acctId = 1111;
-- "credit" the other account
UPDATE savings
SET amount = amount + 1000
WHERE acctId = 9999;
COMMIT
SQL commands that need to happen together
SQL TCL
#transactions
What is a Transaction? Observations
Colloquially, a transaction in a database is a unit of work�that should appear to “happen together.”
Classic example: Debit/Credit transaction�i.e., moving money from�one account (1111) to another (9999).
8
BEGIN
-- "debit" one account
UPDATE checking � SET amount = amount – 1000 � WHERE acctId = 1111;
-- "credit" the other account
UPDATE savings
SET amount = amount + 1000
WHERE acctId = 9999;
COMMIT
A few observations about defining “happen together”:
We’ll come back to this!!
#transactions
SQL TCL
9
Image: source
Data Definition Language
Transaction Control Language
Data Manipulation Language
Data�Query Language
Data�Control�Language
our focus today
✅
✅
✅
BEGIN
TCL is used to define transactions as sets of SQL commands that are meant to “happen together.”
#transactions
SQL TCL, Briefly
A transaction in SQL is a list of commands sandwiched by BEGIN and COMMIT.
SQL syntax can vary slightly across systems, but generally:
10
Most systems autocommit, i.e., if BEGIN/COMMIT is not specified, then the system will auto-wrap each individual SQL command in its own transaction.
BEGIN� <command 1>� …� <command n>�COMMIT
#transactions
Savepoints
A transaction in SQL is a list of commands sandwiched by BEGIN and COMMIT.
SQL syntax can vary slightly across systems, but generally:
11
Savepoints let you break your transactions up into pieces. You can then “partially rollback” to a prior savepoint, or abort altogether.
Use case: beyond the scope of this class, but generally used with SQL conditionals or as part of � database constraints.
BEGIN
UPDATE checking � SET amount = amount – 1000 � WHERE acctId = 1234;
SAVEPOINT debit_done;
UPDATE savings
SET amount = amount + 1000
WHERE acctId = 4322;
SAVEPOINT credit_done;
ROLLBACK TO SAVEPOINT debit_done;
UPDATE savings
SET amount = amount + 1000
WHERE acctId = 4321;
END
destroy
undo
#transactions
The ACID Principle
Transactions
The ACID Principle
Isolation and Serializability
Strict 2-Phase Locking
Determining Serializability:
Formal Terminology: Conflict Serializable
Weak Isolation
Additional Slides
12
Lecture 21, Data 101 Fall 2023
What is a Transaction? Observations
Colloquially, a transaction in a database is a unit of work�that should appear to “happen together.”
Classic example: Debit/Credit transaction�i.e., moving money from�one account (1111) to another (9999).
13
BEGIN
-- "debit" one account
UPDATE checking � SET amount = amount – 1000 � WHERE acctId = 1111;
-- "credit" the other account
UPDATE savings
SET amount = amount + 1000
WHERE acctId = 9999;
COMMIT
A few observations about defining “happen together”:
These four properties, collectively known as ACID, define how transactions can guarantee concurrency control and recovery.
#transactions
ACID: Basic Guarantees
ACID defines four properties of transactions that guarantee concurrency control and recovery.
Atomicity �
Consistency
�
Isolation
�.
Durability
14
#transactions
ACID: Basic Guarantees
ACID defines four properties of transactions that guarantee concurrency control and recovery.
Atomicity Either all the commands are reflected in the database, or none are.� Ex: Both debit and credit should occur, or both should fail to occur.
Consistency
�
Isolation
�.
Durability
15
#transactions
ACID: Basic Guarantees
ACID defines four properties of transactions that guarantee concurrency control and recovery.
Atomicity Either all the commands are reflected in the database, or none are.� Ex: Both debit and credit should occur, or both should fail to occur.
Consistency If COMMIT succeeds, all the database integrity checks hold true.
Isolation
�.
Durability
16
#transactions
ACID: Basic Guarantees
ACID defines four properties of transactions that guarantee concurrency control and recovery.
Atomicity Either all the commands are reflected in the database, or none are.� Ex: Both debit and credit should occur, or both should fail to occur.
Consistency If COMMIT succeeds, all the database integrity checks hold true.
Isolation Concurrent transactions should externally appear to run sequentially.
Durability
17
#transactions
ACID: Basic Guarantees
ACID defines four properties of transactions that guarantee concurrency control and recovery.
Atomicity Either all the commands are reflected in the database, or none are.� Ex: Both debit and credit should occur, or both should fail to occur.
Consistency If COMMIT succeeds, all the database integrity checks hold true.
Isolation Concurrent transactions should externally appear to run sequentially.
Durability If COMMIT succeeds, all changes from the transaction persist,� even if there is a power failure or a reboot, until the transaction� is overwritten by a later transaction.
18
#transactions
What part of the database guarantees ACID for transactions?
Designing these DB components are beyond the scope of this course, but here’s the teaser.
Atomicity Either all the commands are reflected in the database, or none are.� Ex: Both debit and credit should occur, or both should fail to occur.
Consistency If COMMIT succeeds, all the database integrity checks hold true.
Isolation Concurrent transactions should externally appear to run sequentially.
Durability If COMMIT succeeds, all changes from the transaction persist,� even if there is a power failure or a reboot, until the transaction� is overwritten by a later transaction.
19
The database’s internal recovery system.
After a crash:
See CS186 for the implementation…Devil is in the details!
#transactions
What part of the database guarantees ACID for transactions?
Designing these DB components are beyond the scope of this course, but here’s the teaser.
Atomicity Either all the commands are reflected in the database, or none are.� Ex: Both debit and credit should occur, or both should fail to occur.
Consistency If COMMIT succeeds, all the database integrity checks hold true.
Isolation Concurrent transactions should externally appear to run sequentially.
Durability If COMMIT succeeds, all changes from the transaction persist,� even if there is a power failure or a reboot, until the transaction� is overwritten by a later transaction.
20
Standard database checks (relatively efficient to check for core things like attribute types, keys, constraints, etc.)
#transactions
What part of the database guarantees ACID for transactions?
Designing these DB components are beyond the scope of this course, but here’s the teaser.
Atomicity Either all the commands are reflected in the database, or none are.� Ex: Both debit and credit should occur, or both should fail to occur.
Consistency If COMMIT succeeds, all the database integrity checks hold true.
Isolation Concurrent transactions should externally appear to run sequentially.
Durability If COMMIT succeeds, all changes from the transaction persist,� even if there is a power failure or a reboot, until the transaction� is overwritten by a later transaction.
21
Provided by concurrency control, a component of the database. We’ll see this intuition today!!
#transactions
[Exercise] ACID
22
BEGIN
-- "debit" one account
UPDATE checking � SET amount = amount – 1000 � WHERE acctId = 1111;
-- "credit" the other account
UPDATE savings
SET amount = amount + 1000
WHERE acctId = 9999;
COMMIT
Match 1-4 with A, C, I, and D from the ACID Principle.
🤔
A. D, I, C, A
B. I, C, A, D
C. A, C, I, D
D. A, C, D, I
E. Something else
#transactions
Match 1-4 with A, C, I, and D from the ACID Principle.
ⓘ
Click Present with Slido or install our Chrome extension to activate this poll while presenting.
[Solution] ACID
24
BEGIN
-- "debit" one account
UPDATE checking � SET amount = amount – 1000 � WHERE acctId = 1111;
-- "credit" the other account
UPDATE savings
SET amount = amount + 1000
WHERE acctId = 9999;
COMMIT
A. We need both commands (debit and credit) to � happen, i.e., we should not have partial transactions.
C. If the full transaction succeeds, database constraints � should still be satisfied.
I. Even if there is another transaction happening � simultaneously, one should appear to have finished � “first.”
D. The transaction should appear to have happened � even if there is a power failure/reboot later.
#transactions
[History] Why ACID? Unknown, but…
25
Jim Gray, PhD, UC Berkeley
Industry/Academic researcher
1998 Turing Award Winner “For seminal contributions to database and transaction processing research and technical leadership in system implementation.”
1983
#transactions
Isolation and Serializability
Transactions
The ACID Principle
Isolation and Serializability
Strict 2-Phase Locking
Determining Serializability:
Formal Terminology: Conflict Serializable
Weak Isolation
Additional Slides
26
Lecture 21, Data 101 Fall 2023
Isolation, discussed in terms of object read/writes
Isolation: Concurrent transactions should externally appear to run sequentially.
27
Hire Mercy as the new VP of Engineering!
Move the entire payroll of the London Office to the Cambridge Office!
Prepare tax projections for the 2nd quarter!
#transactions
Isolation, discussed in terms of object read/writes
Isolation: Concurrent transactions should externally appear to run sequentially.
The challenge: How do we execute these transactions “in isolation” but “concurrently”? With one single machine?
28
Prepare tax projections for the 2nd quarter!
Hire Mercy as the new VP of Engineering!
Move the entire payroll of the London Office to the Cambridge Office!
The precise order of these three transactions doesn’t matter. What matters is that they appeared to have been executed by the DBMS in some order.
#transactions
Notation
Isolation: Concurrent transactions should externally appear to run sequentially.
The challenge: How do we execute these transactions “in isolation” but “concurrently”? With one single machine?
29
Prepare tax projections for the 2nd quarter!
Hire Mercy as the new VP of Engineering!
Move the entire payroll of the London Office to the Cambridge Office!
For simplicity, we will limit our discussion to reads and writes of individual “objects”:
R1(X1)
W1(X1 = X1-275000)
W1(Y1 = (“VPE”, “Mercy”))
W3(Q3 = …, Q11=..., )
R2(X1-100000)
W2(Z1=…)
#transactions
Determining Transaction Schedules that Maintain Isolation
Our goal: Allow multiple transactions to run concurrently (for performance)�but also in isolation (for ACID).
To do so, we’ll define the following:
30
#transactions
1. Transaction Schedules
A transaction schedule is a ordered list of actions from a set of transactions.
31
#transactions
1. Transaction Schedules
A transaction schedule is a ordered list of actions from a set of transactions.
32
-- set Parth’s salary to 10% more �-- than Jonah’s
UPDATE employee
SET salary = (SELECT salary*1.1
FROM employee
WHERE name='Jonah')
WHERE name = 'Parth';
-- set Gabi’s salary to 10% more �-- than Parth’s
UPDATE employee
SET salary = (SELECT salary*1.1
FROM employee
WHERE name='Parth')
WHERE name = Gabi';
T1:
R1(J) |
W1(P) |
R2(P) |
W2(G) |
T2:
#transactions
1. Transaction Schedules
A transaction schedule is a ordered list of actions from a set of transactions.
33
T1:
R1(J) |
W1(P) |
R2(P) |
W2(G) |
T2:
T1 | T2 |
R1(J) | |
W1(P) | |
| R2(P) |
| W2(G) |
A proposed Transaction Schedule�of T1 and T2
time
In a transaction schedule, we separate the transactions by column.
#transactions
1. Transaction Schedules
A transaction schedule is a ordered list of actions from a set of transactions.
34
T1 | T2 |
R1(J) | |
W1(P) | |
| R2(P) |
| W2(G) |
T1:
R1(J) |
W1(P) |
R2(P) |
W2(G) |
T2:
A proposed Transaction Schedule�of T1 and T2
time
✅
✅
#transactions
2. Serial Schedules
A serial schedule is a transaction schedule for which �actions from different transactions are not interleaved.
35
T1:
R1(J) |
W1(P) |
R2(P) |
W2(G) |
T2:
Serial schedules, as defined, satisfy the isolation property of ACID.
T1 | T2 |
R1(J) | |
W1(P) | |
| R2(P) |
| W2(G) |
T1 | T2 |
| R2(P) |
| W2(G) |
R1(J) | |
W1(P) | |
T1 | T2 |
| R2(P) |
R1(J) | |
| W2(G) |
W1(P) | |
serial schedule
time
serial schedule
not a serial schedule;�transactions interleaved
#transactions
2. Serial Schedules
36
T1 | T2 |
R1(J) | |
W1(P) | |
| R2(P) |
| W2(G) |
T1 | T2 |
| R2(P) |
| W2(G) |
R1(J) | |
W1(P) | |
T1 | T2 |
| R2(P) |
R1(J) | |
| W2(G) |
W1(P) | |
serial schedule
time
serial schedule
not a serial schedule;�transactions interleaved
-- set Parth’s salary to 10% more �-- than Jonah’s
T1:
R1(J) |
W1(P) |
R2(P) |
W2(G) |
T2:
-- set Gabi’s salary to 10% more �-- than Parth’s
#transactions
3a. Equivalency of Database Outcomes
37
-- set Parth’s salary to 10% more �-- than Jonah’s
T1:
R1(J) |
W1(P) |
R2(P) |
W2(G) |
T2:
T1 | T2 |
| R2(P) |
R1(J) | |
| W2(G) |
W1(P) | |
time
Despite the interleaving, this schedule has an equivalent database outcome to one of the serial schedules!
-- set Gabi’s salary to 10% more �-- than Parth’s
T1 | T2 |
| R2(P) |
| W2(G) |
R1(J) | |
W1(P) | |
serial schedule
#transactions
3b. Serializable Schedules
38
-- set Parth’s salary to 10% more �-- than Jonah’s
T1:
R1(J) |
W1(P) |
R2(P) |
W2(G) |
T2:
T1 | T2 |
| R2(P) |
| W2(G) |
R1(J) | |
W1(P) | |
T1 | T2 |
| R2(P) |
R1(J) | |
| W2(G) |
W1(P) | |
time
A serializable schedule is a transaction schedule whose database outcome is equivalent to some serial schedule.
-- set Gabi’s salary to 10% more �-- than Parth’s
serial schedule
#transactions
3c. Unserializable Schedules
39
serial schedule
-- set Parth’s salary to 10% more �-- than Jonah’s
T1:
R1(J) |
W1(P) |
R2(P) |
W2(G) |
T2:
T1 | T2 |
| R2(P) |
| W2(G) |
R1(J) | |
W1(P) | |
T1 | T2 |
| R2(P) |
R1(J) | |
| W2(G) |
W1(P) | |
time
A serializable schedule is a transaction schedule whose database outcome is equivalent to some serial schedule.
-- set Gabi’s salary to 10% more �-- than Parth’s
T1 | T2 |
| R2(P) |
R1(J) | |
W1(P) | |
| W2(G) |
Note that not all schedules are serializable; the above is an unserializable schedule, because there is no serial equivalent, and therefore transactions do not appear isolated.
⚠️
#transactions
Summary so far
Our goal: Allow multiple transactions to run concurrently (for performance)�but also in isolation (for ACID).
To do so, we’ve traced the following steps:
40
T1 | T2 |
| R2(P) |
R1(J) | |
| W2(G) |
W1(P) | |
T1 | T2 |
| R2(P) |
| W2(G) |
R1(J) | |
W1(P) | |
T1 | T2 |
| R2(P) |
R1(J) | |
W1(P) | |
| W2(G) |
serial schedule
serializable schedule
unserializable schedule
#transactions
Summary so far
Our goal: Allow multiple transactions to run concurrently (for performance)�but also in isolation (for ACID).
To do so, we’ve traced the following steps:
41
We want systems that can provide serializability, i.e., guarantee steps 2 or 3 for all executed schedules.
T1 | T2 |
| R2(P) |
R1(J) | |
| W2(G) |
W1(P) | |
T1 | T2 |
| R2(P) |
| W2(G) |
R1(J) | |
W1(P) | |
T1 | T2 |
| R2(P) |
R1(J) | |
W1(P) | |
| W2(G) |
serial schedule
serializable schedule
unserializable schedule
Under the covers, a database system can allow serializable schedules that may not be serial, but have the same outcome as some serial schedule.
❌
#transactions
A Joke
42
#transactions
Strict 2-Phase Locking
Transactions
The ACID Principle
Isolation and Serializability
Strict 2-Phase Locking
Determining Serializability:
Formal Terminology: Conflict Serializable
Weak Isolation
Additional Slides
43
Lecture 21, Data 101 Fall 2023
Database locking
How do databases ensure serializability?
One of the most straightforward implementations is called Strict Two-Phase Locking (Strict 2PL).
44
#transactions
Database locking
How do databases ensure serializability?
One of the most straightforward implementations is called Strict Two-Phase Locking (Strict 2PL).
Locking is the process of ensuring that 2 conflicting actions happen in order.
45
#transactions
Strict Two-Phase Locking (Strict 2PL)
Phase 1: During the transaction, lock objects before use. �Two types of locks:
Phase 2: At the end of the transaction (i.e., � COMMIT or ROLLBACK),� release all locks at once.
46
# locks held
acquisition phase
time
release all locks at end of xact
The Strict 2PL algorithm allows only serializable schedules!
Note that schedules can result in deadlock. See Discussion for more info/practice!
#transactions
Strict Two-Phase Locking (Strict 2PL), Practically
Phase 1: During the transaction, lock objects before use. �Two types of locks:
What objects are we locking?
What does it mean to “acquire” or “release” a lock?
Phase 2: At the end of the transaction (i.e., � COMMIT or ROLLBACK),� release all locks at once.
47
# locks held
acquisition phase
time
release all locks at end of xact
#transactions
Database locking
How do databases ensure serializability?
One of the most straightforward implementations is called Strict Two-Phase Locking (Strict 2PL).
Locking is the process of ensuring that 2 conflicting actions happen in order.
48
#transactions
Determining Serializability:
Conflicting Actions
Transactions
The ACID Principle
Isolation and Serializability
Strict 2-Phase Locking
Determining Serializability:
Formal Terminology: Conflict Serializable
Weak Isolation
Additional Slides
49
Lecture 21, Data 101 Fall 2023
How do we know if a schedule is serializable?
We like serializable schedules.
50
Conflicting actions between transactions will determine if a schedule is serializable.
What does it mean???
Let’s dive in!
#transactions
Conflicting Actions
Def: Two actions conflict if:
Alt Def: If T1 and T2 have conflicting actions, then every equivalent serial schedule �(i.e., with the same database outcome) must have T1 and T2 in some specific order.
51
#transactions
Which of the following are conflicting actions?
Def: Two actions conflict if:
Alt Def: If T1 and T2 have conflicting actions, then every equivalent serial schedule�(i.e., with the same database outcome) must have T1 and T2 in some specific order.
Suppose T1 → T2 in a schedule, i.e., T1 comes before T2.
52
🤔
Select all for which the following is true: Flipping the order of the two actions in T1 and T2 would result in a different database outcome state.
W1(P)
R2(P)
T1 writes �Parth salary �as 110000
T2 reads �Parth salary �as 110000
R1(G)
W2(G)
T1 reads Gabi salary as 100000
T2 writes Gabi salary as 121000
W1(J)
W2(J)
T1 writes Jonah salary as 300000
T2 writes Jonah salary as 0
R1(G)
R2(G)
T1 reads Gabi salary as 121000
T2 reads Gabi salary as 121000
W1(J)
W2(P)
T1 writes Jonah salary as 0
T2 writes Parth salary as 200000
A.
B.
C.
D.
E.
#transactions
Select all for which the following is true: Flipping the order of the two actions in T1 and T2 would result in a different database outcome state.
ⓘ
Click Present with Slido or install our Chrome extension to activate this poll while presenting.
Which of the following are conflicting actions?
Def: Two actions conflict if:
Alt Def: If T1 and T2 have conflicting actions, then every equivalent serial schedule�(i.e., with the same database outcome) must have T1 and T2 in some specific order.
Suppose T1 → T2 in a schedule, i.e., T1 comes before T2.
54
Select all for which the following is true: Flipping the order of the two actions in T1 and T2 would result in a different database outcome state.
W1(P)
R2(P)
T1 writes �Parth salary �as 110000
T2 reads �Parth salary �as 110000
R1(G)
W2(G)
T1 reads Gabi salary as 100000
T2 writes Gabi salary as 121000
W1(J)
W2(J)
T1 writes Jonah salary as 300000
T2 writes Jonah salary as 0
R1(G)
R2(G)
T1 reads Gabi salary as 121000
T2 reads Gabi salary as 121000
W1(J)
W2(P)
T1 writes Jonah salary as 0
T2 writes Parth salary as 200000
A.
B.
C.
D.
E.
#transactions
Which of the following are conflicting actions?
Def: Two actions conflict if:
Alt Def: If T1 and T2 have conflicting actions, then every equivalent serial schedule�(i.e., with the same database outcome) must have T1 and T2 in some specific order.
Suppose T1 → T2 in a schedule, i.e., T1 comes before T2.
55
W1(P)
R2(P)
T1 writes �Parth salary �as 110000
T2 reads �Parth salary �as 110000
R1(G)
W2(G)
T1 reads Gabi salary as 100000
T2 writes Gabi salary as 121000
W1(J)
W2(J)
T1 writes Jonah salary as 300000
T2 writes Jonah salary as 0
R1(G)
R2(G)
T1 reads Gabi salary as 121000
T2 reads Gabi salary as 121000
W1(J)
W2(P)
T1 writes Jonah salary as 0
T2 writes Parth salary as 200000
can be flipped! no conflicts!
cannot be flipped! conflicting actions!
#transactions
How do we know if a schedule is serializable?
We like serializable schedules.
56
Conflicting actions between transactions will determine if a schedule is serializable.
You probably still don’t believe me yet! Let’s keep looking.
#transactions
Which of the following are conflicting actions?
57
W1(P)
R2(P)
T1 writes �Parth salary �as 110000
T2 reads �Parth salary �as 110000
R1(G)
W2(G)
T1 reads Gabi salary as 100000
T2 writes Gabi salary as 121000
W1(J)
W2(J)
T1 writes Jonah salary as 300000
T2 writes Jonah salary as 0
R1(G)
R2(G)
T1 reads Gabi salary as 121000
T2 reads Gabi salary as 121000
W1(J)
W2(P)
T1 writes Jonah salary as 0
T2 writes Parth salary as 200000
A.
B.
C.
D.
E.
R2(P)
W1(P)
T2 reads �Parth salary �as ????
T1 writes�Parth salary �as ????
W2(G)
R1(G)
T2 writes Gabi salary as ????
T1 reads Gabi salary as ????
W2(J)
W1(J)
T2 writes Jonah salary as ????
T1 writes Jonah salary as ????
R2(G)
R1(G)
T2 reads Gabi salary as ????
T1 reads Gabi salary as ????
W2(P)
W1(J)
T2 writes Jonah salary as ????
T1 writes Parth salary as ????
Suppose T1 → T2 in a schedule. For which of the following would the resulting flip of actions mean that this transaction order would change, i.e., that now T2 → T1? Select all.
[for review next time]
#transactions
Determining Serializability: Conflict Graphs
Transactions
The ACID Principle
Isolation and Serializability
Strict 2-Phase Locking
Determining Serializability:
Formal Terminology: Conflict Serializable
Weak Isolation
Additional Slides
58
[for next time]
Lecture 21, Data 101 Fall 2023
Formal Terminology: Conflict Serializable
Transactions
The ACID Principle
Isolation and Serializability
Strict 2-Phase Locking
Determining Serializability:
Formal Terminology: Conflict Serializable
Weak Isolation
Additional Slides
59
[for next time]
Lecture 21, Data 101 Fall 2023
Weak Isolation
Transactions
The ACID Principle
Isolation and Serializability
Strict 2-Phase Locking
Determining Serializability:
Formal Terminology: Conflict Serializable
Weak Isolation
Additional Slides
60
[for next time]
Lecture 21, Data 101 Fall 2023
Additional Slides
61
Lecture 21, Data 101 Fall 2023
Protocol for Writes (Exclusive locks)
62
Item | Lock Mode | Granted | Wait Queue |
O | S | T2, T3 | |
| | | |
| | | |
#transactions
Protocol for Reads (Shared locks)
63
Item | Lock Mode | Granted | Wait Queue |
O | S | T2, T3 | |
| | | |
| | | |
#transactions
Protocol for X Lock Release
64
Item | Lock Mode | Granted | Wait Queue |
O | X | T1 | T4:S, T5:S, T6:X |
| | | |
| | | |
#transactions
You can prove that:
65
#transactions
One remaining issue: Deadlock
T1 acquires X-lock on O
T2 acquires S-lock on P
T1 requests X-lock on P.
T1 is descheduled, waiting for T2.
T2 requests S-lock on O
T2 is descheduled, waiting for T1.
Solution: the system periodically detects deadlock cycles, and rolls back one transaction on the cycle.
66
T1
T2
Waits-for Graph. A slightly different kind of graph: each edge shows when a transaction is waiting for another transaction.
A cycle corresponds to a deadlock. No serializability violations happen, but transactions on the cycle are stuck and objects remain locked.
#transactions