ida / adit lecture 9: transactions and concurrency control jose m. peña [email protected]

50
IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña [email protected]

Upload: lizbeth-martin

Post on 02-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT

Lecture 9: Transactions and concurrency control

Jose M. Peñ[email protected]

Page 2: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 2

How can several users access

and update the database at the

same time ?

Real world

Model

Databasesystem

Physical database

Databasemanagementsystem

Processing of Queries/updates

Access to stored data

Page 3: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 3

Concurrent processing

• Single user system: At most one user can use the system at each point in time.

• Multiple user system: Several users can use the system at the same time.o Multiple CPU: Parallel processing.o One CPU: Concurrent processing, interleaving.

• Hereinafter, we focus on multiple user systems with just one CPU.

Page 4: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 4

Transactions: Definition

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

• Simplified database operations in a transaction: o Read-item(X)o Write-item(X)

Page 5: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 5

Transactions: 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)

Page 6: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 6

How to execute 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 the program variable X.

Page 7: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 7

How to execute Write-item(X)

1. Locate the block on disk that contains X.

2. Copy the block to primary memory (a buffer).

3. Copy the value of the program variable X to the right place in the buffer.

4. Store the modified block on disk.

Page 8: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 8

Schedule: Definition

• A schedule defines the order in which the operations in a set of transactions are to be executed.

• Is any order equally good ?

Page 9: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 9

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 10: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 10

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 11: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 11

Dirty read problem

T1 T2

Read-item(my-account)

my-account := my-account - 2000

Write-item(my-account)

Read-item(other-account)

FAIL

Read-item(my-account)

my-account := my-account +1000

Write-item(my-account)

TIME

Page 12: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 12

Incorrect summary problem

T1 T2

Read-item(my-account1)

my-account1 := my-account1 - 2000

Write-item(my-account1)Read-item(my-account1)

sum := sum + my-account1

TIME

Read-item(my-account2)

my-account2 := my-account2 + 2000

Write-item(my-account2)

Read-item(my-account2)

sum := sum + my-account2

sum := 0

Page 13: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 13

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 14: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 14

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 relational model and any other condition declared for the database), is also in a consistent state after the execution of the transaction.

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

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

Page 15: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 15

Properties for transactions

• Who ensures that the ACID property are satisfied ? o Atomicity: Recovery system.o Consistency preservation: Programmer + DBMS.o Isolation: Concurrency contol. o Durability: Recovery system.

Page 16: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 16

Serial schedule: Definition

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

• A serial schedule satisfies the isolation property but it may highly inefficient, i.e. it may not maximize the CPU usage.

Page 17: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 17

Serial 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 18: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 18

Serial 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 19: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 19

Non-serial schedule: Example

T1 T2

Read-item(my-account)

my-account := my-account - 2000

Write-item(my-account)

other-account := other-account + 2000

Read-item(other-account)

Write-item(other-account)

Read-item(my-account)

my-account := my-account +1000

Write-item(my-account)

TIME

Page 20: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 20

Non-serial 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 21: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 21

• A serial schedule satisfies the isolation property but it may highly inefficient, i.e. it may not maximize the CPU usage.

• Solution: Find a non-serial schedule that is equivalent to a serial schedule.

• Equivalent = They put the database in the same final state for any initial state of the database.

• Equivalent = Conflict equivalent, i.e. they execute the conflicting operations in the same order, e.g. Read-item(X) after Write-item(X), etc.

• A schedule is serializable if there is a conflict equivalent serial schedule.

Serializable schedule: Definition

Page 22: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 22

Serializable schedule: Example

T1 T2

Read-item(my-account)

my-account := my-account - 2000

Write-item(my-account)

other-account := other-account + 2000

Read-item(other-account)

Write-item(other-account)

Read-item(my-account)

my-account := my-account +1000

Write-item(my-account)

TIME

Page 23: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 23

Serializable schedule: Example

T1 T2

Read-item(my-account)

my-account := my-account - 2000

Write-item(my-account)

other-account := other-account + 2000

Read-item(other-account)

Write-item(other-account)

Read-item(my-account)

my-account := my-account +1000

Write-item(my-account)

TIME

No conflict on the resource

Page 24: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 24

Serializable schedule: Example

T1 T2

Read-item(my-account)

my-account := my-account - 2000

Write-item(my-account)

other-account := other-account + 2000

Read-item(other-account)

Write-item(other-account)

Read-item(my-account)

my-account := my-account +1000

Write-item(my-account)

TIME

No conflict on the resource

Page 25: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 25

Non-serializable 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

Conflict!

Conflict!

Page 26: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 26

Conflict: Definition

• Two operations are in conflict ifo they belong to different transactions,o they access (read/write) the same data item X, ando one of the operations is a Write-item(X).

Page 27: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 27

Conflict equivalence: Definition

• A schedule is serializable if there is a conflict equivalent serial schedule.

• Two schedules and 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 and get a serial schedule.

Page 28: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 28

Serializable schedule: Example

T1 T2

Read-item(my-account)

my-account := my-account - 2000

Write-item(my-account)

other-account := other-account + 2000

Read-item(other-account)

Write-item(other-account)

Read-item(my-account)

my-account := my-account +1000

Write-item(my-account)

TIME

Page 29: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 29

Non-serializable 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 30: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 30

Checking if a schedule is serializable

