DBMS - Concurrency ControlDBMS - Concurrency...

5
4/7/2020 DBMS - Concurrency Control - Tutorialspoint https://www.tutorialspoint.com/dbms/dbms_concurrency_control.htm 1/5 DBMS - Concurrency Control DBMS - Concurrency Control In a multiprogramming environment where multiple transactions can be In a multiprogramming environment where multiple transactions can be executed simultaneously, it is highly important to control the executed simultaneously, it is highly important to control the concurrency of transactions. We have concurrency control protocols to concurrency of transactions. We have concurrency control protocols to ensure atomicity, isolation, and serializability of concurrent transactions. ensure atomicity, isolation, and serializability of concurrent transactions. Concurrency control protocols can be broadly divided into two Concurrency control protocols can be broadly divided into two categories − categories − Lock based protocols Lock based protocols Time stamp based protocols Time stamp based protocols Lock-based Protocols Lock-based Protocols Database systems equipped with lock-based protocols use a Database systems equipped with lock-based protocols use a mechanism by which any transaction cannot read or write data until it mechanism by which any transaction cannot read or write data until it acquires an appropriate lock on it. Locks are of two kinds − acquires an appropriate lock on it. Locks are of two kinds − Binary Locks Binary Locks − A lock on a data item can be in two states; it − A lock on a data item can be in two states; it is either locked or unlocked. is either locked or unlocked. Shared/exclusive Shared/exclusive This type of locking mechanism This type of locking mechanism differentiates the locks based on their uses. If a lock is differentiates the locks based on their uses. If a lock is acquired on a data item to perform a write operation, it is an acquired on a data item to perform a write operation, it is an exclusive lock. Allowing more than one transaction to write on exclusive lock. Allowing more than one transaction to write on the same data item would lead the database into an the same data item would lead the database into an inconsistent state. Read locks are shared because no data inconsistent state. Read locks are shared because no data value is being changed. value is being changed. There are four types of lock protocols available − There are four types of lock protocols available − Simplistic Lock Protocol Simplistic Lock Protocol

Transcript of DBMS - Concurrency ControlDBMS - Concurrency...

Page 1: DBMS - Concurrency ControlDBMS - Concurrency Controluietkanpur.com/Online_Course/DV_CSE_S511_1.pdf · 4/7/2020 DBMS - Concurrency Control - Tutorialspoint ... TTwo-phase locking has

4/7/2020 DBMS - Concurrency Control - Tutorialspoint

https://www.tutorialspoint.com/dbms/dbms_concurrency_control.htm 1/5

DBMS - Concurrency ControlDBMS - Concurrency Control

In a multiprogramming environment where multiple transactions can beIn a multiprogramming environment where multiple transactions can beexecuted simultaneously, it is highly important to control theexecuted simultaneously, it is highly important to control theconcurrency of transactions. We have concurrency control protocols toconcurrency of transactions. We have concurrency control protocols toensure atomicity, isolation, and serializability of concurrent transactions.ensure atomicity, isolation, and serializability of concurrent transactions.Concurrency control protocols can be broadly divided into twoConcurrency control protocols can be broadly divided into twocategories −categories −

Lock based protocolsLock based protocols

Time stamp based protocolsTime stamp based protocols

Lock-based ProtocolsLock-based Protocols

Database systems equipped with lock-based protocols use aDatabase systems equipped with lock-based protocols use amechanism by which any transaction cannot read or write data until itmechanism by which any transaction cannot read or write data until itacquires an appropriate lock on it. Locks are of two kinds −acquires an appropriate lock on it. Locks are of two kinds −

Binary LocksBinary Locks − A lock on a data item can be in two states; it − A lock on a data item can be in two states; itis either locked or unlocked.is either locked or unlocked.

Shared/exclusiveShared/exclusive − This type of locking mechanism − This type of locking mechanismdifferentiates the locks based on their uses. If a lock isdifferentiates the locks based on their uses. If a lock isacquired on a data item to perform a write operation, it is anacquired on a data item to perform a write operation, it is anexclusive lock. Allowing more than one transaction to write onexclusive lock. Allowing more than one transaction to write onthe same data item would lead the database into anthe same data item would lead the database into aninconsistent state. Read locks are shared because no datainconsistent state. Read locks are shared because no datavalue is being changed.value is being changed.

