Transactions
Transactions
Transactions
Chapter 20
Overview
What are transactions? What is a schedule? What is concurrency control? Why we need concurrency control:
Practice:
Recovery facilities
2
What is a Transaction?
Transaction Action, or series of actions, carried out by user or application, which accesses or changes contents of database.
Logical unit of work on the database. Transforms database from one consistent state to another, although consistency may be violated during transaction. Example: Read(staffNo, salary) salary=salary * 1.1 write(staffNo , salary)
3
What is a Transaction?
Can have one of two outcomes: Success - transaction commits and database reaches a new consistent state. Failure - transaction aborts, and database must be restored to consistent state before it started (rolled back or undone). Committed transaction cannot be aborted. Aborted transactions that are rolled back can be restarted later.
Properties of Transactions
Four basic (ACID) properties of a transaction are: Atomicity Consistency All or nothing property. Must transform database from one consistent state to another. Partial effects of incomplete transactions should not be visible to other transactions. Effects of a committed transaction are permanent and must not be lost because of later failure.
Isolation
Durability
Schedule
Start with t0 or t1
Running Transactions
Time
Order of execution
t0 t1
T1
Begin Transaction Read(Balance1)
T2
T3
Balance1
100
Balance2
200 200
Begin Transaction
t2
t3 t4 t5 : : Balance1 += 500 Write(Balance1)
Read(Balance1)
100
100 600
200
200 200 200 :
Commit :
Read(Balance1) :
600 :
Schedule Rules
Never start two transactions at the same time. Never perform Reads and Writes of different transactions at the same time. Each transaction should end with a commit or abort (rollback).
Schedule Definitions
Schedule Sequence of reads/writes by set of concurrent transactions. Serial Schedule Schedule where operations of each transaction are executed consecutively without any interleaved operations from other transactions.
No guarantee that results of all serial executions of a given set of transactions will be identical. (Think of an example)
Non-Serial Schedule Schedule where operations from set of concurrent transactions are interleaved
8
T1
Begin Transaction Read(Balance1) Balance1 += 500 Commit
T2
T1
Begin Transaction
T2
Begin Transaction
10
Prevents interference when two or more users are accessing database simultaneously and at least one is updating data.
12
Successfully completed update is overridden by another user. T1 withdrawing 10 from an account with balx, initially 100. T2 depositing 100 into same account. Serially, final balance would be 190. Loss of T2s update avoided by preventing T1 from reading balx until after update
13
T3
T4
begin-transaction read(balx) balx = balx +100 write(balx)
balx
100 100 100 200
t5
t6 t7 t8
read(balx)
balx = balx -10 write(balx) commit
:
rollback
200
100 190 190
Occurs when one transaction can see intermediate results of another transaction before it has committed. T4 updates balx to 200 but it aborts, so balx should be back at original value of 100. T3 has read new value of balx (200) and uses value as basis of 10 reduction, giving a new balance of 190, instead of 90. Problem avoided by preventing T3 from reading balx until after T4 14 commits or aborts.
Occurs when transaction reads several values but second transaction updates some of them during execution of first.
read.
Problem avoided by preventing T6 from reading balx and balz until after T5 completed updates.
16
Serializability
Main goal is to prevent transactions interfering with each other (3 problems discussed earlier).
17
Serializability
Conflict. View.
Serializability
Theory
Practice
Locking
Timestamping
Optimistic
Conflict Serializability
(a) If two transactions only read a data item, they do not (b) If two transactions either read or write completely
Conflict Serializability
2.
A schedule is conflict serializable if you can switch order of 2 non-conflicting operations until you reach a serial schedule. Precedence graph.
20
t7
t8 t9 t10 t11 t12
read(baly)
write(baly) commit read(baly) write(baly) commit write(baly) commit
write(balx)
21
t7
t8 t9 t10 t11 t12 write(baly) commit
write(balx)
write(baly)
commit
22
t5
t6 t7
write(balx)
commit commit
begin_transaction
write(balx) commit
23
Create:
node for each transaction; a directed edge Ti Tj, if Tj reads the value of an item written by Ti; a directed edge Ti Tj, if Tj writes a value into an item after it has been read by Ti. a directed edge Ti Tj, if Tj writes a value into an item after it has been written by Ti.
t3
t4 t5 t6 t7 t8 t9 t10 t11
t12
t13 t14 t15
25
commit
read(baly) write(baly) commit
View Serializability
For each data item x, if Ti reads initial value of x in S1, Ti must also read initial value of x in S2.
For each data item x, if last write on x performed by Ti in S1, same transaction must perform final write on x in S2.
26
View Serializability
Every conflict serializable schedule is view serializable, although converse is not true.
All Schedules
View Serializable Schedules Conflict Serializable Schedules
It can be shown that any view serializable schedule that is not conflict serializable contains one or more blind writes.
27
t7
t8 t9 t10 t11 t12
begin-transaction
read(balx) write(balx) read(baly) write(baly) commit
read(baly)
write(baly) commit read(baly) write(baly) commit
28
t7
t8 t9 t10
commit
begin_transaction write(balx) commit
29
Recoverable Schedule
A schedule where, for each pair of transactions Ti and Tj, if Tj reads a data item previously written by Ti, then the commit operation of Ti precedes the commit operation of Tj.
30
Serializability
Theory
Practice
Locking
Timestamping
Optimistic
31
Locking, Timestamping.
Optimistic methods assume conflict is rare and only check for conflicts at commit.
32
Locking
Timestamping
Optimistic
Basic Rules
2PL
Deadlock Prevention
Deadlock Detection
Regular Strict 33
Rigorous
Time outs
WaitDie
Wound -Wait
Wait-for Graph
Locking
Main Idea: Transaction uses locks to deny access to other transactions and so prevent incorrect updates.
a shared (read) on x before it can read it. or an exclusive (write) lock on x before it can write it.
Shared Lock:
If transaction has shared lock on item, it can read but not update item. More than one transaction can hold a shared lock on an item.
Exclusive Lock:
If transaction has exclusive lock on item, can both read and update item. Only one transaction can hold an exclusive lock on an item.
upgrade read lock to an exclusive lock. downgrade exclusive lock to a shared lock.
35
Locking -- Commands
To release a lock on X:
Unlock(X)
36
Time t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14 t15 t16 t17 t18 t19 t20 t21 t22
T10
begin_transaction write_lock(balx) read(balx) balx = balx * 1.1 write(balx) unlock(balx) write_lock(baly) read(baly) baly = baly * 1.1 write(baly) commit/unlock(baly) write_lock(baly) read(baly) baly = baly - 100 write(baly) commit/unlock(baly)
We just saw that locking alone doesnt always work. Solution: 2PL.
Transaction follows 2PL protocol if all locking operations precede first unlock operation in the transaction.
Growing phase - acquires all locks but cannot release any locks.
Shrinking phase - releases locks but cannot acquire any new locks.
Time t1 t2
T1 begin-transaction
T2 begin-transaction read(balx)
t3
t4 t5 t6
read(balx)
balx = balx -10 write(balx) commit
100
200 90 90
39
t1
t2 t3 t4 t5
begin-transaction
write_lock(balx) read(balx) 100 balx = balx +100 write(balx)
100
100 100 200
t6
t7 t8 t9 t10
WAIT
read(balx) balx = balx -10 write(balx) commit/unlock(balx)
commit/unlock(balx)
200
200 200 190 190
40
Time t1 t2 t3 t4 t5 t6
T3
write(balx) : rollback
t7
t8
write(balx)
commit
190
190
41
t1
t2 t3 t4 t5 begin_transaction write_lock(balx)
begin-transaction
write_lock(balx) read(balx) 100 balx = balx +100 write(balx)
100
100 100 200
t6
t7 t8 t9 t10
WAIT
read(balx) balx = balx -10 write(balx) commit/unlock(balx)
commit/unlock(balx)
200
200 200 190 190
42
43
44
45
Cascading Rollbacks
If every transaction in a schedule follows 2PL, schedule is serializable. However, problems can occur with interpretation of when locks can be released.
Cascading rollback is undesirable since they potentially lead to the undoing of a significant amount of work
Rigorous 2PL: Leave release of all locks until end of transaction. Strict 2PL: Holds only exclusive locks until the end of the transaction.
BOTH are still 2PL. So both still have growing and shrinking phases.
Cascading Rollbacks:
Solved with strict or rigorous 2PL.
2.
Dead Locks:
Happen in regular 2PL, and also in strict and rigorous 2PL. Handled using deadlock detection and prevention techniques.
47
Deadlocks
Deadlock: An impasse that may result when two (or more) transactions are each waiting for locks held by the other to be released.
48
Example Deadlock
Time t1 T9 begin-transaction T10 begin-transaction write_lock(baly) read(baly) baly = baly + 100 write(baly) wait_lock(balx) WAIT WAIT WAIT
t2
t3 t4 t5
write_lock(balx)
read(balx) balx = balx - 10 write(balx)
t6
t7 t8 t9 t10 t11
49
write_lock(baly)
WAIT WAIT WAIT WAIT
:
Deadlock Handling
Deadlock detection and recovery: DBMS allows deadlocks to happens but detects and recovers from them.
50
Timeouts
If lock has not been granted within this period, lock request times out.
DBMS assumes transaction deadlocked, even though it may not be, and it aborts and automatically restarts the transaction.
51
Timestamps
Before we discuss Wait-die and Wound-wait techniques, introduce timestamps. A timestamp is a unique number given to each transaction. Traditionally, it is the time the transaction started.
52
Timestamps
Time T11 T12 read(balx) begin_transaction write(balx) commit write(balx) commit begin_transaction write(balx) T13
t1
t2 t3 t4 t5 t6 t7 t8 t9
begin-transaction
t10
commit
Wait-Die Technique
Only an older transaction can wait for younger one, otherwise transaction is aborted (dies) and restarted with same timestamp. (Why the same?) If a transaction Ti requests a lock on an item held by Tj:
If Ti > Tj [TS(Ti) < TS(Tj)], Ti waits for Tj to release the lock. If Ti < Tj [TS(Ti) > TS(Tj)], Ti is aborted and restarted with the same TS.
54
Wound-Wait Technique
only a younger transaction can wait for an older one. If older transaction requests lock held by younger one, younger one is aborted (wounded) and restarted with same timestamp. (Why the same?) If a transaction Ti requests a lock on an item held by Tj:
If Ti > Tj [TS(Ti) < TS(Tj)], Tj is aborted and Ti gets the lock. If Ti < Tj [TS(Ti) > TS(Tj)], Ti waits for Tj to release the lock.
55
Create a node for each transaction. Create edge Ti Tj, if Ti waiting to lock item locked by Tj.
56
t3
t4 t5 t6 t7 t8 t9 t10 t11
57
read(balx)
balx = balx - 10 write(balx) write_lock(baly) WAIT WAIT WAIT WAIT
:
T9
T10
WAIT
WAIT WAIT :
WFG is created at regular intervals. Several issues when recovering from a deadlock:
58
Locking
Timestamping
Optimistic
Basic Rules
2PL
Deadlock Prevention
Deadlock Detection
Regular Strict 59
Rigorous
Time outs
WaitDie
Wound -Wait
Wait-for Graph
Timestamping
Main Idea: Transactions ordered globally so that older transactions (smaller timestamps) get priority in the event of conflict.
Timestamping
2 Techniques:
1.
2.
61
Read/write proceeds only if last update on that data item was carried out by an older transaction.
Main Goal: Ordering writes then reads/writes as they would have been ordered in a serial schedule.
62
x already updated by younger (later) transaction. Transaction T must be aborted and restarted with a new
timestamp.
TS(T) write_timestamp(x)
63
x already written by younger transaction. Transaction T must be aborted and restarted with a new timestamp.
65
Provide greater concurrency by rejecting obsolete write operations. When a read(x) is encountered, behave just like in slide 62.
x already written by younger transaction. Ignores the write operation (ignore obsolete write rule)
Comparison of Methods
All Schedules
View Serializable Schedules
TS
67
Main Idea: Versioning of data can be used to increase concurrency so create multiple versions of each data item. Basic timestamp assumes only one version of data item exists, and so only one transaction can access data item at a time. Multiversion allows multiple transactions to read and write different versions of same data item. Multiversion ensures each transaction sees consistent set of versions for all data items it accesses. In multiversion: Each write operation creates new version of data item while retaining old version. When transaction attempts to read data item, system selects one version that ensures serializability NO ABORTS ON READs Each version has a read and a write timestamp. Versions can be deleted once they are no longer required.
68
When a transaction T wishes to read x, we find the correct version and let it read it. The correct version, xi, is the latest version written by an older transaction:
TS(T) write_timestamp(xi)
When a transaction T wishes to write x, we need to perform a test first then write a new version. Test: we need make sure that there is no older version of x that has been read by a transaction younger than T The transaction that is younger than T should read Ts version, not this older version.
70
Find the correct version, xi: is the latest version written by an older transaction:
TS(T) write_timestamp(xi)
2.
Test it: make sure that no younger transaction has already read xi: TS(T) < read_timestamp(xi)?
71
Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
72
T1
T2
T4
T5 Begin
Read(y)
Read(y) Begin Read(y) y=y/2 Write(y)
y=y+x-100 Write(y)
x=x+0.10x Write(x) Commit Commit
Commit
Commit Commit
Locking
Timestamping
Optimistic
Basic Rules
2PL
Deadlock Prevention
Deadlock Detection
Regular Strict 73
Rigorous
Time outs
WaitDie
Wound -Wait
Wait-for Graph
Optimistic Techniques
Main Idea: conflict is rare and it is more efficient to let transactions proceed without delays to ensure serializability.
At commit, check is made to determine whether conflict has occurred. If there is a conflict, transaction must be rolled back and restarted.
Potentially allows greater concurrency than traditional protocols. Three phases: Read. Validation. Write.
74
Transaction reads values from database and stores them in local variables. Updates are applied to a local copy of the data.
Follows the read phase just before the transaction commits. For read-only transaction:
76
Follows successful validation phase for update transactions. Updates made to local copy are applied to the database.
77
Database Recovery
Process of restoring database to a correct state in the event of a failure. Transactions represent basic unit of recovery. Recovery manager responsible for atomicity and durability.
If failure occurs between commit and database buffers being flushed to secondary storage then, to ensure durability, recovery manager has to redo (rollforward) transactions updates. If transaction had not committed at failure time, recovery manager has to undo (rollback) any effects of that transaction for atomicity.
Partial undo - only one transaction has to be undone. Global undo - all transactions have to be undone.
78
Example
T1 T2
T3
T4 T5 T6 t0
tc
tf
DBMS starts at time t0, but fails at time tf. Assume data for transactions T2 and T3 have been written to secondary storage.
Recovery Facilities
1.
2.
3.
4.
80
Transaction records.
Checkpoint records.
81
3. Checkpoint Facility
Checkpoint Point of synchronization between database and log file. All buffers are written to secondary storage.
1. 2. 3. 4.
A checkpoint consists of the following actions: Suspend execution of transactions temporarily. Write all updated buffers to disk. Write a checkpoint log record to disk. Resume executing transactions.
Redo all transactions that committed since the checkpoint. Undo all transactions active at time of crash (if using immediate update).
82
Example
T1 T2 T3 T4 T5 T6 t0 tc tf
84
Shadow Paging.
85
Deferred Update
Updates are not written to the database until after a transaction has reached its commit point. Start from the last checkpoint:
If a transaction has committed before checkpoint Do nothing. If a transaction has committed after checkpoint Redo it. If a transaction has not committed after checkpoint Do nothing.
86
Immediate Update
Updates are applied to database as they occur. Start from the last checkpoint:
If a transaction has committed before checkpoint Do nothing. If a transaction has committed after checkpoint Redo it. If a transaction has not committed after checkpoint Undo it.
Undo is done in reverse order: from bottom of log file to top. Redo is done in order: from top of log file to bottom.
87
Example
Shadow Paging
When transaction starts, two tables are the same. Shadow page table is never changed thereafter and is used to restore database in event of failure. During transaction, current page table records all updates to database. When transaction completes, current page table becomes shadow page table.
89