Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a...

30
Transactions
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    215
  • download

    1

Transcript of Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a...

Page 1: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

Transactions

Page 2: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

Definitions• Transaction (program): A series of

Read/Write operations on items in a Database.

• Example: Transaction 1

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),

Page 3: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

Definitions

• Schedule: The order of execution of operations of 2 or more transactions.

Read(A)Read(C)

Write(A)Read(B)

Write(C)Read(B)Write(B)

Write(B)

Transaction1 Transaction2

R1(A)R2(C)

W1(A)R1(B)

W2(C)R2(B)W2(B)

W1(B)

Schedule S1Schedule S1

Page 4: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

Example• Transaction T1: R1(A), R1(B), W1(B), W1(A)• Transaction T2: R2(A), W2(B), R2(B), W2(A)

• Possible Schedule of T1,T2: R1(A), R1(B), R2(A), W2(B), R2(B), W1(B), W1(A), W2(A)

• NOT a schedule of T1,T2:R1(B), W1(B), R2(A), W2(B), R2(B), R1(A), W1(A), W2(A)

Page 5: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

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)

Page 6: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

Schedules• A schedule is “correct” if it gives the same

result as a serial schedule for any calculation.

• Why is this a “wrong” schedule?

Read(A) Read(A) Write(A)Write(A)Read(B)Write(B) Read(B) Write(B)

Perform A=A+100 and

write A

Perform A=A+100

and write A

In total A was raised by 100 instead of 200!

Page 7: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

Schedules

• Example for a “correct” schedule:

Read(A) Read(C) Write(C)Write(A)Read(B)Write(B) Read(B) Write(B)

Read(A) Write(A)Read(B)Write(B) Read(C) Write(C) Read(B) Write(B)

Will always give the same

result as

And this will never cause an

interleaving problem

Page 8: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

• 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.

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

Page 9: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

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?

Page 10: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

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?

Page 11: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

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?

Page 12: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

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?

Page 13: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

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

Page 14: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

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

Page 15: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

• 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)

Page 16: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

• 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

Page 17: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

• 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 there is Read(A) in T1, “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

Page 18: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

• 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

Page 19: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

• 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)

R(B) W(B)

W(B)R(B)

Blind write!

S2

S2

S1

Page 20: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

R(B)R)B(W(B)R(B) W(B)

Why is the No Blind Writes demand Necessary?

R(B)W(B)R(B) W(B)

S1S2

R(B) W(B)

W(B)R(B)

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

Page 21: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

• 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, 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…

Page 22: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

• 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:

Page 23: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

• 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.

Page 24: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

• 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

Page 25: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

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

Page 26: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

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(

Page 27: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

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.

Page 28: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

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 equivalence

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’

Page 29: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

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!

Page 30: Transactions. Definitions Transaction (program): A series of Read/Write operations on items in a Database. Example: Transaction 1 Read(C) Read(A) Write(A)

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!