There are four types of lock protocols available −There are four types of lock protocols available −

Simplistic Lock ProtocolSimplistic Lock Protocol

Page 2: DBMS - Concurrency ControlDBMS - Concurrency Controluietkanpur.com/Online_Course/DV_CSE_S511_1.pdf · 4/7/2020 DBMS - Concurrency Control - Tutorialspoint ... TTwo-phase locking has

4/7/2020 DBMS - Concurrency Control - Tutorialspoint

https://www.tutorialspoint.com/dbms/dbms_concurrency_control.htm 2/5

Simplistic lock-based protocols allow transactions to obtain a lock onSimplistic lock-based protocols allow transactions to obtain a lock onevery object before a 'write' operation is performed. Transactions mayevery object before a 'write' operation is performed. Transactions mayunlock the data item after completing the ‘write’ operation.unlock the data item after completing the ‘write’ operation.

Pre-claiming Lock ProtocolPre-claiming Lock Protocol

Pre-claiming protocols evaluate their operations and create a list ofPre-claiming protocols evaluate their operations and create a list ofdata items on which they need locks. Before initiating an execution, thedata items on which they need locks. Before initiating an execution, thetransaction requests the system for all the locks it needs beforehand. Iftransaction requests the system for all the locks it needs beforehand. Ifall the locks are granted, the transaction executes and releases all theall the locks are granted, the transaction executes and releases all thelocks when all its operations are over. If all the locks are not granted,locks when all its operations are over. If all the locks are not granted,the transaction rolls back and waits until all the locks are granted.the transaction rolls back and waits until all the locks are granted.

Two-Phase Locking 2PLTwo-Phase Locking 2PL

This locking protocol divides the execution phase of a transaction intoThis locking protocol divides the execution phase of a transaction intothree parts. In the first part, when the transaction starts executing, itthree parts. In the first part, when the transaction starts executing, itseeks permission for the locks it requires. The second part is where theseeks permission for the locks it requires. The second part is where thetransaction acquires all the locks. As soon as the transaction releasestransaction acquires all the locks. As soon as the transaction releasesits first lock, the third phase starts. In this phase, the transaction cannotits first lock, the third phase starts. In this phase, the transaction cannotdemand any new locks; it only releases the acquired locks.demand any new locks; it only releases the acquired locks.

Page 3: DBMS - Concurrency ControlDBMS - Concurrency Controluietkanpur.com/Online_Course/DV_CSE_S511_1.pdf · 4/7/2020 DBMS - Concurrency Control - Tutorialspoint ... TTwo-phase locking has

4/7/2020 DBMS - Concurrency Control - Tutorialspoint

https://www.tutorialspoint.com/dbms/dbms_concurrency_control.htm 3/5

Two-phase locking has two phases, one is Two-phase locking has two phases, one is growinggrowing, where all the locks, where all the locksare being acquired by the transaction; and the second phase isare being acquired by the transaction; and the second phase isshrinking, where the locks held by the transaction are being released.shrinking, where the locks held by the transaction are being released.

To claim an exclusive (write) lock, a transaction must first acquire aTo claim an exclusive (write) lock, a transaction must first acquire ashared (read) lock and then upgrade it to an exclusive lock.shared (read) lock and then upgrade it to an exclusive lock.

Strict Two-Phase LockingStrict Two-Phase Locking

The first phase of Strict-2PL is same as 2PL. After acquiring all theThe first phase of Strict-2PL is same as 2PL. After acquiring all thelocks in the first phase, the transaction continues to execute normally.locks in the first phase, the transaction continues to execute normally.But in contrast to 2PL, Strict-2PL does not release a lock after using it.But in contrast to 2PL, Strict-2PL does not release a lock after using it.Strict-2PL holds all the locks until the commit point and releases all theStrict-2PL holds all the locks until the commit point and releases all thelocks at a time.locks at a time.

Strict-2PL does not have cascading abort as 2PL does.Strict-2PL does not have cascading abort as 2PL does.

Page 4: DBMS - Concurrency ControlDBMS - Concurrency Controluietkanpur.com/Online_Course/DV_CSE_S511_1.pdf · 4/7/2020 DBMS - Concurrency Control - Tutorialspoint ... TTwo-phase locking has

