Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC-608 Database Systems

Similar presentations


Presentation on theme: "CPSC-608 Database Systems"— Presentation transcript:

1 CPSC-608 Database Systems
Fall 2018 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #37

2 Tranactions What can affect transaction ACID?
Atomicity: either all steps of a transaction happen, or none happen Consistency: a transaction transforms a consistent DB into a consistent DB Isolation: execution of a transaction is isolated from that of other transactions Durability: if a transaction commits, its effects persist. Database T1 …… Tn What can affect transaction ACID? Accident system failures may destroy Atomicity and Durability, thus Consistency; Concurrent executions of transactions (which is highly desired) may affect Isolation, thus Consistency.

3 DBMS Graduate Database lock table DDL language DDL complier file
administrator DDL complier lock table DDL language file manager logging & recovery concurrency control transaction manager database programmer index/file manager buffer manager query execution engine DML complier main memory buffers DML (query) language secondary storage (disks) DBMS Graduate Database

4 Constraint: A=B T1: Read(A) A  A+100 Write(A) Read(B) B  B+100 Write(B) T2: Read(A) A  A2 B B2 Observation. In a schedule of a collection of transactions, if for every transaction T, the actions of T are scheduled consecutively without interlacing with actions from other transactions, then surely all transactions satisfy the Isolation condition. Such a schedule is called a serial schedule. A serial schedule can be given by a permutation of the transactions that indicates the order of the execution: T1T2 · · · Tk Thus, if a schedule is “equivalent” to a serial schedule, then it ensures Isolation. (two schedules S1 and S2 are “equivalent” if starting with any initial DB state, they always result in the same DB state.) T1: Read(A) A  A+100 Write(A) Read(B) B  B+100 Write(B) T2: Read(A) A  A2 B B2

5 Serializability A schedule is serializable if it is equivalent to a serial schedule.

6 Conflict-Serializability
(A sufficient condition for serializability) Consider: … r1(*)r1(*) … swap? … w1(*)r1(*) … swap? T1: Violate T1’s logic! … r1(*)w1(*) … swap? … w1(*)w1(*) … swap? … r1(A)r2(B) … swap? … r1(A)w2(B) … … w1(A)r2(B) … … w1(A)w2(B) … Two actions conflict if either they are by the same transaction, or they are by two transactions on the same element and at least one of the actions is write T1, T2: … r1(A)r2(A) … swap? … r1(A)w2(A) … … w1(A)r2(A) … … w1(A)w2(A) …

7 Conflict-Serializability
Two actions conflict if either they are by the same transaction, or they are by two transactions on the same element and at least one of the actions is write Conflict-Serializability (A sufficient condition for serializability) Thus: Swapping two non-conflicting actions gives an equivalent schedule. A schedule S1 is conflict-serializable if it can be converted into a serial schedule by swapping non-conflicting actions. A conflict-serializable schedule is serializable. A serializable schedule may not be conflict-serializable. How do we know if a schedule is conflict-serializable?

8 Testing Conflict-Serializability
Two actions conflict if either they are by the same transaction, or they are by two transactions on the same element and at least one of the actions is write Testing Conflict-Serializability conflict-serializable Algorithm TestCS. Input: a schedule S Build a directed graph GS; Each transaction is a node in GS; If two actions a1 and a2 conflict, where a1 and a2 are by T1 and T2, resp., and a1 is before a2 in S, then add an edge from T1 to T2 in GS; S is conflict-serializable if and only if GS contains no cycle. S1: r2(A)r1(B)w2(A)r3(A)w1(B)w3(A)r2(B)w2(B) 1 2 3 S2: r2(A)r1(B)w2(A)r2(B)r3(A)w1(B)w3(A)w2(B) 1 2 3 Not conflict-serializable Why does the algorithm work? Claim: If GS is acyclic, then S is equivalent to any serial schedule obtained by topologically sorting GS. Proof. Let T1T2 … Tk be a topological order of GS. Since there is no edge into T1, for each action a in T1, there is no action from other transactions that conflict a and appears in S before a. Therefore, the actions of T1 can be all swapped with non-conflict actions and move to the front of the schedule, resulting in an equivalent schedule. Now by induction we can also convert the rest of the schedule into an equivalent serial schedule.

9 Enforcing Conflict-Serializability
Using locking protocol scheduler T1 lock table T2 Tn ……

10 Enforcing Conflict-Serializability
Using locking protocol scheduler T1 lock table T2 Tn …… Two new actions: lock: li(A) (transaction Ti exclusively locks the element A) unlock: ui(A) (transaction Ti releases the lock on the element A)

