Transactions &�Concurrency Control 1
R&G 16/17
There are three side effects of acid. Enhanced long term memory, decreased short term memory, and I forget the third.
- Timothy Leary
Architecture of a DBMS
You are here
Completed
Database Management
System
Database
Query Parsing�& Optimization
SQL Client
Relational Operators
Files and Index Management
Buffer Management
Disk Space Management
Completed
Completed
Completed
Completed
Completed
Completed
Architecture of a DBMS, Part 2
Database
Database Management
System
Query Parsing�& Optimization
SQL Client
Relational Operators
Files and Index Management
Buffer Management
Disk Space Management
Database Management
System
Query Parsing�& Optimization
SQL Client
Relational Operators
Files and Index Management
Buffer Management
Disk Space Management
Database Management
System
Query Parsing�& Optimization
SQL Client
Relational Operators
Files and Index Management
Buffer Management
Disk Space Management
You are here
Transaction Manager
Architecture of a DBMS, Part 3
Database
Database Management
System
You are here
Logging & Recovery
Lock Manager
Database Management
System
Query Parsing�& Optimization
SQL Client
Relational Operators
Files and Index Management
Buffer Management
Disk Space Management
Database Management
System
Query Parsing�& Optimization
SQL Client
Relational Operators
Files and Index Management
Buffer Management
Disk Space Management
Database Management
System
Query Parsing�& Optimization
SQL Client
Relational Operators
Files and Index Management
Buffer Management
Disk Space Management
Applications on DBMS
Applications Want Something from the DBMS
Database Management
System
SQL
SQL
SQL
SQL
SQL
SQL
SQL
Concurrency Control & Recovery
Concurrent Execution: Why bother?
Motivating Example
UPDATE Budget
SET money = money – 500
WHERE pid = 1
UPDATE Budget
SET money = money + 200
WHERE pid = 2
UPDATE Budget
SET money = money + 300
WHERE pid = 3
SELECT sum(money)
FROM Budget
Two Issues:
Different Types of Problems
INSERT INTO DollarProducts(name, price)
SELECT pname, price
FROM Product
WHERE price <= 0.99
DELETE Product
WHERE price <= 0.99
SELECT count(*)
FROM Product
SELECT count(*)
FROM DollarProducts
User 1
User 2
What could go wrong?
Inconsistent Reads
Different Types of Problems, Part 2
UPDATE Product
SET Price = Price – 10.99
WHERE pname = “CoolToy”
Lost Update
UPDATE Product
SET Price = Price*0.6
WHERE pname = “CoolToy”
User 2
User 1
What could go wrong?
Different Types of Problems, Part 3
UPDATE Account
SET amount = 1000000
WHERE number = “my-account”
Dirty Reads
Aborted by the system
SELECT amount
FROM Account
WHERE number = “my-account”
User 2
User 1
What could go wrong?
TRANSACTIONS
Transaction: Concept and Implementation
What is a Transaction?
Our Transaction Model
assumption, but we’ll stick with it here)
Transaction Example
Not seen by the DBMS transaction manager!
ACID: High-Level Properties of Transactions
Note: This is a mnemonic, not a formalism. We’ll do some formalisms shortly.
Isolation (Concurrency)
Isolation: An Example
T1
T2
Atomicity and Durability
to disk when system crashes
Atomicity and Durability, cont.
Transaction Consistency
DBMS
(state1)
Transaction
DBMS
(state2)
Consistent States
(ICs all satisfied)
Summary
CONCURRENCY CONTROL
Concurrency Control: Providing Isolation
Transaction Schedules
A schedule is a sequence of actions on data from one or more transactions. ��Actions: Begin, Read, Write, Commit and Abort.
�R1(A) W1(A) R1(B) W1(B) R2(A) W2(A) R2(B) W2(B)
By convention we only include committed transactions, and omit �Begin and Commit.
T1 | T2 |
begin | |
read(A) | |
write(A) | |
read(B) | |
write(B) | |
commit | |
| begin |
| read(A) |
| write(A) |
| read(B) |
| write(B) |
| commit |
Serial Equivalence
Serializability
=?
=?
S
Schedule 1
T1: Transfer $100 from A to B | T2: Add 10% interest to A & B |
begin | |
read(A) | |
A = A - 100 | |
write(A) | |
read(B) | |
B = B + 100 | |
write(B) | |
commit | |
| begin |
| read(A) |
| A = A * 1.1 |
| write(A) |
| read(B) |
| B = B * 1.1 |
| write(B) |
| commit |
Schedule 2
T1: Transfer $100 from A to B | T2: Add 10% interest to A & B |
| begin |
| read(A) |
| A = A * 1.1 |
| write(A) |
| read(B) |
| B = B * 1.1 |
| write(B) |
| commit |
begin | |
read(A) | |
A = A - 100 | |
write(A) | |
read(B) | |
B = B + 100 | |
write(B) | |
commit | |
Schedule 3
Thanks to @jmpatel
T1: Transfer $100 from A to B | T2: Add 10% interest to A & B |
begin | |
read(A) | |
A = A - 100 | |
write(A) | |
| begin |
| read(A) |
| A = A * 1.1 |
| write(A) |
read(B) | |
B = B + 100 | |
write(B) | |
commit | |
| read(B) |
| B = B * 1.1 |
| write(B) |
| commit |
Conflicting Operations
Conflict Serializable Schedules
Note: some serializable schedules are NOT conflict serializable
Conflict Serializability - Intuition
R(A)
R(B)
W(A)
W(B)
R(A)
W(A)
R(B)
W(B)
R(A)
W(A)
W(B)
R(A)
W(A)
W(B)
Conflict Serializability – Intuition, Part 2
R(A)
R(B)
W(A)
W(B)
R(A)
W(A)
R(B)
W(B)
R(A)
R(B)
W(A)
W(B)
R(A)
W(A)
R(B)
W(B)
R(A)
W(A)
W(B)
R(A)
W(A)
W(B)
Conflict Serializability – Intuition, Part 3
R(A)
R(B)
W(A)
W(B)
R(A)
W(A)
R(B)
W(B)
R(A)
R(
W(A)
W(B)
R(A)
W(A)
W(B)
R(A)
R(B)
W(A)
W(B)
R(A)
W(A)
R(B)
W(B)
Conflict Serializability – Intuition, Part 4
R(A)
R(B)
W(A)
W(B)
R(A)
W(A)
R(B)
W(B)
R(A)
R(B)
W(A)
W(B)
R(A)
R(B)
W(B)
R(A)
W(A)
W(B)
R(A)
W(A)
W(B)
Conflict Serializability – Intuition, Part 5
R(A)
R(B)
W(A)
W(B)
R(A)
W(A)
R(B)
W(B)
R(A)
W(A)
W(B)
R(A)
W(A)
W(B)
R(A)
R(B)
W(A)
W(B)
R(A)
R(B)
W(B)
Conflict Serializability – Intuition, cont
R(A)
R(B)
W(A)
W(B)
R(A)
W(A)
R(B)
W(B)
R(A)
R(
W(A)
W(B)
R(A)
W(A)
W(B)
R(A)
R(B)
W(A)
W(B)
R(A)
W(A)
R(B)
W(B)
Conflict Serializability (Continued)
R(A)
W(A)
R(A)
W(A)
NOT!
Conflict Dependency Graph
dependency graph is acyclic.
Proof Sketch: Conflicting operations prevent us� from “swapping” operations into a
serial schedule
Ti
Tj
Example
T1
T2
Dependency graph
T1: R(A), W(A)
Example, pt 2
T1: R(A), W(A),
T2: R(A)
T1
T2
Dependency graph
Example, pt 3
T1: R(A), W(A),
T2: R(A), W(A), R(B), W(B)
T1
T2
Dependency graph
A
Example, pt 4
T1: R(A), W(A), R(B)
T2: R(A), W(A), R(B), W(B)
B
T1
T2
Dependency graph
A
View Serializability
view
T1: R(A) W(A)
T2: W(A)
T3: W(A)
T1: R(A) W(A)
T2: W(A)
T3: W(A)
Notes on Serializability Definitions
TWO PHASE LOCKING
Last lecture
Two Phase Locking (2PL)
Two Phase Locking (2PL), Part 2
| S | X |
S | √ | – |
X | – | – |
Lock
Compatibility
Matrix
Two Phase Locking (2PL), Part 3
time
# locks held
release phase
acquisition phase
Why 2PL guarantees conflict serializability
Strict Two Phase Locking (2PL)
T1: R(A), W(A) Abort
T2: R(A), W(A)
Strict Two Phase Locking