ida / adit lecture 9: transactions and concurrency control jose m. peña [email protected]
TRANSCRIPT
IDA / ADIT 2
How can several users access
and update the database at the
same time ?
Real world
Model
Databasesystem
Physical database
Databasemanagementsystem
Processing of Queries/updates
Access to stored data
IDA / ADIT 3
Concurrent processing
• Single user system: At most one user can use the system at each point in time.
• Multiple user system: Several users can use the system at the same time.o Multiple CPU: Parallel processing.o One CPU: Concurrent processing, interleaving.
• Hereinafter, we focus on multiple user systems with just one CPU.
IDA / ADIT 4
Transactions: Definition
• A transaction is a logical unit of database processing and consists of one or several operations.
• Simplified database operations in a transaction: o Read-item(X)o Write-item(X)
IDA / ADIT 5
Transactions: Example
T1 T2
Read-item(my-account)
my-account := my-account - 2000
Write-item(my-account)
Read-item(other-account)
other-account := other-account + 2000
Write-item(other-account)
Read-item(my-account)
my-account := my-account +1000
Write-item(my-account)
IDA / ADIT 6
How to execute Read-item(X)
• Locate the block on disk that contains X.• Copy the block to primary memory (a buffer).• Copy X from the buffer to the program variable X.
IDA / ADIT 7
How to execute Write-item(X)
1. Locate the block on disk that contains X.
2. Copy the block to primary memory (a buffer).
3. Copy the value of the program variable X to the right place in the buffer.
4. Store the modified block on disk.
IDA / ADIT 8
Schedule: Definition
• A schedule defines the order in which the operations in a set of transactions are to be executed.
• Is any order equally good ?
IDA / ADIT 9
Schedule: Example
T1 T2
Read-item(my-account)
my-account := my-account - 2000
Write-item(my-account)
Read-item(other-account)
Other-account := other-account + 2000
Write-item(other-account)
Read-item(my-account)
my-account := my-account +1000
Write-item(my-account)
TIME
IDA / ADIT 10
Lost update problem
T1 T2
Read-item(my-account)
my-account := my-account- 2000
Write-item(my-account)
Read-item(other-account)
Other-account := other-account + 2000
Write-item(other-account)
Read-item(my-account)
my-account := my-account +1000
Write-item(my-account)
TIME
IDA / ADIT 11
Dirty read problem
T1 T2
Read-item(my-account)
my-account := my-account - 2000
Write-item(my-account)
Read-item(other-account)
FAIL
Read-item(my-account)
my-account := my-account +1000
Write-item(my-account)
TIME
IDA / ADIT 12
Incorrect summary problem
T1 T2
Read-item(my-account1)
my-account1 := my-account1 - 2000
Write-item(my-account1)Read-item(my-account1)
sum := sum + my-account1
TIME
Read-item(my-account2)
my-account2 := my-account2 + 2000
Write-item(my-account2)
Read-item(my-account2)
sum := sum + my-account2
sum := 0
IDA / ADIT 13
Unrepeatable read problem
T1 T2
Read-item(my-account)
Read-item(my-account)
my-account:= my-account + 1000
TIME
Read-item(my-account)
Write-item(my-account)
IDA / ADIT 14
Properties for transactions
ACID: Atomicity, Consistency preservation, Isolation, Durability
• A: A transaction is an atomic unit: It is either executed completely or not at all.
• C: A database that is in a consistent state before the execution of a transaction (i.e. it fulfills the conditions in the relational model and any other condition declared for the database), is also in a consistent state after the execution of the transaction.
• I: A transaction should act as if it is executed isolated from the other transactions.
• D: Changes in the database made by a committed transaction are permanent.
IDA / ADIT 15
Properties for transactions
• Who ensures that the ACID property are satisfied ? o Atomicity: Recovery system.o Consistency preservation: Programmer + DBMS.o Isolation: Concurrency contol. o Durability: Recovery system.
IDA / ADIT 16
Serial schedule: Definition
• A schedule S is serial if the operations in any transaction T are executed directly after each other.
• A serial schedule satisfies the isolation property but it may highly inefficient, i.e. it may not maximize the CPU usage.
IDA / ADIT 17
Serial schedule: Example
T1 T2
Read-item(my-account)
my-account := my-account - 2000
Write-item(my-account)
Read-item(other-account)
other-account := other-account + 2000
Write-item(other-account)
Read-item(my-account)
my-account := my-account +1000
Write-item(my-account)
TIME
IDA / ADIT 18
Serial schedule: Example
T1 T2
Read-item(my-account)
my-account := my-account - 2000
Write-item(my-account)
Read-item(other-account)
other-account := other-account + 2000
Write-item(other-account)
Read-item(my-account)
my-account := my-account +1000
Write-item(my-account)
TIME
IDA / ADIT 19
Non-serial schedule: Example
T1 T2
Read-item(my-account)
my-account := my-account - 2000
Write-item(my-account)
other-account := other-account + 2000
Read-item(other-account)
Write-item(other-account)
Read-item(my-account)
my-account := my-account +1000
Write-item(my-account)
TIME
IDA / ADIT 20
Non-serial schedule: Example
T1 T2
Read-item(my-account)
My-account := my-account - 2000
Write-item(my-account)
Read-item(other-account)
Other-account := other-account + 2000
Write-item(other-account)
Read-item(my-account)
My-account := my-account +1000
Write-item(my-account)
TIME
IDA / ADIT 21
• A serial schedule satisfies the isolation property but it may highly inefficient, i.e. it may not maximize the CPU usage.
• Solution: Find a non-serial schedule that is equivalent to a serial schedule.
• Equivalent = They put the database in the same final state for any initial state of the database.
• Equivalent = Conflict equivalent, i.e. they execute the conflicting operations in the same order, e.g. Read-item(X) after Write-item(X), etc.
• A schedule is serializable if there is a conflict equivalent serial schedule.
Serializable schedule: Definition
IDA / ADIT 22
Serializable schedule: Example
T1 T2
Read-item(my-account)
my-account := my-account - 2000
Write-item(my-account)
other-account := other-account + 2000
Read-item(other-account)
Write-item(other-account)
Read-item(my-account)
my-account := my-account +1000
Write-item(my-account)
TIME
IDA / ADIT 23
Serializable schedule: Example
T1 T2
Read-item(my-account)
my-account := my-account - 2000
Write-item(my-account)
other-account := other-account + 2000
Read-item(other-account)
Write-item(other-account)
Read-item(my-account)
my-account := my-account +1000
Write-item(my-account)
TIME
No conflict on the resource
IDA / ADIT 24
Serializable schedule: Example
T1 T2
Read-item(my-account)
my-account := my-account - 2000
Write-item(my-account)
other-account := other-account + 2000
Read-item(other-account)
Write-item(other-account)
Read-item(my-account)
my-account := my-account +1000
Write-item(my-account)
TIME
No conflict on the resource
IDA / ADIT 25
Non-serializable schedule: Example
T1 T2
Read-item(my-account)
My-account := my-account - 2000
Write-item(my-account)
Read-item(other-account)
Other-account := other-account + 2000
Write-item(other-account)
Read-item(my-account)
My-account := my-account +1000
Write-item(my-account)
TIME
Conflict!
Conflict!
IDA / ADIT 26
Conflict: Definition
• Two operations are in conflict ifo they belong to different transactions,o they access (read/write) the same data item X, ando one of the operations is a Write-item(X).
IDA / ADIT 27
Conflict equivalence: Definition
• A schedule is serializable if there is a conflict equivalent serial schedule.
• Two schedules and are conflict equivalent if the order of any two conflicting operations is the same in both schedules.
• In a (conflict) serializable schedule it is possible to reorder the operations and get a serial schedule.
IDA / ADIT 28
Serializable schedule: Example
T1 T2
Read-item(my-account)
my-account := my-account - 2000
Write-item(my-account)
other-account := other-account + 2000
Read-item(other-account)
Write-item(other-account)
Read-item(my-account)
my-account := my-account +1000
Write-item(my-account)
TIME
IDA / ADIT 29
Non-serializable schedule: Example
T1 T2
Read-item(my-account)
My-account := my-account - 2000
Write-item(my-account)
Read-item(other-account)
Other-account := other-account + 2000
Write-item(other-account)
Read-item(my-account)
My-account := my-account +1000
Write-item(my-account)
TIME
IDA / ADIT 30
Checking if a schedule is serializable
• Create the following directed graph:1. Create a node for each transaction in the schedule.
2. If transation Tj executes a Read-item(X) after transaction Ti executes a Write-item(X), then create an arch Ti Tj .
3. If transation Tj executes a Write-item(X) after transaction Ti executes a Read-item(X), then create an arch Ti Tj .
4. If transation Tj executes a Write-item(X) after transaction Ti executes a Write-item(X), then create an arch Ti Tj .
• The schedule is serializable if and only if the graph above does not contain any directed cycle.
IDA / ADIT 31
Serializable schedule: Example
T1 T2
Read-item(my-account)
my-account := my-account - 2000
Write-item(my-account)
other-account := other-account + 2000
Read-item(other-account)
Write-item(other-account)
Read-item(my-account)
my-account := my-account +1000
Write-item(my-account)
TIME
IDA / ADIT 32
Non-serializable schedule: Example
T1 T2
Read-item(my-account)
My-account := my-account - 2000
Write-item(my-account)
Read-item(other-account)
Other-account := other-account + 2000
Write-item(other-account)
Read-item(my-account)
My-account := my-account +1000
Write-item(my-account)
TIME
IDA / ADIT 33
(Non-)Serializable schedule: Example
T1
R(A)
T2
W(C)
T3
W(A)
T4
R(A)
W(B)
R(C)W(A)
W(D)
IDA / ADIT 34
(Non-)Serializable schedule: Example
T1
R(A)
T2 T3
W(A)
T4
R(A)
W(A)
IDA / ADIT 35
• Can we make sure that we only get serializable schedules? Yes.
Key question
IDA / ADIT 36
Locks: Definition
• Locks control access to data.• Every data item X has a lock in one of three possible states:
o Read-locked (shared).o Write-locked (exclusive).o Unlocked.
• Read-lock(X):o If X is unlocked or read-locked, then it read-locks it, else it waits until
it becomes unlocked or read-locked.
• Write-lock(X):o If X is unlocked, then it write-locks it, else it waits until X becomes
unlocked.
• Unlock(X):o If X is write-locked, then it unlocks X.o If X is read-locked only once, then it unlocks X.
IDA / ADIT 37
Locks: Usage
1. A transaction T must issue a Read-lock(X) or Write-lock(X) before executing a Read-item(X).
2. A transaction T must issue a Write-lock(X) before executing a Write-item(X).
3. A transaction T must issue a Unlock(X) after all the Read-item(X) and Write-item(X) in T have been executed.
4. A transaction T must not issue a Read-lock(X) or Write-lock(X) if it already has a read or write lock on X.
IDA / ADIT 38
Two-phase locking protocol: Definition
• A transaction follows the two-phase locking (2PL) protocol if all Read-lock() and Write-lock() operations come before the first Unlock() operation in the transaction.
• A transaction that follows the 2PL protocol has an expansion phase and a shrinking phase.
• If all the transactions in a schedule follow the 2PL protocol, then the schedule is serializable.
IDA / ADIT 39
Two-phase locking protocol: Example
T1
Read-item(my-account1)
Write-lock(my-account2)
Unlock(my-account1)
Read-item(my-account2)
my-account2 := my-account2 + 2000
Write-item(my-account2)
Read-lock(my-account1)
Unlock(my-account2)
T2
Read-item(my-account1)
Write-lock(my-account2)
Unlock(my-account1)
Read-item(my-account2)
my-account2 := my-account2 + 2000
Write-item(my-account2)
Read-lock(my-account1)
Unlock(my-account2)
IDA / ADIT 40
Two-phase locking protocol: Example
T1
Read-item(my-account1)
Write-lock(my-account2)
Unlock(my-account1)
Read-item(my-account2)
my-account2 := my-account2 + 2000
Write-item(my-account2)
Read-lock(my-account1)
Unlock(my-account2)
T2
Read-item(my-account1)
Write-lock(my-account2)
Unlock(my-account1)
Read-item(my-account2)
my-account2 := my-account2 + 2000
Write-item(my-account2)
Read-lock(my-account1)
Unlock(my-account2)
IDA / ADIT 41
Two-phase locking protocol: Example
T1 T2
Read-item(my-account)
my-account := my-account - 2000
Write-item(my-account)
other-account := other-account + 2000
Read-item(other-account)
Write-item(other-account)
Read-item(my-account)
my-account := my-account +1000
Write-item(my-account)
TIME
IDA / ADIT 42
Two-phase locking protocol: ExampleT1 T2
Read-item(my-account)
my-account := my-account - 2000
Write-item(my-account)
other-account := other-account + 2000
Read-item(other-account)
Write-item(other-account)
Read-item(my-account)
my-account := my-account +1000
Write-item(my-account)
TIME
Write-lock(my-account)
Write-lock(my-account)
Write-lock(other-account)???
Write-lock(other-account)???
IDA / ADIT 43
Two-phase locking protocol: ExampleT1 T2
Read-item(my-account)
my-account := my-account - 2000
Write-item(my-account)
other-account := other-account + 2000
Read-item(other-account)
Write-item(other-account)
TIME
Write-lock(my-account)
Read-item(my-account)
my-account := my-account +1000
Write-item(my-account)
Wite-lock(my-account)
Write-lock(other-account)Unlock(my-account)
Unlock(other-account)
Unlock(my-account)
IDA / ADIT 44
Two-phase locking protocol: Example
T1 T2
Read-item(my-account)
My-account := my-account - 2000
Write-item(my-account)
Read-item(other-account)
Other-account := other-account + 2000
Write-item(other-account)
Read-item(my-account)
My-account := my-account +1000
Write-item(my-account)
TIME
IDA / ADIT 45
Two-phase locking protocol: Example
T1
R(A)
T2
W(C)
T3
W(A)
T4
R(A)
W(B)
R(C)W(A)
W(D)
IDA / ADIT 46
Two-phase locking protocol: ExampleT1 T2 T3
read(x)x:=x+1write(x)
read(x)x:=x+1write(x)
read(x)x:=x+1write(x)
read(y)y:=y+1write(y)
read(y)y:=y+1write(y)
IDA / ADIT 47
Two-phase locking protocol: Example
T1 T2 T3
write(x)
write(x)
write(y)
write(y)
IDA / ADIT 48
Deadlock
• Two or more transactions wait for each other to unlock some data item.
• Deadlock prevention:o Conservative 2PL protocol: Wait until you can lock all the data to be
used beforehand, o wait-die, o wound-wait, o no waiting, o cautious waiting, etc.
• Deadlock detection: o Wait-for graph, o timeouts, etc.
IDA / ADIT 49
Deadlock
T1
Write-lock(my-account1)
Write-lock(my-account2)
T2
Write-lock(my-account2)
Write-lock(my-account1)
TIME
IDA / ADIT 50
Starvation
• A transaction is not executed for an indefinite period of time while other transactions are executed normally.
• Starvation prevention:o First-come-first-served waiting scheme, oro wait-die,o wound-wait, etc.