11 Enforcing Conflict-Serializability
Using locking protocol scheduler T1 lock table T2 Tn …… Two new actions: lock: li(A) (transaction Ti exclusively locks the element A) unlock: ui(A) (transaction Ti releases the lock on the element A) A transaction can lock an unlocked element, and must release (unlock) it later. A transaction cannot access a locked element until it is released by the transaction holding the lock.

12 for conflict-serializability
A transaction can lock an unlocked element, and must release (unlock) it later. A transaction cannot access a locked element until it is released by the transaction holding the lock. Locking is not enough for conflict-serializability A B 125 250 50 150 T1 Read(A); A  A+100 Write(A); Read(B); B  B+100; Write(B); T2 A  A2; B  B2; Schedule D

13 for conflict-serializability
A transaction can lock an unlocked element, and must release (unlock) it later. A transaction cannot access a locked element until it is released by the transaction holding the lock. Locking is not enough for conflict-serializability A B 125 250 50 150 T1 l1(A); Read(A); A  A+100 Write(A); u1(A); l1(B); Read(B); B  B+100; Write(B); u1(B); T2 l2(A); Read(A); A  A2; Write(A); u2(A); l2(B); Read(B); B  B2; Write(B); u2(B); Schedule D

14 for conflict-serializability
A transaction can lock an unlocked element, and must release (unlock) it later. A transaction cannot access a locked element until it is released by the transaction holding the lock. Locking is not enough for conflict-serializability A B 125 250 50 150 T1 l1(A); Read(A); A  A+100 Write(A); u1(A); l1(B); Read(B); B  B+100; Write(B); u1(B); T2 l2(A); Read(A); A  A2; Write(A); u2(A); l2(B); Read(B); B  B2; Write(B); u2(B); Schedule D Inconsistent, so isolation was destroyed

15 Two phase locking (2PL) (for transactions)
Ti: li(A)… li(B) ………… ui(B)… ui(A) ... no unlocks no locks In a transaction, all lockings precede all unlockings

16 Two phase locking (2PL) (for transactions)
Ti: li(A)… li(B) ………… ui(B)… ui(A) ... no unlocks no locks # of locks held by Ti growing phase shrinking time In a transaction, all lockings precede all unlockings

17 2PL enforces conflict-serializability
In a transaction, all lockings precede all unlockings Schedule D A B 125 250 50 150 T1 l1(A); Read(A); A  A+100 Write(A); u1(A); l1(B); Read(B); B  B+100; Write(B); u1(B); T2 l2(A); Read(A); A  A2; Write(A); u2(A); l2(B); Read(B); B  B2; Write(B); u2(B);

18 2PL enforces conflict-serializability
In a transaction, all lockings precede all unlockings Schedule D A B 125 250 50 150 T1 l1(A); Read(A); A  A+100 Write(A); u1(A); l1(B); Read(B); B  B+100; Write(B); u1(B); T2 l2(A); Read(A); A  A2; Write(A); u2(A); l2(B); Read(B); B  B2; Write(B); u2(B); not conflict-serializable even with locks

19 2PL enforces conflict-serializability
In a transaction, all lockings precede all unlockings Schedule D A B T1 l1(A); Read(A); A  A+100 Write(A); u1(A); l1(B); Read(B); B  B+100; Write(B); u1(B); T2 l2(A); Read(A); A  A2; Write(A); u2(A); l2(B); Read(B); B  B2; Write(B); u2(B); Try to apply 2PL On the transactions

20 2PL enforces conflict-serializability
In a transaction, all lockings precede all unlockings Schedule D A B T1 l1(A); Read(A); A  A+100 Write(A); u1(A); l1(B); Read(B); B  B+100; Write(B); u1(B); T2 l2(A); Read(A); A  A2; Write(A); u2(A); l2(B); Read(B); B  B2; Write(B); u2(B); Try to apply 2PL On the transactions

21 2PL enforces conflict-serializability
In a transaction, all lockings precede all unlockings Schedule D A B T1 l1(A); Read(A); A  A+100 Write(A); u1(A); l1(B); Read(B); B  B+100; u1(A); Write(B); u1(B); T2 l2(A); Read(A); A  A2; Write(A); u2(A); l2(B); Read(B); B  B2; Write(B); u2(B); Try to apply 2PL On the transactions

