View
217
Download
0
Tags:
Embed Size (px)
Citation preview
Transactions or Concurrency Control
Introduction• A program which operates on a DB performs 2
kinds of operations:– Access to the Database (Read/Write)– Memory operations
DISK
sailors
Reserves
Main Memory
Read operationRead operation
Memory operations
Read + Memory operations
Introduction• When dealing with concurrency control, we
are only interested in operations on the DB: Read or Write
• So, we deal with “Abstractions” of programs
• An “Abstraction” of a program is a series of operations the program performs on a DB
• We also call this a Transaction (or sometimes, a Program)
Example
Read(C)Read(A)Write(A)Read(B)Write(B)Write(C) Items=
A, B, C
R1(C)R1(A)W1(A)R1(B)W1(B)W1(C)
operations= Read(A),
…
Transaction 1 Transaction 1
or
Definitions
• Schedule: The order of execution of operations of 2 or more transactions.
R(A) R(C)
W(A)R(B)
W(C) R(B) W(B)
W(B)
Transaction1 Transaction2
Schedule S1
Tim
e
• When a single Transaction is run, there is no Concurrency Control problem
• When there are more, problems might occur
• Example: 2 programs, each adding 100$ to an account A
Example: 2 programs, each adding 100$ to an account A
• If they are run one after the other:
R(A)W(A)
Transaction 1
R(A)W(A)
Transaction 2
Tim
e
No problem!
Example: 2 programs, each adding 100$ to an account A
• If they are run in parallel:
R(A)
W(A)
Transaction 1
R(A)W(A)
Transaction 2
Tim
e
Problem! Why?
Definitions• Serial Schedule: A schedule in which the
transactions are performed one after the other in a serial manner.
Read(A)Write(A)Read(B)Write(B) Read(C) Write(C) Read(B) Write(B)
Schedules• A schedule is “correct” if it gives the same
result as a serial schedule for any calculation.
• Examples:
Read(A) Read(A) Write(A)Write(A)Read(B)Write(B) Read(B) Write(B)
Read(A) Read(A)Write(A) Write(A)
Read(B)Write(B) Read(B) Write(B)
Read(A) Write(A) Read(A) Write(A)Read(B)Write(B) Read(B) Write(B)
Schedules
• Example for a “correct” schedule:
Read(A) Write(A)Read(B)Write(B) Read(A) Write(A) Read(B) Write(B)
Will always give the same
result as
And this will never cause an
interleaving problem
Read(A) Write(A) Read(A) Write(A)Read(B)Write(B) Read(B) Write(B)
• We would thus like to know when 2 schedules are equivalent
• Equivalent: Will give the same result for any input
• How do you check for equivalence?• Naïve approach: Check the output for all inputs• This is clearly impossible• So, we need a simple set of rules to tell us if 2
schedules are equivalent
• Schedules are View Equivalent if:1. They consist of the same transactions.
2. If Tk reads an initial value for A in S1, then Tk will also read an initial value for A in S2 (“initial”=A has not been written to yet).
3. If Tk reads a value of A written by Ti in S1, then Tk will also read a value of A written by Ti in S2.
4. If Ti writes a final value for A in S1, then Ti writes a final value for A in S2.
What are the violations of the following schedules to view-equivalence?
R1(A) R2(C) W2(C)W1(A)R1(C)W1(C) R3(C) W3(C) R2(B) W2(B)
Schedule S1
T1 T2 T3
R1(A)W1(A)R1(C)W1(C) R2(C) W2(C) R2(B) W2(B) R3(C) W3(C)
Schedule S2
T1 T2 T3
R1(A) R2(C) W2(C)W1(A)R1(C)W1(C) R3(C) W3(C) R2(B) W2(B)
Schedule S1
T1 T2 T3
R1(A) R2(C) W2(C)W1(A)R1(C)W1(C) R2(B) W2(B) W3(C) R3(C)
Schedule S2
T1 T2 T3
Are these schedules View-Equivalent?
R2(C) W2(C)R1(A)W1(A)R1(C)W1(C) W3(C) R3(C) R2(B) W2(B)
Schedule S1
T1 T2 T3
R1(A) R2(C) W2(C)W1(A)R1(C)W1(C) R2(B) W2(B) W3(C) R3(C)
Schedule S2
T1 T2 T3
Are these schedules View-Equivalent?
R1(A) R2(C) W2(C)W1(A)R1(C)W1(C) R3(C) W3(C) R2(B) W2(B)
Schedule S1
T1 T2 T3
R1(A) R2(C) W2(C)W1(A) R2(B) W2(B) R3(C) R1(C)W1(C) W3(C)
Schedule S2
T1 T2 T3
Are these schedules View-Equivalent?
R1(A) R2(C) W2(C)W1(A)R1(C)W1(C) R3(C) W3(C) R2(B) W2(B)
Schedule S1
T1 T2 T3
R1(A) R2(C) W2(C)W1(A)R1(C)W1(C) R2(B) W2(B) R3(C) W3(C)
Schedule S2
T1 T2 T3
Are these schedules View-Equivalent?
View-Equivalence• If 2 schedules are view-equivalent:
– The same transactions will read the same values in both schedules
– Therefore, they will also write the same values– This is true for any calculation
Definitions• A schedule is View-Serializable if it is
View-Equivalent to some Serial schedule.
Read(A) Read(C)Write(A)
Read(B)
Write(C)Write(B)
Read(B)
Write(B)
Read(A)Write(A)Read(B)Write(B) Read(C)
Write(C)
Read(B)
Write(B)
S1 S2
Schedule S1 is view-equivalent to a serial schedule (S2), so it is
View-Serializable
• What is the Serial Schedule that S1 is equivalent to?
W(B) R(A) W(A)R(A)R(B)
R(C)W(B)
W(C) R)C(
S1 S2R(A)
W(A)R(C)W(C)
R)C(W(B) R(A)R(B)W(B)
• What is the Serial Schedule that S1 is equivalent to?
R(A) W(A)R(A)R(C)
R(C)W(A)
W(C) R)A(
S1
There is no Serial Schedule that S1 is view equivalent to.
In other words, S1 is not View-Serializable
• We already said that for any equivalent S1, S2: If Tk
reads a value of A written by Ti in S1, then Tk will also read a value of A written by Ti in S2.
• In simpler words: If in S1 Read(A) in T1 is “lower” than Write(A) in T2, then this has to hold in S2 too.
• And in a picture:
• What about Write(A) which is “lower” than Read(A)? And Write(A) which is “lower” than Write(A)?
Do these also have to hold in an equivalent schedule?
W(B) R(A) W(A)R(A)R(B)
R(B)
S1Lower = later
• Blind Write: A transaction performs a Blind Write of A if it writes A without reading it before.
• Assuming there are no Blind Writes, and S2 is an equivalent serial schedule :1. If Tk writes a value of A which was previously
read by Ti in S1, then this will happen in S2 too.
2. If Tk writes a value of A which was previously written by Ti in S1, then this will happen in S2 too.
Read(A)Write(A)Read(C)Write(B)
Blind Write
• We want to show that if Write(B) in Ti is “lower” than Read(B) in Tk then this has to happen in any equivalent serial schedule.
• Suppose this is the case:
• So, suppose this is the case:
R(B)R(B) W(B)
S1 R(B) W(B)R(B)
R(B)W(B)R(B) W(B)
Blind write!
S2
S2
S1
R(B) W(B)W(B)R(B)
Why is the No Blind Writes demand Necessary?
S1S2
Blind write!
R(B) W(B)
R)B(W(B)R(B)
S1S2
No Blind write
Bottom line: if there are no blind writes, If Tk writes a value of A which was previously read by Ti in S1, then this will
happen in any equivalent serial schedule
R(B)W(B)R(B) W(B)
R(B) W(B)W(B)R(B)
R(B)R)B(W(B)R(B) W(B)
• This can also be shown for two Write operations in the same way.
This leads us to the following definition:• There is a Conflict between 2 operations in different transactions, if
at least one of them is a Write, and they are performed on the same item A.
• According to what we showed, if there are no blind writes, the direction of the conflict (arrow) has to be kept in any equivalent serial schedule !
• So is there a view-equivalent serial schedule to S1?
R(A) W(A)R(A)R(B)
R(B)W(B)
W(B) R)C(
S1
Find the conflicts…
• We can now define equivalence between schedules according to their conflicts:
• Schedules S1, S2 are Conflict Equivalent if they consist of the same transactions and the conflict arrows have the same directions.
R(A) W(A)R(A)R(B)
R(B)W(B)
W(B) R)C(
S1 R(A) W(A)R(A)
R(B)R(B)
W(B) W(B) R)C(
S2
Conflict -Equivalent:
• Lemma: Conflict Equivalence => View Equivalence (this is true even if there are Blind Writes!)
Proof: We assume S1 and S2 are Conflict Equivalent. We need to prove 1-4 from above.
Schedules are View Equivalent if:1. They consist of the same transactions.2. If Tk reads an initial value for A in S1, then Tk will also read an
initial value for A in S2 (initial=A has not been written to).3. If Tk reads a value of A written by Ti in S1, then Tk will also read a
value of A written by Ti in S2.4. If Ti writes a final value for A in S1, then Ti writes a final value for
A in S2.
Schedules are Conflict Equivalent if:1. They consist of the same transactions.2. The conflict arrows have the same directions.
• Schedule S1 is Conflict Serializable if it is Conflict-Equivalent to some serial schedule S2.
• Conflict Serializable => View Serializable (directly from the Lemma).
• The other direction is not necessarily true if there are Blind Writes:
T 1T2 T3
R(A)W(A)
W(A) W(A)
T 1T2 T3
R(A)W(A)
W(A)
W(A)
S1
S2
There is no serial schedule which is conflict-equivalent to s1
But S2 is serial and is view- equivalent to S1
The precedence graph
R(A) W(A)R(A)
R(B)R(B)
W(B) W(B) R)C(
S1T2T1 T1
T2
S1 is conflict-serializable iff its precedence graph
doesn’t contain a circular path
Node for each
transaction
Edge from T1 to T2 if there is a
conflict between T1 and T2 in
which T1 occurs first
Which is conflict-Serializable?W(B) R(A) W(A)R(A)R(B)
R(C)W(B)
W(C) R)C(
W(B) R(A) W(B)R(A)R(B)
R(C)W(C)
W(C) R)C(
R(C) R(A) W(A) W(B)R(A)
R(B)W(B)
R)C(
Locks• Used in order to allow only serializable schedules.• The principle: before performing a write/read on item
A, a transaction asks for a lock on A.• Only after getting the lock from the lock-manager can
the transaction perform the read/write.• 2 kinds of locks:
1. Shared lock: many transactions can hold a shared lock on the same item at the same time.
2. Exclusive lock: only one transaction can hold an exclusive lock on an item at any given time.
– In order to Read, a Shared Lock is needed.– In order to Write, an Exclusive Lock is needed.
2-Phase Locking (2PL)• A protocol (set of rules) which uses locks to ensure
only serializable schedules.• The only additional rule: after a transaction has freed a
lock it cannot get any new lock.• This means every transaction will perform 2 phases:
getting locks, and then releasing locks.• 2PL => conflict serializability
T3
T2T1
R/W(A) R/W(A)
R/W(B) R/W(B) R/W(C)R/W(C)
At least one end of each arrow is a
‘Write’
Recovering from crashes• Up until now we ignored the possibility of a crash of a
transaction.• To handle such a case we remember Commit and
Rollback.• Consider this schedule:
W(B) R(A) W(A)R(A)R(B)
R(C)W(B)
W(C) R)C(
T1 T2
T1 finished so it commits
Crash!!
Problem: T1 read a value which T2
wrote, and committed. The
schedule is called “Not Recoverable”
T2 rolls back
Notice that this
schedule is Conflict
Serializable!
Recovering from crashes• Solution: Commit only after all transactions which you
have read from have committed (assuming your are a transaction).
• Even more strict solution: Read an item only after all transactions which write this item have committed
• This leads to a new protocol:• Strict 2PL: Same rules as 2PL with the addition that a
transaction releases its locks only after it has committed.
• Strict 2PL ensures recovering. Why?
Good luck!