55
IDA / ADIT Databasteknik Databaser och bioinformatik Transaction Fang Wei-Kleiner

Databasteknik Databaser och bioinformatik Transaction

  • Upload
    ronda

  • View
    50

  • Download
    0

Embed Size (px)

DESCRIPTION

Databasteknik Databaser och bioinformatik Transaction. Fang Wei-Kleiner. Transactions. A transaction is a logical unit of database processing and consists of one or several operations. Database operations in a simplified model: read-item(X) write-item(X). Read-item(my-account). - PowerPoint PPT Presentation

Citation preview

Page 1: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT

DatabasteknikDatabaser och bioinformatik

Transaction

Fang Wei-Kleiner

Page 2: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 2

Transactions

• A transaction is a logical unit of database processing and consists of one or several operations.

• Database operations in a simplified model: o read-item(X)o write-item(X)

Page 3: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 3

Transactions - examples

T1 T2

Read-item(my-account)my-account := my-account - 2000Write-item(my-account)Read-item(other-account)other-account := other-account + 2000

Write-item(other-account)

Read-item(my-account)my-account := my-account +1000Write-item(my-account)

Page 4: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 4

Transactions

• Q: How to execute a read-item and a write-item?

• Note: more about buffers in the next lecture.

Page 5: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 5

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 program variable X.

Page 6: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 6

Write-item(X)

1. Locate the block on disk that contains X2. Copy the block to primary memory (a buffer)3. Copy the value of program variable X to the right place in

the buffer4. Store the modified block on disk.

Page 7: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 7

Schedule

• A schedule defines the order between the operations in the different transactions.

Page 8: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 8

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

Page 9: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 9

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

Page 10: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 10

Dirty read problem

T1 T2

Read-item(my-account)my-account := my-account - 2000Write-item(my-account)

Read-item(other-account)

FAIL

Read-item(my-account)my-account := my-account +1000

Write-item(my-account)

TIME

Page 11: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 11

Incorrect summary problemT1 T2

Read-item(my-account1)my-account1 := my-account1 - 2000Write-item(my-account1) Read-item(my-account1)

sum := sum + my-account1

TIME

Read-item(my-account2)my-account2 := my-account2 + 2000Write-item(my-account2)

Read-item(my-account2)sum := sum + my-account2

sum := 0

Page 12: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 12

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)

Page 13: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 13

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 schema and other conditions declared for the database), is also in a consistent state after the execution.

Page 14: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 14

Properties for transactions

ACID: Atomicity, Consistency preservation, Isolation, Durability

• I: A transaction should act as if it is executed isolated from other transactions.

• D: Changes in the database made by a committed transaction are permanent.

Page 15: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 15

Properties for transactions

How are the ACID properties achieved? • A: recovery system• C: programmer + DBMS• I: concurrency contol • D: recovery system

Page 16: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 16

Concurrency control (Isolation)

Page 17: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 17

Serial and serializable schedules

• A schedule S is serial if the operations in every transaction T are executed directly after each other

perfect with respect to isolation, but …• A schedule S is serializable if there is an equivalent serial

schedule S’ Equivalent: conflict-equivalent.

Page 18: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 18

Transactions

T1 T2

Read-item(my-account)my-account := my-account - 2000Write-item(my-account)Read-item(other-account)other-account := other-account + 2000

Write-item(other-account)

Read-item(my-account)my-account := my-account +1000Write-item(my-account)

Page 19: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 19

Serial schedule

T1 T2

Read-item(my-account)my-account := my-account - 2000Write-item(my-account)Read-item(other-account)other-account := other-account + 2000

Write-item(other-account)

Read-item(my-account)my-account := my-account +1000Write-item(my-account)

TIME

Page 20: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 20

Serial scheduleT1 T2

Read-item(my-account)my-account := my-account - 2000Write-item(my-account)Read-item(other-account)other-account := other-account + 2000

Write-item(other-account)

Read-item(my-account)my-account := my-account +1000Write-item(my-account)

TIME

Page 21: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 21

Non-serial schedule

T1 T2

Read-item(my-account)my-account := my-account - 2000Write-item(my-account)

other-account := other-account + 2000Read-item(other-account)

Write-item(other-account)

Read-item(my-account)my-account := my-account +1000Write-item(my-account)

TIME

Page 22: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 22

Non-serial schedule (2)

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

Page 23: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 23

• Want schedules that are “good”, regardless ofo initial state ando transaction semantics

• Only look at order of read and writes

What is a good schedule?

Page 24: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 24

Non-serial schedule (S)

T1 T2

Read-item(my-account)my-account := my-account - 2000Write-item(my-account)

other-account := other-account + 2000Read-item(other-account)

Write-item(other-account)

Read-item(my-account)my-account := my-account +1000Write-item(my-account)

TIME

Page 25: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 25

Non-serial schedule (S’)

T1 T2

Read-item(my-account)my-account := my-account - 2000Write-item(my-account)

other-account := other-account + 2000Read-item(other-account)

Write-item(other-account)

Read-item(my-account)my-account := my-account +1000Write-item(my-account)

TIME

Page 26: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 26

S equivalent to S’

T1 T2

Read-item(my-account)my-account := my-account - 2000Write-item(my-account)

other-account := other-account + 2000Read-item(other-account)

Write-item(other-account)

Read-item(my-account)my-account := my-account +1000Write-item(my-account)

TIME

No conflict on the resource

Page 27: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 27

Non-serial schedule (2)

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!

Page 28: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 28

Conflicts

• Two operations are in conflict if:o they belong to different transactionso they access (read/write) the same data Xo one of the operations is a write-item(X)

Page 29: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 29

Conflict-equivalence

• Two schedules S and S’ 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 that are in conflict until one gets a serial schedule.