22 2PL enforces conflict-serializability
In a transaction, all lockings precede all unlockings Schedule D A B T1 l1(A); Read(A); A  A+100 Write(A); u1(A); l1(B); Read(B); B  B+100; u1(A); Write(B); u1(B); T2 l2(A); Read(A); A  A2; Write(A); u2(A); l2(B); Read(B); B  B2; Write(B); u2(B); u2(A); Try to apply 2PL On the transactions

23 2PL enforces conflict-serializability
In a transaction, all lockings precede all unlockings Schedule D A B 125 T1 l1(A); Read(A); A  A+100 Write(A); u1(A); l1(B); Read(B); B  B+100; u1(A); Write(B); u1(B); T2 l2(A); Read(A); A  A2; Write(A); u2(A); l2(B); Read(B); B  B2; Write(B); u2(B); u2(A);

24 2PL enforces conflict-serializability
In a transaction, all lockings precede all unlockings Schedule D A B 125 T1 l1(A); Read(A); A  A+100 Write(A); u1(A); l1(B); Read(B); B  B+100; u1(A); Write(B); u1(B); T2 l2(A); Read(A); A  A2; Write(A); u2(A); l2(B); Read(B); B  B2; Write(B); u2(B); u2(A); blocked, so delayed

25 2PL enforces conflict-serializability
In a transaction, all lockings precede all unlockings Schedule D A B 125 T1 l1(A); Read(A); A  A+100 Write(A); u1(A); l1(B); Read(B); B  B+100; u1(A); Write(B); u1(B); T2 l2(A); Read(A); A  A2; Write(A); u2(A); l2(B); Read(B); B  B2; Write(B); u2(B); u2(A); blocked, so delayed

26 2PL enforces conflict-serializability
In a transaction, all lockings precede all unlockings Schedule D A B 125 T1 l1(A); Read(A); A  A+100 Write(A); u1(A); l1(B); Read(B); B  B+100; u1(A); Write(B); u1(B); T2 l2(A); Read(A); A  A2; Write(A); u2(A); l2(B); Read(B); B  B2; Write(B); u2(B); u2(A); blocked, so delayed

27 2PL enforces conflict-serializability
In a transaction, all lockings precede all unlockings Schedule D A B 125 T1 l1(A); Read(A); A  A+100 Write(A); u1(A); l1(B); Read(B); B  B+100; u1(A); Write(B); u1(B); T2 l2(A); Read(A); A  A2; Write(A); u2(A); l2(B); Read(B); B  B2; Write(B); u2(B); u2(A);

28 2PL enforces conflict-serializability
In a transaction, all lockings precede all unlockings Schedule D A B 125 250 T1 l1(A); Read(A); A  A+100 Write(A); u1(A); l1(B); Read(B); B  B+100; u1(A); Write(B); u1(B); T2 l2(A); Read(A); A  A2; Write(A); u2(A); l2(B); Read(B); B  B2; Write(B); u2(B); u2(A);

29 2PL enforces conflict-serializability
In a transaction, all lockings precede all unlockings Schedule D A B 125 250 T1 l1(A); Read(A); A  A+100 Write(A); u1(A); l1(B); Read(B); B  B+100; u1(A); Write(B); u1(B); T2 l2(A); Read(A); A  A2; Write(A); u2(A); l2(B); Read(B); B  B2; Write(B); u2(B); u2(A); blocked, so delayed

30 2PL enforces conflict-serializability
In a transaction, all lockings precede all unlockings Schedule D A B 125 250 T1 l1(A); Read(A); A  A+100 Write(A); u1(A); l1(B); Read(B); B  B+100; u1(A); Write(B); u1(B); T2 l2(A); Read(A); A  A2; Write(A); u2(A); l2(B); Read(B); B  B2; Write(B); u2(B); u2(A); blocked, so delayed

31 2PL enforces conflict-serializability
In a transaction, all lockings precede all unlockings Schedule D A B 125 250 T1 l1(A); Read(A); A  A+100 Write(A); u1(A); l1(B); Read(B); B  B+100; u1(A); Write(B); u1(B); T2 l2(A); Read(A); A  A2; Write(A); u2(A); l2(B); Read(B); B  B2; Write(B); u2(B); u2(A); blocked, so delayed

32 2PL enforces conflict-serializability
In a transaction, all lockings precede all unlockings Schedule D A B 125 250 T1 l1(A); Read(A); A  A+100 Write(A); u1(A); l1(B); Read(B); B  B+100; u1(A); Write(B); u1(B); T2 l2(A); Read(A); A  A2; Write(A); u2(A); l2(B); Read(B); B  B2; Write(B); u2(B); u2(A);

