Академический Документы
Профессиональный Документы
Культура Документы
Concurrrency Control
Transaction Support
Transaction: Action, or series of actions, carried out by user or application, which reads or updates contents of database.
Transforms database from one consistent state to another, although consistency may be violated during transaction.
Example Transaction
Transaction Support
Can
Success - transaction commits and database reaches a new consistent state. Failure - transaction aborts, and database must be restored to consistent state before it started. Such a transaction is rolled back or undone.
Committed
transaction cannot be aborted. Aborted transaction that is rolled back can be restarted later.
4
Properties of Transactions
Four
Atomicity All or nothing property. Consistency Must transform database from one consistent state to another. Isolation Partial effects of incomplete transactions should not be visible to other transactions. Durability Effects of a committed transaction are permanent and must not be lost because of later failure.
Concurrency Control
Process of managing simultaneous operations on the database without having them interfere with one another.
Prevents interference when two or more users are accessing database simultaneously and at least one is updating data. Although two transactions may be correct in themselves, interleaving of operations may produce an incorrect result.
examples of potential problems caused by concurrency: Lost update problem. Uncommitted dependency problem. Inconsistent analysis problem.
Example: T1 withdrawing 10 from an account with balx, initially 100. T2 depositing 100 into same account. Serially, final balance would be 190.
T2s
update is lost ! This can be avoided by preventing T1 from reading balx until after update.
10
Example: 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.
11
Problem
Problem avoided by preventing T6 from reading balx and balz until after T5 completed updates.
14
15
Serializability
In serializability, ordering of read/writes is important: (a) If two transactions only read a data item, they do not
conflict and order is not important. (b) If two transactions either read or write completely separate data items, they do not conflict and order is not important. (c) If one transaction writes a data item and another reads or writes same data item, order of execution is important.
16
17
Serializability
Conflict
serializable schedule orders any conflicting operations in same way as some serial execution. Constrained write rule: transaction updates data item based on its old value, which is first read. Under the constrained write rule we can use precedence graph to test for serializability
18
Precedence Graph
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. If precedence graph contains cycle schedule is not conflict serializable.
19
20
View Serializability
Offers less stringent definition of schedule equivalence than conflict serializability. Two schedules S1 and S2 are view equivalent if:
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 read on x by Ti in S1, if value read by x is written by Tj, Ti must also read value of x produced by Tj 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.
21
View Serializability
Schedule
is view serializable if it is view equivalent to a serial schedule. Every conflict serializable schedule is view serializable, although converse is not true.
22
23
Exercise
Consider the following schedule
Recoverability
Serializability
identifies schedules that maintain database consistency, assuming no transaction fails. 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.
25
Exercise 2
Consider the following schedule
27
Locking
Most widely used approach to ensure serializability. Transaction uses locks to deny access to other transactions and so prevent incorrect updates. Generally, a transaction must claim a shared (read), or exclusive (write) lock on a data item before read or write.
28
If transaction has shared lock on item, can read but not update item. If transaction has exclusive lock on item, can both read and update item. Reads cannot conflict, so more than one transaction can hold shared locks simultaneously on same item. Exclusive lock gives transaction exclusive access to that item. Some systems allow transaction to upgrade read lock to an exclusive lock, or downgrade exclusive lock to a shared lock.
29
Two phases for transaction: Growing phase - acquires all locks but cannot release any locks. Shrinking phase - releases locks but cannot acquire any new locks.
30
31
32
33
T2
T1 commits
Cascading rollback
34
Now T2 aborts!
35
Summary
Concurrency
Serial Schedules
Conflict-serializable Schedules Serializable Schedules
Two-Phase Locking
36
Deadlock
An impasse that may result when two (or more) transactions are each waiting for locks held by the other to be released.
37
38
Timeouts
Transaction
that requests lock will only wait for a system-defined period of time. If lock has not been granted within this period, lock request times out. In this case, DBMS assumes transaction may be deadlocked, even though it may not be, and it aborts and automatically restarts the transaction.
39
Deadlock Prevention
DBMS
looks ahead to see if transaction would cause deadlock and never allows deadlock to occur. Could order transactions using transaction timestamps: Wait-Die - only an older transaction can wait for younger
one, otherwise transaction is aborted (dies) and restarted with same timestamp. Wound-Wait - only a younger transaction can wait for an older one. If older transaction requests lock held by younger one, younger one is aborted (wounded).
40
Create a node for each transaction. Create edge Ti -> Tj, if Ti waiting to lock item locked by Tj.
Deadlock exists if and only if WFG contains cycle. WFG is created at regular intervals.
41
42
Several issues: choice of deadlock victim; how far to roll a transaction back; avoiding starvation.
43
Exercise
Consider the following schedule involving three transactions T1, T2 and T3:
Time 1 2 3 4 5 6 7 8 9 10 11 12 T1 T2 W(A) R(B) R(C) W(C)
W(C) Commit W(B) W(C)
T3
W(B)
R(A)
Commit Commit
Describe how the strict two-phase locking protocol with headlock detection would handle the schedule. 44
Exercise
Consider the same schedule involving three transactions T1, T2 and T3:
Time 1 2 3 4 5 6 7 8 9 10 11 12 T1 T2 W(A) R(B) R(C) W(C)
W(C) Commit W(B) W(C)
T3
W(B)
R(A)
Commit Commit
Describe how the strict two-phase locking with wound-wait deadlock prevention would handle the schedule.
45
Validation
Maintain a record of what active transactions are doing
46
Timestamping
Transactions ordered globally so that older transactions, transactions with smaller timestamps, get priority in the event of conflict. Conflict is resolved by rolling back and restarting transaction. No locks so no deadlock. Timestamp: A unique identifier created by DBMS that indicates relative starting time of a transaction.
47
Timestamping - definition
Timestamping: a concurrency control protocol that orders transactions in such a way that older transactions get priority in the event of a conflict. Read/write proceeds only if last update on that data item was carried out by an older transaction. Otherwise, transaction requesting read/write is restarted and given a new timestamp. Also timestamps for data items:
read-timestamp - timestamp of last transaction to read item; write-timestamp - timestamp of last transaction to write item. 48
Conflict serializable schedule that is equivalent to a serial schedule in which the timestamp order of transactions is the order to execute them
Serial schedule
T starts U starts V starts
Example
T1 200 r1(B) Transactions T2 T3 150 175 Database elements A B C RT=0 RT=0 RT=0 WT=0 WT=0 WT=0 RT=200
RT=150 r2(A) RT=175 rtoo 3(c) Writing w1(B) WT=200 late! WT=200 w1(A) w2(C) Abort; w3(A) WT=175 52
Locks
Superior in high-conflict situations Frequently delay transactions as they wait for locks
In high-conflict situations, rollback will be frequent, introducing more delays than a locking system
53
Comparison of Methods
54
optimistic concurrency control Maintains a record of what active transactions are doing Just before a transaction starts to write, it goes through a validation phase If a there is a risk of physically unrealizable behavior, the transaction is rolled back Potentially allows greater concurrency than traditional protocols.
55
Validation-based Scheduler
Keep track of each transaction Ts Read set RS(T): the set of elements T read Write set WS(T): the set of elements T write Execute transactions in three phases: 1. Read. T reads all the elements in RS(T) 2. Validate. Validate T by comparing its RS(T) and WS(T) with those in other transactions. If the validation fails, T is rolled back 3. Write. T writes its values for the elements in WS(T)
56
the set of transactions that have started, but not yet completed validation. For each T, maintain (T, START(T)) VAL: the set of transactions that have been validated, but not yet finished. For each T, maintain (T, START(T), VAL(T)) FIN: the set of transaction that have completed. For each T, maintain (T, START(T), VAL(T), FIN(T))
57
may think of each transaction that successfully validates as executing at the moment that it validates
U validates
V validates
U start
T start
U validated
T validating
59
U validated
T validating
U finish
60
Validation Rules To validate a transaction T, 1. Check that RS(T) WS(U) is an empty set for any validated U and START(T) < FIN(U) 2. Check that WS(T) WS(U) is an empty set for any validated U that did not finish before T validated, i.e., if VAL(T) < FIN(U)
61
Example
RS = {B} WS= {D} U
= start
T RS={A,B} WS= {A,C}
= validate = finish
62
Storage utilization Locks: space in the lock table is proportional to the number of database elements locked Timestamps: Read and write times for recently accessed database elements Validation: timestamps and read/write sets for each active transaction, plus a few more transactions that finished after some currently active transaction started
63
Locking delays transactions but avoids rollbacks, even when interaction is high If interference is low, neither timestamps nor validation will cause many transactions abort When a rollback is necessary, timestamps catch some problems earlier than validation
64