Page 30: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 30

Serializable schedule

T1 T2

Read-item(my-account)my-account := my-account - 2000Write-item(my-account)

other-account := other-account + 2000Read-item(other-account)

Write-item(other-account)

Read-item(my-account)my-account := my-account +1000Write-item(my-account)

TIME

Page 31: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 31

Not serializable schedule

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

Page 32: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 32

Algorithm: Serializability testWith a directed graph:1. Create a node for each transaction2. If Tj executes a read-item(X) after Ti executes a write-item(X), create an arch Ti Tj

3. If Tj executes a write-item(X) after Ti executes a read-item(X), create an arch Ti Tj

4. If Tj executes a write-item(X) after Ti executes a write-item(X), create an arch Ti Tj

S is serializable if the graph does not contain any cycles.

Page 33: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 33

Serializable schedule

T1 T2

Read-item(my-account)my-account := my-account - 2000Write-item(my-account)

other-account := other-account + 2000Read-item(other-account)

Write-item(other-account)

Read-item(my-account)my-account := my-account +1000Write-item(my-account)

TIME

Page 34: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 34

Not serializable schedule

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

Page 35: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 35

Example

T1

R(A)

T2

W(C)

T3

W(A)

T4

R(A)

W(B)R(C)

W(A)

W(D)

Page 36: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 36

Example

T1

R(A)

T2 T3

W(A)

T4

R(A)

W(A)

Page 37: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 37

• Can we make sure that we only get serializable schedules?

Page 38: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 38

Locking

• Locking: to control access to data• Shared/Exclusive lock or read/write lock

o read-lock(X) (shared lock)• If X is unlocked or locked by a shared lock, lock it, otherwise wait

until it is possible to lock ito write-lock(X) (exclusive lock)

• If X is unlocked, lock it, otherwise wait until X is unlockedo unlock(X).

Page 39: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 39

Shared/Exclusive locking

1. A transaction T should lock X with a read-lock(X) or a write-lock(X) before executing a read-item(X).

2. A transaction T should lock X with a write-lock(X) before executing a write-item(X).

3. A transaction T should unlock X with a unlock(X) after all read-item(X) and write-item(X) in T have been executed.

Page 40: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 40

Shared/Exclusive locking

4. A transaction T should not use a read-lock(X) if it already has a read or write lock on X.

5. A transaction T should not use a write-lock(X) if it already has a read or write lock on X.

4 and 5 can sometimes be replaced by up- and downgrading of locks.

Page 41: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 41

Two-phase locking

• A transaction follows the two-phase locking protocol if all locking operations (read-lock and write-lock) for all data items come before the first unlock operation in the transaction

• A transaction that follows the two-phase locking protocol has an expansion phase and a shrinking phase.

Page 42: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 42

Two-phase locking – allowed transactions?

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 + 2000Write-item(my-account2)

Read-lock(my-account1)

Unlock(my-account2)

Page 43: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 43

Two-phase locking – allowed transactions?

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 + 2000Write-item(my-account2)

Read-lock(my-account1)

Unlock(my-account2)

Page 44: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 44

Follow 2PL Protocol?

T1 T2

Read-item(my-account)my-account := my-account - 2000Write-item(my-account)

other-account := other-account + 2000Read-item(other-account)

Write-item(other-account)

Read-item(my-account)my-account := my-account +1000Write-item(my-account)

TIME

Page 45: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 45

Follow 2PL Protocol?T1 T2

Read-item(my-account)my-account := my-account - 2000Write-item(my-account)

other-account := other-account + 2000Read-item(other-account)

Write-item(other-account)

Read-item(my-account)my-account := my-account +1000Write-item(my-account)

TIME

lock(my-account)

lock(my-account)

lock(other-account)???

lock(other-account)???

Page 46: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 46

Follow 2PL Protocol? (yes)T1 T2

Read-item(my-account)my-account := my-account - 2000Write-item(my-account)

other-account := other-account + 2000Read-item(other-account)

Write-item(other-account)

TIME

lock(my-account)

Read-item(my-account)my-account := my-account +1000Write-item(my-account)

lock(my-account)

lock(other-account)unlock(my-account)

unlock(other-account)

unlock(my-account)

Page 47: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 47

Follow 2PL Protocol?

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

Page 48: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 48

Follow 2PL Protocol? (no!)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)

TIMElock(my-account)

lock(my-account)

Page 49: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 49

Follow 2PL Protocol? (no!)

T1

R(A)

T2

W(C)

T3

W(A)

T4

R(A)

W(B)R(C)

W(A)

W(D)

Page 50: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 50

Follow 2PL Protocol? (yes) T1 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)

Page 51: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 51

Serializability through 2PL

Theorem If all transactions follow the two-phase locking protocol then the schedule is serializable.

• Follow (aka permit) the two-phase locking protocol means we can apply the protocol so that the transactions interleave as it is.

However, there are serializable schedules which do not follow two-phase locking.

Page 52: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 52

Serializable, but not follow 2PL Protocol

T1 T2 T3

write(x)

write(x)

write(y)

write(y)

Page 53: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 53

Deadlock

• Two or more transactions wait for each other to get data unlocked

• Deadlock prevention: - lock all data beforehand, wait-die, wound-wait, no waiting,

cautious waiting• Deadlock detection: wait-for graph, timeouts

Page 54: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 54

Deadlock

T1

Write-lock(my-account1)

Write-lock(my-account2)

T2

Write-lock(my-account2)

Write-lock(my-account1)

TIME

Page 55: Databasteknik Databaser och bioinformatik Transaction

IDA / ADIT 55

Starvation

• A transaction is not executed for an indefinite period of time while other transactions are executed normally