0% found this document useful (0 votes)
46 views

Adms CH-4

This document discusses different concurrency control techniques used in database systems. It describes three basic techniques: locking methods, timestamp ordering methods, and optimistic concurrency control schemes. For locking methods, it provides details on lock types (shared vs exclusive), two-phase locking protocol, and problems that can arise like deadlocks. It also covers timestamp ordering methods and how they use timestamps to serialize transaction execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Adms CH-4

This document discusses different concurrency control techniques used in database systems. It describes three basic techniques: locking methods, timestamp ordering methods, and optimistic concurrency control schemes. For locking methods, it provides details on lock types (shared vs exclusive), two-phase locking protocol, and problems that can arise like deadlocks. It also covers timestamp ordering methods and how they use timestamps to serialize transaction execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

DVANCED DATABASE SYSTEM

Chapter:4

Concurrency Controlling Techniques


INTRODUCTION TO CONCURRENCY CONTROL
Concurrency Control is the process of managing simultaneous
operations on the database without having them interfere with one
another. Prevents interference when two or more users are accessing
database simultaneously and at least one is updating data.

Although two transactions may be correct in themselves, interleaving


of operations may produce an incorrect result.
INTRODUCTION TO CONCURRENCY CONTROL

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
techniques to ensure atomicity, isolation, and serializability of
concurrent transactions.
Three basic concurrency control techniques:

1. Locking methods concurrency control techniques

2. Time stamping methods concurrency control techniques

3. Optimistic (Validation) Concurrency Control Schemes


1. Concurrency control using Locking Based methods

❖The locking method is a mechanism for preventing simultaneous access on a


shared resource for a critical operation

❖ A LOCK is a mechanism for enforcing limits on access to a resource in an


environment where there are many threads of execution. Locks are one way of
enforcing concurrency control policies.

❖Transaction uses locks to deny access to other transactions and so prevent incorrect
updates.

❖Lock prevents another transaction from modifying item or even reading it, in the
case of a write lock/ exclusive lock
A lock is a mechanism to control concurrent access to a data item
Locking is an operation which secures
(a) permission to Read
(b) permission to Write a data item for a transaction.
Notation :
:Li(X) –Transaction Ti requests a lock on database element X
• Unlocking is an operation which removes these permissions from
the data item.
• Notation :
• Ui (X): Transaction Ti releases (“unlocks”) its lock on
database element X
Lock and Unlock are Atomic operations.
Two-phase locking policy generates two locking algorithms
Basic
 Conservative
Basic:
Transaction locks data items incrementally. This may cause
deadlock
Conservative:
Prevents deadlock by locking all desired data items before
transaction begins execution.
Cont.…

❖Lock (X): If a transaction T1 applies Lock on data item X, then X is


locked and it is not available to any other transaction.

❖Unlock (X): T1 Unlocks X. X is available to other transactions.

Types of a Lock
1. Shared lock/ Read lock
2. Exclusive lock/ write lock
Cont..

1. Shared lock/Read Lock: A Read operation does not change the value of
a data item. Hence a data item can be read by two different transactions
simultaneously under share lock mode. So only to read a data item
T1will do: Share lock (X), then Read (X), and finally Unlock (X

2. Exclusive lock/write lock: A write operation changes the value of the


data item. Hence two write operations from two different transactions or
a write from T1 and a read from T2 are not allowed. A data item can be
modified only under Exclusive lock. To modify a data item T1 will do:
Exclusive lock (X), then Write (X) and finally Unlock (X).
Cont..
When these locks are applied, then a transaction must behave in a
special way. This special behavior of a transaction is referred to as
well-formed.

A transaction is well-formed if it does not lock a locked data item


and it does not try to unlock an unlocked data item
Locking - Basic Rules

 If transaction has a shared lock on an item, it can read but not update the item.

If a transaction has an exclusive lock on an item, it can both read and update the item.

 Reads operation cannot form a conflict, so that more than one transaction can hold
shared locks simultaneously on same item.

Exclusive lock gives transaction exclusive right to access that item.

 Some systems allow transaction to upgrade a shared lock to an exclusive lock, or


vice-versa
Examples: T1 and T2 are two transactions. They are executed under
locking as follows. T1 locks 'A' in exclusive mode. When T2 wants to
lock 'A', it finds it is locked by T1 so T2 waits for Unlock on 'A' by
T1. When 'A' is released then T2 locks 'A' and begins execution.

Suppose a lock on a data item is applied, the data item is processed and
it is unlocked immediately after reading/writing is completed as
follows.

-> Initial values of A = 10 and B = 20.


The final result of the two transactions using the two types of transaction
execution (serial and concurrent) is not the same. This indicates that the
above method of locking and unlocking is not correct.

This is because although such kind of locking and unlocking data items
increases the concurrency of execution it violates the isolation and
atomicity properties of transactions.

Immediate unlocking is not trustworthy. Thus, to preserve consistency


we have to use another approach to locking, the following scheme helps us
to overcome that issue
Cont..

Generally there are three types of lock protocols available for lock based concurrency control

methods:

1) Simple locking Protocol

2) Pre - claiming Lock Protocol

3) Two phase locking protocol

1. Simplistic Lock Protocol

Simplistic lock-based protocols allow transactions to obtain a lock on every object before a 'write'

operation is performed. Transactions may unlock the data item after completing the 'write' operation.

