Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 Concurrency Control 8.1 Lock-Based Protocols 8.2 Multiple Granularity 8.3 Deadlock Handling 8.4 Insert and Delete Operations.

Similar presentations


Presentation on theme: "Chapter 8 Concurrency Control 8.1 Lock-Based Protocols 8.2 Multiple Granularity 8.3 Deadlock Handling 8.4 Insert and Delete Operations."— Presentation transcript:

1 Chapter 8 Concurrency Control 8.1 Lock-Based Protocols 8.2 Multiple Granularity 8.3 Deadlock Handling 8.4 Insert and Delete Operations

2 8.1 Lock-Based Protocols One way to ensure serializability is to require that data items be accessed in a mutually exclusive manner.(lock) 1. Shared: if a transaction Ti has obtained a shared-mode lock on item Q, then Ti can read, but cannot write Q. 2. Exculsive: if a transaction T i has obtained an exclusive-mode lock on item Q, then T i can both read and write Q. SX Struefalse X Compatibility function:

3 8.1 Lock-Based Protocols T 1 lock-X(B) read(B) B:=B-50 write(B) unlock(B) lock-X(A) read(A) A:=A+50 write(A) unlock(A) T 2 lock-S(A) read(A) unlock(A) lock-S(B) read(B) unlock(B) display(A+B) concurrency-control manager grant-X(B,T 1 ) grant-S(A,T 2 ) grant-S(B,T 2 ) grant-X(A,T 1 ) lock-X(B) read(B) B:=B-50 write(B) unlock(B) lock-X(A) read(A) A:=A+50 write(A) unlock(A) T1:T1: lock-S(A) read(A) unlock(A) lock-S(B) read(B) unlock(B) display(A+B) T2:T2:

4 8.1 Lock-Based Protocols lock-X(B) read(B) B:=B-50 write(B) lock-X(A) read(A) A:=A+50 write(A) unlock(B) unlock(A) T3:T3: lock-S(A) read(A) lock-S(B) read(B) unlock(A) unlock(B) display(A+B) T4:T4: T 3 lock-X(B) read(B) B:=B-50 write(B) lock-X(A) T 4 lock-S(A) read(A) lock-S(B) deadlock

5 8.1 Lock-Based Protocols When a transaction requests a lock on a data item in a particular node, and no other transaction has a lock on the same data item in a conflicting mode, the lock can be granted. T 1 lock-X(A) wait …. T 2 lock-S(A) A:=A+50 unlock(A) T 3 lock-S(A) A:=A+10 unlock(A) T 4 lock-S(A) A:=A-20 unlock(A) T i lock-S(A) …… … starvation

6 One protocol that ensures serializability is the two- phase locking protocol. This protocol requires that each transaction issue lock and unlock requests in two phases: ① Growing phase. A transaction may obtain locks, but may not release any lock. ② Shrinking phase. A transaction may release locks, but may not obtain any new locks. 8.1.3 The Two-Phase Locking Protocol

7 T 1 lock-S(B) read(B) lock-X(A) A:=B+1 write(A) unlock(B) unlock(A) T 2 lock-S(A) wait lock-S(A) read(A) lock-X(B) B:=A+1 write(B) unlock(B) unlock(A) T 3 lock-S(B) read(B) unlock(B) lock-X(A) A:=B+1 write(A) unlock(A) T 4 lock-S(A) wait lock-S(A) read(A) unlock(A) lock-X(B) B:=A+1 write(B) unlock(B) growing phase shrinking phase lock point T 3 、 T 4 Serializable Not two phase T 1 、 T 2 Serializable two phase  The two- phase locking protocol ensures conflict serializabilit y

8 8.1.3 The Two-Phase Locking Protocol T 3 lock-X(B) read(B) B:=B-50 write(B) lock-X(A) T 4 lock-S(A) read(A) lock-S(B) deadlock T 3 、 T 4 two phase  Two-phase locking does not ensure freedom from deadlock. T 5 lock-X(A) read(A) lock-S(B) read(B) write(A) unlock(A) T 6 lock-S(A) read(A) write(A) unlock(A) T 7 lock-S(A) read(A) T 5 、 T 6 、 T 7 two phase  Cascadi ng rollback may occur under two- phase locking.

9 Strict two-phase locking protocol: This protocol requires not only that locking be two phase, but also that all exclusive-mode locks taken by a transaction be held until that transaction commits. Rigorous two-phase locking protocol: This protocol requires that all locks be held until the transaction commits. 8.1.3 The Two-Phase Locking Protocol