33 2PL enforces conflict-serializability
In a transaction, all lockings precede all unlockings Schedule D A B 125 250 T1 l1(A); Read(A); A  A+100 Write(A); u1(A); l1(B); Read(B); B  B+100; u1(A); Write(B); u1(B); T2 l2(A); Read(A); A  A2; Write(A); u2(A); l2(B); Read(B); B  B2; Write(B); u2(B); u2(A);

34 2PL enforces conflict-serializability
In a transaction, all lockings precede all unlockings Schedule D A B 125 250 T1 l1(A); Read(A); A  A+100 Write(A); u1(A); l1(B); Read(B); B  B+100; u1(A); Write(B); u1(B); T2 l2(A); Read(A); A  A2; Write(A); u2(A); l2(B); Read(B); B  B2; Write(B); u2(B); u2(A); This execution of Schedule D is: r1(A)w1(A)r1(B)r2(A)w2(A)w1(B)r2(B)w2(B),

35 2PL enforces conflict-serializability
In a transaction, all lockings precede all unlockings Schedule D A B 125 250 T1 l1(A); Read(A); A  A+100 Write(A); u1(A); l1(B); Read(B); B  B+100; u1(A); Write(B); u1(B); T2 l2(A); Read(A); A  A2; Write(A); u2(A); l2(B); Read(B); B  B2; Write(B); u2(B); u2(A); This execution of Schedule D is: r1(A)w1(A)r1(B)r2(A)w2(A)w1(B)r2(B)w2(B),

36 2PL enforces conflict-serializability
In a transaction, all lockings precede all unlockings Schedule D A B 125 250 T1 l1(A); Read(A); A  A+100 Write(A); u1(A); l1(B); Read(B); B  B+100; u1(A); Write(B); u1(B); T2 l2(A); Read(A); A  A2; Write(A); u2(A); l2(B); Read(B); B  B2; Write(B); u2(B); u2(A); This execution of Schedule D is: r1(A)w1(A)r1(B)r2(A)w2(A)w1(B)r2(B)w2(B), equivalent to the serial schedule: r1(A)w1(A)r1(B)w1(B)r2(A)w2(A)r2(B)w2(B).

37 Why does 2PL work?

38 Two actions conflict if … they are by two transactions on the same element and at least one of the actions is write Why does 2PL work? Theorem. The execution of a 2PL schedule is equivalent to the serial schedule in which transactions are ordered by the time when they start to unlock.

39 Two actions conflict if … they are by two transactions on the same element and at least one of the actions is write Why does 2PL work? Theorem. The execution of a 2PL schedule is equivalent to the serial schedule in which transactions are ordered by the time when they start to unlock. Proof. Let S be a 2PL schedule in which transaction T1 starts its unlocks the latest. Let a be any action of T1. Suppose that there is an action a’ by another transactions Tk that conflicts a and appears after a in the schedule S. Thus, S = …… a …… a’ ……

40 Two actions conflict if … they are by two transactions on the same element and at least one of the actions is write Why does 2PL work? Theorem. The execution of a 2PL schedule is equivalent to the serial schedule in which transactions are ordered by the time when they start to unlock. Proof. Let S be a 2PL schedule in which transaction T1 starts its unlocks the latest. Let a be any action of T1. Suppose that there is an action a’ by another transactions Tk that conflicts a and appears after a in the schedule S. Thus, S = …… a …… a’ …… Since a and a’ conflict, they are on the same element A. Both T1 and Tk need to lock A before their corresponding actions a and a’ can be executed. Since a appears in S before a’, T1 must lock A before Tk does. So the schedule S looks like S = … l1(A) … a … lk (A) … a’…

41 Two actions conflict if … they are by two transactions on the same element and at least one of the actions is write Why does 2PL work? Theorem. The execution of a 2PL schedule is equivalent to the serial schedule in which transactions are ordered by the time when they start to unlock. Proof. Let S be a 2PL schedule in which transaction T1 starts its unlocks the latest. Let a be any action of T1. Suppose that there is an action a’ by another transactions Tk that conflicts a and appears after a in the schedule S. Thus, S = …… a …… a’ …… Since a and a’ conflict, they are on the same element A. Both T1 and Tk need to lock A before their corresponding actions a and a’ can be executed. Since a appears in S before a’, T1 must lock A before Tk does. So the schedule S looks like S = … l1(A) … a … lk (A) … a’… Tk cannot lock A before T1 unlocks A. So S = … l1(A) … a … u1(A) … lk (A) … a’… . Since S is 2PL, the first unlock of Tk must appear after lk (A) , thus after u1(A). But this contradicts the assumption that T1 starts its unlock the latest.