4/7/2020 DBMS - Concurrency Control - Tutorialspoint

https://www.tutorialspoint.com/dbms/dbms_concurrency_control.htm 4/5

Timestamp-based ProtocolsTimestamp-based Protocols

The most commonly used concurrency protocol is the timestamp basedThe most commonly used concurrency protocol is the timestamp basedprotocol. This protocol uses either system time or logical counter as aprotocol. This protocol uses either system time or logical counter as atimestamp.timestamp.

Lock-based protocols manage the order between the conflicting pairsLock-based protocols manage the order between the conflicting pairsamong transactions at the time of execution, whereas timestamp-basedamong transactions at the time of execution, whereas timestamp-basedprotocols start working as soon as a transaction is created.protocols start working as soon as a transaction is created.

Every transaction has a timestamp associated with it, and the orderingEvery transaction has a timestamp associated with it, and the orderingis determined by the age of the transaction. A transaction created atis determined by the age of the transaction. A transaction created at0002 clock time would be older than all other transactions that come0002 clock time would be older than all other transactions that comeafter it. For example, any transaction 'y' entering the system at 0004 isafter it. For example, any transaction 'y' entering the system at 0004 istwo seconds younger and the priority would be given to the older one.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-In addition, every data item is given the latest read and write-timestamp. This lets the system know when the last ‘read and write’timestamp. This lets the system know when the last ‘read and write’operation was performed on the data item.operation was performed on the data item.

Timestamp Ordering ProtocolTimestamp Ordering Protocol

The timestamp-ordering protocol ensures serializability amongThe timestamp-ordering protocol ensures serializability amongtransactions in their conflicting read and write operations. This is thetransactions in their conflicting read and write operations. This is theresponsibility of the protocol system that the conflicting pair of tasksresponsibility of the protocol system that the conflicting pair of tasksshould be executed according to the timestamp values of theshould be executed according to the timestamp values of thetransactions.transactions.

The timestamp of transaction TThe timestamp of transaction T is denoted as TS(T is denoted as TS(T ).).

Read time-stamp of data-item X is denoted by R-timestamp(X).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).Write time-stamp of data-item X is denoted by W-timestamp(X).

Timestamp ordering protocol works as follows −Timestamp ordering protocol works as follows −

If a transaction Ti issues a read(X) operation −If a transaction Ti issues a read(X) operation −

If TS(Ti) < W-timestamp(X)If TS(Ti) < W-timestamp(X)Operation rejected.Operation rejected.

ii ii

Page 5: DBMS - Concurrency ControlDBMS - Concurrency Controluietkanpur.com/Online_Course/DV_CSE_S511_1.pdf · 4/7/2020 DBMS - Concurrency Control - Tutorialspoint ... TTwo-phase locking has

4/7/2020 DBMS - Concurrency Control - Tutorialspoint

https://www.tutorialspoint.com/dbms/dbms_concurrency_control.htm 5/5

If TS(Ti) >= W-timestamp(X)If TS(Ti) >= W-timestamp(X)Operation executed.Operation executed.

All data-item timestamps updated.All data-item timestamps updated.

If a transaction Ti issues a write(X) operation −If a transaction Ti issues a write(X) operation −

If TS(Ti) < R-timestamp(X)If TS(Ti) < R-timestamp(X)

Operation rejected.Operation rejected.

If TS(Ti) < W-timestamp(X)If TS(Ti) < W-timestamp(X)

Operation rejected and Ti rolled back.Operation rejected and Ti rolled back.

Otherwise, operation executed.Otherwise, operation executed.

Thomas' Write RuleThomas' Write Rule

This rule states if TS(Ti) < W-timestamp(X), then the operation isThis rule states if TS(Ti) < W-timestamp(X), then the operation isrejected and Trejected and T is rolled back. is rolled back.

Time-stamp ordering rules can be modified to make the schedule viewTime-stamp ordering rules can be modified to make the schedule viewserializable.serializable.

Instead of making TInstead of making T rolled back, the 'write' operation itself is ignored. rolled back, the 'write' operation itself is ignored.

ii

ii