10 read(a 1 ); read(a 2 ); … read(a n ); write(a 1 ); T8:T8: read(a1); read(a2); display(A+B) T9:T9: T 8 lock-S(a 1 ) lock-S(a 2 ) lock-S(a 3 ) lock-S(a 4 ) lock-S(a n ) upgrade(a 1 ) T 9 lock-S(a 1 ) lock-S(a 2 ) unlock(a 1 ) unlock(a 2 ) lock conversion 8.1.3 The Two-Phase Locking Protocol  Upgrade: share to exclusive mode Upgrading can take place in only the growing phase  Downgrade: exclusive to share mode downgrading can take place in only the shrinking phase

11  When a transaction Ti issues a read(Q) operation, the system issues a lock-S(Q) instruction followed by the read(Q) instruction.  When Ti issues a write (Q) operation, the system checks to see whether Ti already holds a shared lock on Q. if it does, then the system issues an up-grade (Q) instruction, followed by the write (Q) instruction. Otherwise, the system issues a lock-X (Q) instruction, followed by the write (Q) instruction.  All locks obtained by a transaction are unlocked after that transaction commits or aborts. 8.1.3 The Two-Phase Locking Protocol

12 Tree protocol: the only lock instruction allowed is lock-X. each transaction T i can lock a data item at most once, and must observe the following rules: ① The first lock by Ti may be on any data item. ② Subsequently, a data item Q can be locked by T i only if the parent of Q is currently locked by T i. ③ Data items may be unlocked at any time. ④ A data item that has been locked and unlocked by Ti cannot subsequently be relocked by T i. 8.1.4 Graph-Based Protocols

13 Lock and unlock instructions: T 10 : lock-X(B); lock-X(E); lock-X(D); unlock(B); unlock(E); lock-X(G); unlock(D); unlock(G); T 11 : lock-X(D); lock-X(H); unlock(D); unlock(H); T 12 : lock-X(B); lock-X(E); unlock(E); unlock(B); T 13 : lock-X(D); lock-X(H); unlock(D); unlock(H); 8.1.4 Graph-Based Protocols A BC F E I D GH J

14 T 10 lock-X(B) lock-X(E) lock-X(D) unlock(B) unlock(E) lock-X(G) unlock(D) unlock(G) T 11 lock-X(D) lock-X(H) unlock(D) unlock(H) T 12 lock-X(B) lock-X(E) unlock(E) unlock(B) T 13 lock-X(D) lock-X(H) unlock(D) unlock(H) A BC F E I D GH J  Tree protocol ensures conflict serializability and freedom from deadlock  Tree protocol does not ensure recoverability and cascadelessness.

15  Advantages: ① unlike two-phase locking, it is deadlock-free, so no rollbacks are required.unlocking may occur earlier. ② Earlier unlocking may lead to shorter waiting times, and to an increase in concurrency.  Disadvantages: ① in some cases, a transaction may have to lock data items that it does not access. ② Without prior knowledge of what data items will need to be locked, transactions will have to lock the root of the tree, and that can reduce concurrency greatly. 8.1.4 Graph-Based Protocols

16  A method for determining the serializability order is to select an ordering among transactions in advance.(timestamp-ordering scheme)  Timestamps: with each transaction T i in the system, we associate a unique fixed timestamp, denoted by TS(T i ). This timestamp is assigned by the database system before the transaction T i starts execution. If a transaction T i has been assigned timestamp TS(T i ), and a new transaction T j enters the system, then TS(T i )< TS(T j ). There are two simple methods for implementing this scheme: ① Use the value of the system clock as the timestamp; that is, a transaction’s timestamp is equal to the value of the clock when the transaction enters the system. ② Use a logical counter that is incremented after a new timestamp has been assigned; that is, a transaction’s timestamp is equal to the value of the counter when the transaction enters the system. 8.2 Timestamp-Based Protocols

17 Timestamp-ordering protocol: 1.Suppose that transaction Ti issues read(Q). a. If TS(T i )<W-timestamp(Q), then Ti needs to read a value of Q that was already overwritten. Hence, the read operation is rejected, and T i is rolled back. b. If TS(T i )>=W-timestamp(Q) then the read operation is executed, and R-timestamp(Q) is set to the maximum of R- timestamp(Q) and TS(T i ). 8.2 Timestamp-Based Protocols

18 2.Suppose that transaction T i issues write(Q). a. If TS(T i )<R-timestamp(Q), then the value of that T i is producing was needed previously, and the system assumed that value would never be produced. Hence, the system rejects the write operation and rolls T i back. b. If TS(T i )<W-timestamp(Q) then T i is attempting to write an obsolete value of Q. hence, the system rejects this write operation and rolls T i back. c. Otherwise, the system executes the write operation and sets W -timestamp(Q) to TS(T i ) 8.2 Timestamp-Based Protocols

