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

Slides:



Advertisements
Similar presentations
Database System Concepts 5 th Ed. © Silberschatz, Korth and Sudarshan, 2005 See for conditions on re-usewww.db-book.com Chapter 16 : Concurrency.
Advertisements

Database Systems (資料庫系統)
Concurrency Control WXES 2103 Database. Content Concurrency Problems Concurrency Control Concurrency Control Approaches.
Chapter 16 Concurrency. Topics in this Chapter Three Concurrency Problems Locking Deadlock Serializability Isolation Levels Intent Locking Dropping ACID.
Concurrency Control II
Lock-Based Concurrency Control
Concurrency Control.
Lecture 11 Recoverability. 2 Serializability identifies schedules that maintain database consistency, assuming no transaction fails. Could also examine.
CS 728 Advanced Database Systems Chapter 21 Introduction to Protocols for Concurrency Control in Databases.
Sekolah Tinggi Ilmu Statistik (STIS) 1 Dr. Said Mirza Pahlevi, M.Eng.
Transaction Overview and Concurrency Control Chapters 16 & 17.
Quick Review of Apr 29 material
©Silberschatz, Korth and Sudarshan16.1Database System Concepts 3 rd Edition Chapter 16: Concurrency Control Lock-Based Protocols Timestamp-Based Protocols.
Concurrency Control R&G - Chapter 17 Smile, it is the key that fits the lock of everybody's heart. Anthony J. D'Angelo, The College Blue Book.
CS 582 / CMPE 481 Distributed Systems Concurrency Control.
Concurrency Control. Example Schedules Constraint: The sum of A+B must be the same Before: After: T1 read(A) A = A -50 write(A) read(B)
Transaction Management and Concurrency Control
©Silberschatz, Korth and Sudarshan16.1Database System Concepts 3 rd Edition Chapter 16: Concurrency Control Lock-Based Protocols Timestamp-Based Protocols.
Transaction Management
1 Transaction Management Database recovery Concurrency control.
©Silberschatz, Korth and Sudarshan16.1Database System Concepts 3 rd Edition Chapter 16: Concurrency Control Lock-Based Protocols Timestamp-Based Protocols.
CS4432transaction management1 CS4432: Database Systems II Lecture #23 Transaction Management Professor Elke A. Rundensteiner.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Transaction Management and Concurrency Control.
Concurrency Control. General Overview Relational model - SQL  Formal & commercial query languages Functional Dependencies Normalization Transaction Processing.
DBMS2001Notes 9: Transaction Processing1 Principles of Database Management Systems 9: More on Transaction Processing Pekka Kilpeläinen (Partially based.
© 1997 UW CSE 11/13/97N-1 Concurrency Control Chapter 18.1, 18.2, 18.5, 18.7.
Chapter 181 Chapter 18: Concurrency Control (Slides by Hector Garcia-Molina,
15.1Database System Concepts - 6 th Edition Chapter 15: Concurrency Control Lock-Based Protocols The Two-Phase Locking Protocol Graph-Based Protocols #Deadlock.
Concurrency Control.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 10 Transaction Management.
Databases Illuminated
PMIT-6102 Advanced Database Systems
Concurrency Control Lectured by, Jesmin Akhter, Assistant professor, IIT, JU.
Chapter 11 Concurrency Control. Lock-Based Protocols  A lock is a mechanism to control concurrent access to a data item  Data items can be locked in.
CONCURRENCY CONTROL (CHAPTER 16) CONCURRENCY CONTROL INTRODUCTION Motivation: A dbms is multiprogrammed to increase the utilization.
Transactions CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems by Connolly & Begg, © Addison Wesley 2002)
Concurrency Control Techniques Chapter 18
Chapter 15 Concurrency Control Yonsei University 1 st Semester, 2015 Sanghyun Park.
Concurrency Control Concurrency Control By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany) DIRECTOR ARUNAI ENGINEERING COLLEGE TIRUVANNAMALAI.
TRANSACTION MANAGEMENT R.SARAVANAKUAMR. S.NAVEEN..
Chapter 18 Concurrency Control Techniques Copyright © 2004 Pearson Education, Inc.
Concurrency Chapter 6.2 V3.1 Napier University Dr Gordon Russell.
Page 1 Concurrency Control Paul Krzyzanowski Distributed Systems Except as otherwise noted, the content of this presentation.
Concurrency Control Introduction Lock-Based Protocols
1 Concurrency control lock-base protocols timestamp-based protocols validation-based protocols Ioan Despi.
Lecture 8- Concurrency Control Advanced Databases Masood Niazi Torshiz Islamic Azad university- Mashhad Branch
Chapter 8 Concurrency Control 8.1 Lock-Based Protocols 8.2 Multiple Granularity 8.3 Deadlock Handling 8.4 Insert and Delete Operations.
9 1 Chapter 9_B Concurrency Control Database Systems: Design, Implementation, and Management, Rob and Coronel.
Concurrency Control The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems, 4th edition,
10 1 Chapter 10_B Concurrency Control Database Systems: Design, Implementation, and Management, Rob and Coronel.
Em Spatiotemporal Database Laboratory Pusan National University File Processing : Concurrency Control 2004, Spring Pusan National University Ki-Joune Li.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 15 : Concurrency.
Chapter 13 Managing Transactions and Concurrency Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition.
Transaction Management and Concurrency Control
Concurrency Control.
Multiple Granularity Granularity is the size of data item  allowed to lock. Multiple Granularity is the hierarchically breaking up the database into portions.
Part- A Transaction Management
Chapter 16: Concurrency Control
Unit :- 1 Concurrency Control.
Chapter 16: Concurrency Control
Anthony D. Joseph and Ion Stoica
CONCURRENCY CONTROL (CHAPTER 16)
Concurrency.
Chapter 10 Transaction Management and Concurrency Control
CS162 Operating Systems and Systems Programming Review (II)
Concurrency Control WXES 2103 Database.
Chapter 15 : Concurrency Control
Introduction of Week 13 Return assignment 11-1 and 3-1-5
CONCURRENCY Concurrency is the tendency for different tasks to happen at the same time in a system ( mostly interacting with each other ) .   Parallel.
Lecture 18: Concurrency Control
Presentation transcript:

Concurrency control

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”

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.

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

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

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.

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

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)

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.

Concurrency and lock

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

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)

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)

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.

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

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

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.

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

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.

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

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

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

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

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)

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

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

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)

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= = In this case, result A+B incorrect because T1 unlock B early