Presentation is loading. Please wait.

Presentation is loading. Please wait.

Concurrency control. Lock-based protocols One way to ensure serializability is to require the data items be accessed in a mutually exclusive manner One.

Similar presentations


Presentation on theme: "Concurrency control. Lock-based protocols One way to ensure serializability is to require the data items be accessed in a mutually exclusive manner One."— Presentation transcript:

1 Concurrency control

2 Lock-based protocols One way to ensure serializability is to require the data items be accessed in a mutually exclusive manner One transaction is accessing a data item, no other transaction can modify that data item. Common method is “locking”

3 Locks Lock mode Shared Locked Lock mode in read operation If a transaction T i has obtains a shared-mode lock on item Q, then T i can read, but cannot write, Q. Exclusive Locked Lock mode in write operation If a transaction T i has obtains an exclusive- mode lock on item Q, then T i can both read and write Q.

4 Every transaction request a lock in an appropriate mode on data item Q, depending on types of operations it will perform on Q When transaction T i requests a lock mode X on item Q, which is locked by T j, if the lock mode is in compatible mode, T i can be granted a lock on Q Compatible mode can be presented in the following figure

5 Lock compatibility matrix SX- STrueFalseTrue XFalse True - Lock compatibility if A and B represent lock modes. if T j hold a lock mode B on item Q And T i requests a lock A-mode on item Q If Ti can be granted a lock on Q We say mode A is compatible with mode B True mean A Compatible with B

6 Access data To access data item, transaction must lock the item first. Lock-S(Q) for share lock Lock-X(Q) for excusive lock Unlock(Q) for unlock data If data already lock by another transaction in an incompatible mode, the concurrency control manager will not grant the lock until it was released. The Ti is made to wait state.

7 Two phase locking techniques Technique to control concurrent execution of transaction Based on “LOCKING” 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 Two-phase locking theorem If all transactions obey the two-phase locking protocol, then all possible interleaved schedules are serializable. Eswaran et al., “The notions of consistency and predicate Locks in a Data base system,” CACM 19, No. 11 (November 1976)

9 The two phase locking protocol  allows a transaction to lock a new data item only if that transaction has not yet unlocked any data item.  The protocol ensures serializability, but not deadlock freedom.  In absence of information concerning the manner in which data items are accessed, the two-phase locking protocol is both necessary and sufficient.

10 Concurrency and lock