42 Two actions conflict if … they are by two transactions on the same element and at least one of the actions is write Why does 2PL work? Theorem. The execution of a 2PL schedule is equivalent to the serial schedule in which transactions are ordered by the time when they start to unlock. Proof. Let S be a 2PL schedule in which transaction T1 starts its unlocks the latest. Let a be any action of T1. Suppose that there is an action a’ by another transactions Tk that conflicts a and appears after a in the schedule S. Thus, S = …… a …… a’ …… Since a and a’ conflict, they are on the same element A. Both T1 and Tk need to lock A before their corresponding actions a and a’ can be executed. Since a appears in S before a’, T1 must lock A before Tk does. So the schedule S looks like S = … l1(A) … a … lk (A) … a’… Tk cannot lock A before T1 unlocks A. So S = … l1(A) … a … u1(A) … lk (A) … a’… . Since S is 2PL, the first unlock of Tk must appear after lk (A) , thus after u1(A). But this contradicts the assumption that T1 starts its unlock the latest. Thus, in the schedule S, there is no action by transactions other than T1 that conflicts an action of T1 and appears after the conflicting action of T1. So, we can swap the actions in S to move all actions of T1 to the tail of the schedule. The new schedule has a form S1 = α1T1. The new schedule S1 is equivalent to S.

43 Two actions conflict if … they are by two transactions on the same element and at least one of the actions is write Why does 2PL work? Theorem. The execution of a 2PL schedule is equivalent to the serial schedule in which transactions are ordered by the time when they start to unlock. Proof. Let S be a 2PL schedule in which transaction T1 starts its unlocks the latest. Let a be any action of T1. Suppose that there is an action a’ by another transactions Tk that conflicts a and appears after a in the schedule S. Thus, S = …… a …… a’ …… Since a and a’ conflict, they are on the same element A. Both T1 and Tk need to lock A before their corresponding actions a and a’ can be executed. Since a appears in S before a’, T1 must lock A before Tk does. So the schedule S looks like S = … l1(A) … a … lk (A) … a’… Tk cannot lock A before T1 unlocks A. So S = … l1(A) … a … u1(A) … lk (A) … a’… . Since S is 2PL, the first unlock of Tk must appear after lk (A) , thus after u1(A). But this contradicts the assumption that T1 starts its unlock the latest. Thus, in the schedule S, there is no action by transactions other than T1 that conflicts an action of T1 and appears after the conflicting action of T1. So, we can swap the actions in S to move all actions of T1 to the tail of the schedule. The new schedule has a form S1 = α1T1. The new schedule S1 is equivalent to S. Using the same argument, we can move all actions of the transaction T2 that started its unlocks the second latest, to construct a schedule of the form S2 = α2T2T1 that is also equivalent to S. Repeating this process, we will eventually get a serial schedule that is equivalent to S. ■

44 Containment Relations
Serializable schedules 2PL serial Conflict-serializable schedules Serializable schedule: equivalent to a serial schedule Conflict-serializable schedule: can be swapped into a serial schedule 2PL: transactions locks before any unlock. Serial schedule: transactions are executed one after the other

45 Containment Relations
Serializable schedules 2PL serial Conflict-serializable schedules Serializable schedule: equivalent to a serial schedule Conflict-serializable schedule: can be swapped into a serial schedule 2PL: transactions locks before any unlock. Serial schedule: transactions are executed one after the other

46 Containment Relations
Serializable schedules 2PL serial Conflict-serializable schedules Serializable schedule: equivalent to a serial schedule Conflict-serializable schedule: can be swapped into a serial schedule 2PL: transactions locks before any unlock. Serial schedule: transactions are executed one after the other

47 Containment Relations
Serializable schedules 2PL serial Conflict-serializable schedules Serializable schedule: equivalent to a serial schedule Conflict-serializable schedule: can be swapped into a serial schedule 2PL: transactions locks before any unlock. Serial schedule: transactions are executed one after the other

48 Containment Relations
Serializable schedules 2PL serial Conflict-serializable schedules Serializable schedule: equivalent to a serial schedule Conflict-serializable schedule: can be swapped into a serial schedule 2PL: transactions locks before any unlock. Serial schedule: transactions are executed one after the other

49 Beyond the simple 2PL protocol, it is all a matter of improving performance and allowing more concurrency …….


Download ppt "CPSC-608 Database Systems"

Similar presentations


Ads by Google