Presentation is loading. Please wait.

Presentation is loading. Please wait.

Concurrency control.

Similar presentations


Presentation on theme: "Concurrency control."— Presentation transcript:

1 Concurrency control

2 In a multiprogramming environment where multiple transactions can be executed simultaneously, it is highly important to control the concurrency of transactions. We have concurrency control protocols to ensure atomicity, isolation, and serializability of concurrent transactions. Concurrency control protocols can be broadly divided into two categories − Lock based protocols Time stamp based protocols

3 Lock-based Protocols A lock is nothing but a mechanism that tells the DBMS whether a particular data item is being used by any transaction for read/write purpose. Since there are two types of operations, i.e. read and write, whose basic nature are different, the locks for read and write operation may behave differently. Database systems equipped with lock-based protocols use a mechanism by which any transaction cannot read or write data until it acquires an appropriate lock on it. Locks are of two kinds − Binary Locks − A lock on a data item can be in two states; it is either locked or unlocked. Shared/exclusive − This type of locking mechanism differentiates the locks based on their uses. If a lock is acquired on a data item to perform a write operation, it is an exclusive lock. Allowing more than one transaction to write on the same data item would lead the database into an inconsistent state. Read locks are shared because no data value is being changed.

4 The relationship between Shared and Exclusive Lock can be represented by the following table which is known as Lock Matrix. The simple rule for locking can be derived from here. If a transaction is reading the content of a sharable data item, then any number of other processes can be allowed to read the content of the same data item. But if any transaction is writing into a sharable data item, then no other transaction will be allowed to read or write that same data item. Shared Exclusive TRUE FALSE

5 Lock-X (A); (Exclusive Lock, we want to both read A’s value and modify it) Read A; A = A – 100; Write A; Unlock (A); (Unlocking A after the modification is done) Lock-X (B); (Exclusive Lock, we want to both read B’s value and modify it) Read B; B = B + 100; Write B; Unlock (B); (Unlocking B after the modification is done)

6 Lock-S (A); (Shared Lock, we only want to read A’s value) Read A; Temp = A * 0.1; Unlock (A); (Unlocking A) Lock-X (C); (Exclusive Lock, we want to both read C’s value and modify it) Read C; C = C + Temp; Write C; Unlock (C); (Unlocking C after the modification is done)

7 Timestamp-based Protocols
The most commonly used concurrency protocol is the timestamp based protocol. This protocol uses either system time or logical counter as a timestamp. Lock-based protocols manage the order between the conflicting pairs among transactions at the time of execution, whereas timestamp-based protocols start working as soon as a transaction is created. Every transaction has a timestamp associated with it, and the ordering is determined by the age of the transaction. A transaction created at 0002 clock time would be older than all other transactions that come after it. For example, any transaction 'y' entering the system at 0004 is two seconds younger and the priority would be given to the older one. In addition, every data item is given the latest read and write-timestamp. This lets the system know when the last ‘read and write’ operation was performed on the data item.

8 Timestamp Ordering Protocol
The timestamp-ordering protocol ensures serializability among transactions in their conflicting read and write operations. This is the responsibility of the protocol system that the conflicting pair of tasks should be executed according to the timestamp values of the transactions. The timestamp of transaction Ti is denoted as TS(Ti). Read time-stamp of data-item X is denoted by R-timestamp(X). Write time-stamp of data-item X is denoted by W-timestamp(X).

9 For Read operations: If TS (T) < W-timestamp (Q)
Operation rejected. then the transaction T is trying to read a value of data item Q which has already been overwritten by some other transaction. Hence the value which T wanted to read from Q does not exist there anymore, and T would be rolled back. If TS (T) >= W-timestamp (Q), then the transaction T is trying to read a value of data item Q which has been written and committed by some other transaction earlier. Hence T will be allowed to read the value of Q, and the R-timestamp of Q should be updated to TS (T). Operation executed. All data-item timestamps updated.

10 For Write operations: If TS (T) < R-timestamp (Q),
then it means that the system has waited too long for transaction T to write its value, and the delay has become so great that it has allowed another transaction to read the old value of data item Q. In such a case T has lost its relevance and will be rolled back. Else if TS (T) < W-timestamp (Q), then transaction T has delayed so much that  the system has allowed another transaction to write into the data item Q. in such a case too, T has lost its relevance and will be rolled back. Otherwise the system executes transaction T and updates the W-timestamp of Q to TS (T).


Download ppt "Concurrency control."

Similar presentations


Ads by Google