• Create the following directed graph:1. Create a node for each transaction in the schedule.

2. If transation Tj executes a Read-item(X) after transaction Ti executes a Write-item(X), then create an arch Ti Tj .

3. If transation Tj executes a Write-item(X) after transaction Ti executes a Read-item(X), then create an arch Ti Tj .

4. If transation Tj executes a Write-item(X) after transaction Ti executes a Write-item(X), then create an arch Ti Tj .

• The schedule is serializable if and only if the graph above does not contain any directed cycle.

Page 31: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 31

Serializable schedule: Example

T1 T2

Read-item(my-account)

my-account := my-account - 2000

Write-item(my-account)

other-account := other-account + 2000

Read-item(other-account)

Write-item(other-account)

Read-item(my-account)

my-account := my-account +1000

Write-item(my-account)

TIME

Page 32: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 32

Non-serializable 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 33: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 33

(Non-)Serializable schedule: Example

T1

R(A)

T2

W(C)

T3

W(A)

T4

R(A)

W(B)

R(C)W(A)

W(D)

Page 34: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 34

(Non-)Serializable schedule: Example

T1

R(A)

T2 T3

W(A)

T4

R(A)

W(A)

Page 35: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 35

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

Key question

Page 36: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 36

Locks: Definition

• Locks control access to data.• Every data item X has a lock in one of three possible states:

o Read-locked (shared).o Write-locked (exclusive).o Unlocked.

• Read-lock(X):o If X is unlocked or read-locked, then it read-locks it, else it waits until

it becomes unlocked or read-locked.

• Write-lock(X):o If X is unlocked, then it write-locks it, else it waits until X becomes

unlocked.

• Unlock(X):o If X is write-locked, then it unlocks X.o If X is read-locked only once, then it unlocks X.

Page 37: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 37

Locks: Usage

1. A transaction T must issue a Read-lock(X) or Write-lock(X) before executing a Read-item(X).

2. A transaction T must issue a Write-lock(X) before executing a Write-item(X).

3. A transaction T must issue a Unlock(X) after all the Read-item(X) and Write-item(X) in T have been executed.

4. A transaction T must not issue a Read-lock(X) or Write-lock(X) if it already has a read or write lock on X.

Page 38: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 38

Two-phase locking protocol: Definition

• A transaction follows the two-phase locking (2PL) protocol if all Read-lock() and Write-lock() operations come before the first Unlock() operation in the transaction.

• A transaction that follows the 2PL protocol has an expansion phase and a shrinking phase.

• If all the transactions in a schedule follow the 2PL protocol, then the schedule is serializable.

Page 39: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 39

Two-phase locking protocol: Example

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 + 2000

Write-item(my-account2)

Read-lock(my-account1)

Unlock(my-account2)

Page 40: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 40

Two-phase locking protocol: Example

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 + 2000

Write-item(my-account2)

Read-lock(my-account1)

Unlock(my-account2)

Page 41: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 41

Two-phase locking protocol: Example

T1 T2

Read-item(my-account)

my-account := my-account - 2000

Write-item(my-account)

other-account := other-account + 2000

Read-item(other-account)

Write-item(other-account)

Read-item(my-account)

my-account := my-account +1000

Write-item(my-account)

TIME

Page 42: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 42

Two-phase locking protocol: ExampleT1 T2

Read-item(my-account)

my-account := my-account - 2000

Write-item(my-account)

other-account := other-account + 2000

Read-item(other-account)

Write-item(other-account)

Read-item(my-account)

my-account := my-account +1000

Write-item(my-account)

TIME

Write-lock(my-account)

Write-lock(my-account)

Write-lock(other-account)???

Write-lock(other-account)???

Page 43: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 43

Two-phase locking protocol: ExampleT1 T2

Read-item(my-account)

my-account := my-account - 2000

Write-item(my-account)

other-account := other-account + 2000

Read-item(other-account)

Write-item(other-account)

TIME

Write-lock(my-account)

Read-item(my-account)

my-account := my-account +1000

Write-item(my-account)

Wite-lock(my-account)

Write-lock(other-account)Unlock(my-account)

Unlock(other-account)

Unlock(my-account)

Page 44: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 44

Two-phase locking protocol: 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 45: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 45

Two-phase locking protocol: Example

T1

R(A)

T2

W(C)

T3

W(A)

T4

R(A)

W(B)

R(C)W(A)

W(D)

Page 46: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 46

Two-phase locking protocol: ExampleT1 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 47: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 47

Two-phase locking protocol: Example

T1 T2 T3

write(x)

write(x)

write(y)

write(y)

Page 48: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 48

Deadlock

• Two or more transactions wait for each other to unlock some data item.

• Deadlock prevention:o Conservative 2PL protocol: Wait until you can lock all the data to be

used beforehand, o wait-die, o wound-wait, o no waiting, o cautious waiting, etc.

• Deadlock detection: o Wait-for graph, o timeouts, etc.

Page 49: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 49

Deadlock

T1

Write-lock(my-account1)

Write-lock(my-account2)

T2

Write-lock(my-account2)

Write-lock(my-account1)

TIME

Page 50: IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña jose.m.pena@liu.se

IDA / ADIT 50

Starvation

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

• Starvation prevention:o First-come-first-served waiting scheme, oro wait-die,o wound-wait, etc.