Presentation is loading. Please wait.

Presentation is loading. Please wait.

IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña

Similar presentations


Presentation on theme: "IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña"— Presentation transcript:

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

2 IDA / ADIT 2 How can several users access and update the database at the same time ? Real world Model Database system Physical database Database management system Processing of Queries/updates Access to stored data

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

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

5 IDA / ADIT 5 Transactions: Example T1T2 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)

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

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

8 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 ?

9 IDA / ADIT 9 Schedule: Example T1T2 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

10 IDA / ADIT 10 Lost update problem T1T2 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

11 IDA / ADIT 11 Dirty read problem T1T2 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

12 IDA / ADIT 12 Incorrect summary problem T1T2 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-account2sum := 0

13 IDA / ADIT 13 Unrepeatable read problem T1T2 Read-item(my-account) my-account:= my-account + 1000 TIME Read-item(my-account) Write-item(my-account)

14 IDA / ADIT 14 Properties for transactions 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.

15 IDA / ADIT 15 Properties for transactions 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.

16 IDA / ADIT 16 Serial schedule: Definition 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.

17 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

18 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

19 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

20 IDA / ADIT 20 Non-serial schedule: Example T1T2 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

21 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

22 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

23 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

24 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

25 IDA / ADIT 25 Non-serializable schedule: Example T1T2 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!

26 IDA / ADIT 26 Conflict: Definition Conflict: Definition Two operations are in conflict if o they belong to different transactions, o they access (read/write) the same data item X, and o one of the operations is a Write-item(X).

27 IDA / ADIT 27 Conflict equivalence: Definition 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.

28 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

29 IDA / ADIT 29 Non-serializable schedule: Example T1T2 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

30 IDA / ADIT 30 Checking if a schedule is serializable Checking if a schedule is serializable Create the following directed graph: 1. Create a node for each transaction in the schedule. 2. If transation T j executes a Read-item(X) after transaction T i executes a Write-item(X), then create an arch T i  T j. 3. If transation T j executes a Write-item(X) after transaction T i executes a Read-item(X), then create an arch T i  T j. 4. If transation T j executes a Write-item(X) after transaction T i executes a Write-item(X), then create an arch T i  T j. The schedule is serializable if and only if the graph above does not contain any directed cycle.

31 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

32 IDA / ADIT 32 Non-serializable schedule: Example T1T2 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

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

34 IDA / ADIT 34 (Non-)Serializable schedule: Example T1 R(A) T2 T3 W(A) T4 R(A) W(A)

35 IDA / ADIT 35 Can we make sure that we only get serializable schedules? Yes. Key question

36 IDA / ADIT 36 Locks: Definition 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.

37 IDA / ADIT 37 Locks: Usage 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.

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

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

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

41 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

42 IDA / ADIT 42 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 Write-lock(my-account) Write-lock(other-account)???

43 IDA / ADIT 43 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) 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)

44 IDA / ADIT 44 Two-phase locking protocol: Example T1T2 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

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

46 IDA / ADIT 46 Two-phase locking protocol: Example T1 T2 T3 read(x) x:=x+1 write(x) read(x) x:=x+1 write(x) read(x) x:=x+1 write(x) read(y) y:=y+1 write(y) read(y) y:=y+1 write(y)

47 IDA / ADIT 47 Two-phase locking protocol: Example T1 T2 T3 write(x) write(y)

48 IDA / ADIT 48 Deadlock 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.

49 IDA / ADIT 49 Deadlock T1 Write-lock(my-account1) Write-lock(my-account2) T2 Write-lock(my-account2) Write-lock(my-account1) TIME

50 IDA / ADIT 50 Starvation 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, or o wait-die, o wound-wait, etc.


Download ppt "IDA / ADIT Lecture 9: Transactions and concurrency control Jose M. Peña"

Similar presentations


Ads by Google