11 Lost update problem T1T2 Lock_S(A) Read_item(A) A := A – 50 Lock_S(A) Read_item(A) temp := 0.1*A A:= A-temp Lock_X(A) Wait Wait Lock_X(A) Wait T1T2 Read_item(A) A := A – 50 Read_item(A) temp := 0.1*A A:= A-temp Write_item(A) Read_item(B) Write_item(A) Read_item(B) B := B + 50 Write_item(B) B := B + temp Write_item(B

12 Temporary update problem T1T2 - Write_item(R) Read_item(R) - - Rollback T1T2 -LOCK_X(R) Write_item(R) LOCK_S(R) Wait Wait Rollback UNLOCK(R) LOCK_S(R) READ_ITEM(R)

13 Inconsistency problem T1 T2 Read_item(A) SUM = Sum+A Read_item(B) SUM = A + B Read_item(C) C = C - 10 Write_item(C) Read_item(A) A = A + 10 Write_item(A) COMMIT Read_item(C) SUM = SUM + C T1 T2 LOCK_S(A) Read_item(A) SUM = Sum+ALOCK_S(B) Read_item(B) SUM = A + B LOCK_X(C) Read_item(C) C = C – 10 Write_item(C) LOCK_S(A) Read_item(A) A = A + 10 LOCK_X(A) Wait LOCK_R(C)

14 Dead Lock Two-phase locking protocol can be used to solve the three basic concurrency problems. DEADLOCK But locking introduce new problems, “DEADLOCK” Deadlock is situation in which two or more transactions are in a simultaneous wait state, each of them waiting for one of the others to release a lock before it can be proceed.

15 How the system detect and break deadlock? Detecting the deadlock involves detecting a cycle in the Wait-For Graph, which is graph of “who is waiting for whom?” Breaking the dead lock involves choosing one of the deadlocked transactions as the victim and rolling it back (releasing locks and allowing some other transaction to proceed. Some system use timeout mechanism and simply assume that a transaction that has done no work for some prescribed period of time is deadlock. For the victim, some systems automatically restart such a transaction from the beginning, on the assumption that the conditions that caused deadlock will probably not arise again Some system may send “deadlock victim: exception code back to the application

16 Deadlock avoidance Wait-Dei and Wound-Wait No waiting and cautions waiting Algorithm

17 Deadlock avoidance Wait-Dei and Wound-Wait comes in two version Propose in the context of a distributed system, but could be used in a centralized system as well ( 1980, J.N. Gray: “Experience with the system R Lock Manager” ) comes in two version (Wait-die and Wound-wait) work as follows: Every transaction is timestamped with its start time (which must be unique) When transaction A requests a lock on a tuple that is already locked by transaction B, then Wait-Die: A waits if it is older than B; otherwise, it “dies”- that is, A is rolled back and restarted. Wound-Wait: A waits if it is younger than B; otherwise, it “wounds” B – that is, B is rolled back and restarted. If transaction has to be restarted, it retains its original timestamp.

18 T i older than T j ( TS(T i ) < TS(T j )) TS = timestamp, which is a unique identifier assigned to each transaction. TS based on the order, which transactions are started. Wait-die If ( TS( T i ) < TS( T j ) ) then ( T i older than T j ) T i allowed to wait Else Abort T i (rollback and restart later) End if Wound-wait If ( TS( T i ) < TS( T j ) ) then ( T i older than T j ) Abort T j (T i wound T j ) (rollback and restart later) Else T i allowed to wait End if

19 No waiting algorithm If transaction is unable to obtain a lock, it is immediately aborted and then restarted after a certain time delay without checking whether a deadlock will actually occur or not.

20 Cautions waiting algorithm Caution waiting algorithm was proposed to try to reduce the number of needlessly abort/restarts. If T i tries to lock an item X but X is locked by T j with conflicting lock. The cautions waiting rule as follows: If T j is not blocked (not waiting for some locked item) then T i is blocked and allowed to wait; otherwise Abort T i

21 T1 T2 Lock-x(B) Read(B) B:=B-50 Write(B) lock-S(A) read(A) lock-s(B) read(B) lock-X(A) A = 1000, B= 2000 Dead lock: One transaction already lock and another one try to lock the same data item in incompatible mode, thus in wait state

22 Locking based protocol Lock-based protocol is a set of rule to indicate when the transaction may lock or unlock each data item

23 Two phase locking techniques Technique to control concurrent execution of transaction Based on “LOCKING” 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

24 Two-phase locking theorem If all transactions obey the two-phase locking protocol, then all possible interleaved schedules are serializable. Eswaran et al., “The notions of consistency and predicate Locks in a Data base system,” CACM 19, No. 11 (November 1976)

25 T1 T2 Lock-x(B) Read(B) B:=B-50 Write(B) Unlock(B) lock-S(A) read(A) unlock(A) lock-s(B) read(B) unlock(B) display(A+B) lock-X(A) Read(A) A:=A+50 Write(A) Unlock(A) A = 1000, B= 2000 In this case, result A+B incorrect because T1 unlock B early Not 2 phase locking Because has unlock during lock

26 Example T1: lock-X(B) Read(B) B:=B-50 Write(B) Unlock(B) Lock-X(A) Read(A) A:=A+50 Write(A) Unlock(A) T2: lock-S(A) Read(A) Unlock(A) Lock-S(B) Read(B) Unlock(B) Transition T2

27 reschedule T1’: lock-X(B) Read(B) Write(B) B:=B-50 Lock-X(A) Unlock(B) Read(A) A:=A+50 Write(A) Unlock(A) T2’: lock-S(A) Read(A) Lock-S(B) Unlock(A) Read(B) Unlock(B) T1’ and T2’ which are the same as T1 and T2 schedule but which follow the 2 phase Locking protocol. They can produce dead lock T2: lock-S(A) Read(A) Unlock(A) Lock-S(B) Read(B) Unlock(B) T1: lock-X(B) Read(B) B:=B-50 Write(B) Unlock(B) Lock-X(A) Read(A) A:=A+50 Write(A) Unlock(A)

28 T1 T2concurrency control manager Lock-x(B) grant-X(B,T1) Read(B) B:=B-50 Write(B) Unlock(B) lock-S(A) grant-S(A,T2) read(A) unlock(A) lock-s(B) grant-S(B,T2) read(B) unlock(B) display(A+B) lock-X(A) grant-X(A,T2) Read(A) A:=A+50 Write(A) Unlock(A) A = 1000, B= 2000 2000 1950 1000 1950 1950+1000 = 2950 1050 In this case, result A+B incorrect because T1 unlock B early


Download ppt "Concurrency control. Lock-based protocols One way to ensure serializability is to require the data items be accessed in a mutually exclusive manner One."

Similar presentations


Ads by Google