19 read(B); read(A); display(A+B); T 14 : read(B); B:=B-50; write(B); read(A); A:=A+50; write(A); display(A+B); T 15 : T 14 read(B) read(A) display(A+B) T 15 read(B) B:=B-50 write(B) read(A) A:=A+50 write(A) display(A+B) R-timestamp(B)= TS(T 15 ) W-timestamp(B)= TS(T 15 ) TS(T 14 )< TS(T 15 ) R-timestamp(A)= TS(T 15 )

20 8.4 Multiple Granularity ISIXSSIXX IStrue false IXtrue false Struefalsetruefalse SIXtruefalse X

21  A system is in a deadlock state if there exists a set of transactions such that every transaction in the set is waiting for another transaction in the set.  There two principal methods for dealing with the deadlock problem: ① deadlock prevention ② deadlock detection&deadlock recovery 8.4 Deadlock Handling

22 There are two approaches to deadlock prevention: ① one approach ensures that no cyclic waits can occur by ordering the requests for locks, or requiring all locks to be acquired together. Disadvantages: a. It is often hard to predict, before the transaction begins,what data items need to be locked. b. Data-item utilization may be very low. ② the other approach is closer to deadlock recovery, an performs transaction rollback instead of waiting for a lock, whenever the wait could potentially result in a deadlock. 8.4.1 Deadlock Prevention

23 1.The wait-die scheme is a nonpreemptive technique. When transaction T i requests a data item currently held by T j, T i is allowed to wait only if it has a timestamp smaller than that of T j (that is, T i is older than T j ). Otherwise, T i is rolled back(dies). 8.4.1 Deadlock Prevention timestamp T 22 T 23 T 24 51015 T 22 lock-S(A) wait T 23 lock-X(A) T 24 lock-X(A) rollback T 3 lock-X(B) read(B) write(B) lock-X(A) T 4 lock-S(A) read(A) lock-S(B) Timestamp: T 3 <T 4 T 4 rollback

24 2. The wound-wait scheme is a primitive technique. It is a counterpart to the wait-die scheme. When transaction T i requests a data item currently held by T j, T i is allowed to wait only if it has a timestamp larger than that of T j (that is, T i is younger than T j ). Otherwise, T j is rolled back(T j is wounded by T i ). 8.4.1 Deadlock Prevention timestamp T 22 T 23 T 24 51015 T 22 lock-S(A) T 23 lock-X(A) rollback T 24 lock-X(A) wait T 3 lock-X(B) read(B) write(B) lock-X(A) T 4 lock-S(A) read(A) lock-S(B) Timestamp: T 3 <T 4 T 4 rollback

25 Similarity: both the wound-wait and the wait-die schemes avoid starvation. Difference: operation ① In the wait-die scheme, the older the transaction gets, the more it tends to wait. By contrast, in the wound-wait scheme, an older transaction never waits for a younger transaction. ② In the wait-die scheme, if a transaction T i dies and is rolled back because it requested a data item held by transaction T j, the T i may reissue the same sequence of requests when it is restarted. If the data item is still held by T j, then T i will die again. Thus, T i may die several times before acquiring the needed data item.contrast this series of events with what happens in the wound-wait scheme. There may be fewer rollbacks in the wound-wait scheme. 8.4.1 Deadlock Prevention

26 Lock timeouts: a transaction that has requested a lock waits for at most a specified amount of time. If the lock has not been granted within that time, the transaction is said to timeout,and it rolls itself back and restarts. If there was in fact a deadlock, one or more transactions involved in the deadlock will time out and roll back, allowing the others to proceed. 8.4.2 Timeout-Based Scheme

27 Deadlock Detection: wait-for graph:G=(V,E) V:is a set of vertices E:is a set of edges T i →T j : transaction T i is waiting for transaction T j to release a data item that it needs. 8.4.3 Deadlock Detection and Recovery T 25 T 27 T 28 T 26 T 28 T 25 T 27 T 26 Transaction T 28 is requesting an item held by T 27. Deadlock: contains a cycle

28 Recovery from Deadlock: roll back ① selection of a victim: Given a set of deadlocked transactions, we must determine which transaction to rollback to break the deadlock.(minimum cost) ② Rollback: Once we have decided that a particular transaction must be rolled back, we must determine how far this transaction should be rolled back.(total rollback, partial rollback) ③ Starvation: We must ensure that transaction can be picked as a victim only a finite number of times.(include the number of rollbacks in the cost factor) 8.4.3 Deadlock Detection and Recovery


Download ppt "Chapter 8 Concurrency Control 8.1 Lock-Based Protocols 8.2 Multiple Granularity 8.3 Deadlock Handling 8.4 Insert and Delete Operations."

Similar presentations


Ads by Google