Lock item before access and unlock item when access is completed.
2. Pre-claiming protocols evaluate their operations and create a list of
data items on which they need locks. Before initiating an execution,
the transaction requests the system for all the locks it needs
beforehand.

If all the locks are granted, the transaction executes and releases all
the 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
3. Two - Phase Locking - 2PL- protocol

This locking protocol divides the execution phase of a transaction into


three parts.

In the first part, when the transaction starts executing, it seeks
permission for the locks it requires.

The second part is where the transaction acquires all the locks. As soon
as the transaction releases its first lock,

 the third part starts. In this part, the transaction cannot demand any
new locks; it only releases the acquired locks
A transaction is said to follow two phase locking protocol if all
locking operations (either read_lock or write_lock) precede the first
unlock operation in the transaction

This is a protocol which ensures conflict-serializable schedules.

It can be proved that the transactions can be serialized in the order of
their lock points (i.e. the point where a transaction acquired its final
lock).
Two-phase locking has two phases,

Phase 1: Growing Phase

transaction may obtain locks

growing phase, where all the locks are being acquired by the transaction

transaction may not release locks

Phase 2: Shrinking Phase

transaction may release locks

shrinking phase, where the locks held by the transaction are being released.

transaction may not obtain locks

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

to an exclusive lock
Problem of locking methods

1. Deadlock

Deadlock that may result when two (or more) transactions are each
waiting for locks held by the other to be released.

In a multi-process system, deadlock is an unwanted situation that


arises in a shared resource environment, where a process indefinitely
waits for a resource that is held by another process
For example, assume a set of transactions {T0, T1, T2, ...,Tn}. T0 needs
a resource X to complete its task. Resource X is held by T1, and T1 is
waiting for a resource Y, which is held by T2. T2 is waiting for resource Z,
which is held by T0.

Thus, all the processes wait for each other to release resources. In this
situation, none of the processes can finish their task. This situation is
known as a deadlock.

The transactions involved in the deadlock are either rolled back or


restarted
Two general techniques for handling deadlock:
1. Deadlock prevention.
2. Deadlock detection and recovery

Deadlock Prevention To prevent any deadlock situation in the


system, the DBMS aggressively inspects all the operations, where
transactions are about to execute.

The DBMS inspects the operations and analyzes if they can create a
deadlock situation. If it finds that a deadlock situation might occur,
then that transaction is never allowed to be executed.
There are deadlock prevention schemes that use timestamp
ordering mechanism of transactions in order to predetermine a
deadlock situation

Deadlock Avoidance: Aborting a transaction is not always a


practical approach. Instead, deadlock avoidance mechanisms can
be used to detect any deadlock situation in advance.
2. Concurrency control based on Timestamp ordering
• Timestamp (TS)
Time stamp is a unique identifier created by the DBMS to identify a transaction
A larger timestamp value indicates a more recent event or operation
Timestamp based algorithm uses timestamp to serialize the execution of concurrent
transactions
• Time stamp ordering algorithm associates two time stamp values (TS) with each
database item X
1. Read_TS(x) : the read time stamp of x: This is the largest timestamp among all the
timestamps of transactions that have successfully read item x
• Read_TS(X)=TS(T) where T is the youngest transaction that has read X successfully
2. Write_TS(X) : This is the largest of all the timestamps of transactions
that have successfully written item x – that is, write_TS(x) = TS(T),
where T is the youngest transaction that has written x successfully

• Basic Timestamp Ordering (TO): whenever some transaction T


tries to issue a read_item(X) or a write_item(X) operation, the basic
TO algorithm compares the timestamp of T with read_TS(X) and
write_TS(X)
2. Concurrency control based on Timestamp ordering

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
Time Stamp Based Methods

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
Cont.…
The timestamp-ordering protocol ensures serializability among
transactions in their conflicting read and write operations.

This is the responsibility of the Time stamp ordering protocol that the
conflicting pair of tasks should be executed according to the
timestamp values of the transactions.
The timestamp of transaction Ti is denoted by TS(Ti).
Read timestamp of data-item X is denoted by R-timestamp(X).
Write timestamp of data-item X is denoted by W-timestamp(X)
Timestamp ordering protocol works as follows:

If a transaction Ti issues a read(X) operation:

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

 Operation rejected.

 If TS(Ti) >= W-timestamp(X)

 Operation executed.

All data-item timestamps updated.


Timestamp ordering protocol works as follows:

If a transaction Ti issues a write(X) operation:

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

 Operation rejected.

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

 Operation rejected and Ti rolled back.

Otherwise, operation executed


4. Optimistic (Validation) Concurrency Control Schemes

In this technique, serializability is checked only at the time of commit and transactions
are aborted in case of non-serializable schedules

No checking is done while transaction is executing

In this scheme, updates in the transaction are not applied directly to the database item
until it reaches its commit point

• Three phases:

1. Read phase
2. Validation phase
3. Write phase
1. Read phase:
• A transaction can read values of committed data items. However,
updates are applied only to local copies (versions) of the data
items (in database cache)

2. Validation phase: Serializability is checked before transactions


write their updates to the database.

3. Write phase: On a successful validation transactions’ updates are


applied to the database; otherwise, transactions are restarted
THE END
Assignment 1
Special/ multimedia/ mobile database
Qiuze 1
List out the types of lock protocols (5%)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy