0% found this document useful (0 votes)
52 views12 pages

1) Concurrency Control: (1) Lost Update Problem: This Problem Occurs When Two Transactions That

Important question

Uploaded by

bhuvanam417
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views12 pages

1) Concurrency Control: (1) Lost Update Problem: This Problem Occurs When Two Transactions That

Important question

Uploaded by

bhuvanam417
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

1)Concurrency Control

• One of the fundamental properties of a transaction is isolation.


• When several transactions execute concurrently in the database, however, the
isolation property may no longer be preserved.
• A database can have multiple transactions running at the same time. This is
called concurrency.
• To preserve the isolation property, the system must control the interaction
among the concurrent transactions; this control is achieved through one of a
variety of mechanisms called concurrency control schemes.
• Definition of concurrency control: A mechanism which ensures that
simultaneous execution of more than one transactions does not lead to any
database inconsistencies is called concurrency control mechanism.
• The concurrency control can be achieved with the help of various protocols
such as - lock based protocol, Deadlock handling, Multiple Granularity,
Timestamp based protocol, and validation based protocols.
Need for Concurrency
Following are the purposes of concurrency control -
• To ensure isolation
• To resolve read-write or write-write conflicts
• To preserve consistency of database
• Concurrent execution of transactions over shared database creates several data
integrity and consistency problems - these are
(1) Lost update problem: This problem occurs when two transactions that
access the same database items have their operations interleaved in a way that
makes the value of some database item incorrect.
For example - Consider following transactions
(1) Salary of Employee is read during transaction T1.
(2) Salary of Employee is read by another transaction T2.
(3) During transaction T1, the salary is incremented by 200
(4) During transaction T2, the salary is incremented by 500
The result of the above sequence is that the update made by transaction T1 is
completely lost. Therefor this problem is called as lost update problem.
(2) Dirty read or Uncommited read problem: The dirty read is a situation in
which one transaction reads the data immediately after the write operation of
previous transaction

For example - Consider following transactions -


Assume initially salary is = 1000

(1) At the time t1, the transaction T2 updates the salary to 1200
(2) This salary is read at time t2 by transaction T1. Obviously it is 1200
(3) But at the time t3, the transaction T2 performs Rollback by undoing the
changes made by T1 and T2 at time t1 and t2.
(3) Non-repeatable read problem
This problem is also known as inconsistent analysis problem. This problem
occurs when a particular transaction sees two different values for the same row
within its lifetime. For example-

(4) Phantom read problem


The phantom read problem is a special case of non repeatable read problem.
This is a problem in which one of the transaction makes the changes in the
database system and due to these changes another transaction can not read the
data item which it has read just recently. For example -
2)Locking Protocols
• One of the method to ensure the isolation property in transactions is to require
that data items be accessed in a mutually exclusive manner. That means, while
one transaction is accessing a data item, no other transaction can modify that
data item.
• The most common method used to implement this requirement is to allow a
transaction to access a data item only if it is currently holding a lock on that
item.
• Thus the lock on the operation is required to ensure the isolation of
transaction.

Simple Lock Based Protocol


• Concept of Protocol: The lock based protocol is a mechanism in which there is
exclusive use of locks on the data item for current transaction.
• Types of Locks: There are two types of locks used -

i) Shared Lock: The shared lock is used for reading data items only. It is
denoted by Lock-S. This is also called as read lock.
ii) Exclusive Lock: The exclusive lock is used for both read and write
operations. It is denoted as Lock-X. This is also called as write lock.
• The compatibility matrix is used while working on set of locks. The
concurrency control manager checks the compatibility matrix before granting
the lock. If the two modes of transactions are compatible to each other then only
the lock will be granted.
• In a set of locks may consists of shared or exclusive locks. Following matrix
represents the compatibility between modes of locks.
Here T stands for True and F stands for False. If the control manager get the
compatibility mode as True then it grant the lock otherwise the lock will be
denied.
• For example: If the transaction T1 is holding a shared lock in data item A,
then the no control manager can grant the shared lock to transaction T2 as
compatibility is True.
But it cannot grant the exclusive lock as the compatibility is false. In simple
words if transaction T1 is reading a data item A then same data item A can be
read by another transaction T2 but cannot be written by another transaction.
• Similarly if an exclusive lock (i.e. lock for read and write operations) is hold
on the data item in some transaction then no other transaction can acquire Share
or exclusive lock as the compatibility function denotes F. That means of some
transaction is writing a data item A then another transaction can not read or
write that data item A.

Hence the rule of thumb is


i) Any number of transactions can hold shared lock on an item.
ii) But exclusive lock can be hold by only one transaction.
• Example of a schedule denoting shared and exclusive locks: Consider
following schedule in which initially A=100. We deduct 50 from A in T,
transaction and Read the data item A in transaction T2. The scenario can be
represented with the help of locks and concurrency control manager as follows:
Two Phase Locking
i) Growing phase (Locking phase): It is a phase in which the transaction may
obtain locks but does not release any lock.
ii) Shrinking phase (Unlocking phase): It is a phase in which the transaction
may release the locks but does not obtain any new lock.

3)Deadlock Handling
Deadlock is a specific concurrency problem in which two transactions depend
on each other for something.
For example- Consider that transaction T1 holds a lock on some rows of table A
and needs to update some rows in the B table. Simultaneously, transaction T2
holds locks on some rows in the B table and needs to update the rows in the A
table held by Transaction T1.

Now, the main problem arises. Now Transaction T1 is waiting for T2 to release
its lock and similarly, transaction T2 is waiting for T1 to release its lock. All
activities come to a halt state and remain at a standstill. This situation is called
deadlock in DBMS.
Definition: Deadlock can be formally defined as - " A system is in deadlock
state if there exists a set of transactions such that every transaction in the set is
waiting for another transaction in the set. "
There are four conditions for a deadlock to occur
A deadlock may occur if all the following conditions holds true.
1. Mutual exclusion condition: There must be at least one resource that cannot
be used by more than one process at a time.
2. Hold and wait condition: A process that is holding a resource can request
for vd's additional resources that are being held by other processes in the
system.
3. No preemption condition: A resource cannot be forcibly taken from a
process. Only the process can release a resource that is being held by it.
4. Circular wait condition: A condition where one process is waiting for a
resource that is being held by second process and second process is waiting for
third process In the....so on and the last process is waiting for the first process.
Thus making a circular chain of waiting.
Deadlock can be handled using two techniques -
1. Deadlock Prevention
2. Deadlock Detection and deadlock recovery
1. Deadlock prevention :
For large database, deadlock prevention method is suitable. A deadlock can be
prevented if the resources are allocated in such a way that deadlock never occur.
The DBMS analyzes the operations whether they can create deadlock situation
or not, If they do, that transaction is never allowed to be executed.
There are two techniques used for deadlock prevention -
(i) Wait-Die:
• In this scheme, if a transaction requests for a resource which is already held
with a conflicting lock by another transaction then the DBMS simply checks the
timestamp of both transactions. It allows the older transaction to wait until the
resource is available for execution.
• Suppose there are two transactions T; and T, and let TS(T) is a timestamp of
any transaction T. If T2 holds a lock by some other transaction and T1 is
requesting for resources held by T2 then the following actions are performed by
DBMS:
Let T1 is a transaction which requests the data item acquired by Transaction T2.
Similarly T3 is a transaction which requests the data item acquired by
transaction T2.
Here TS(T1) i.e. Time stamp of T1 is less than TS(T3). In other words T1 is
older than T3. Hence T1 is made to wait while T3 is rolledback.
(ii) Wound - wait:
• In wound wait scheme, if the older transaction requests for a resource which
is held by the younger transaction, then older transaction forces younger one to
not kill the transaction and release the resource. After some delay, the younger
bevomer transaction is restarted but with the same timestamp.
• If the older transaction has held a resource which is requested by the
Younger transaction, then the younger transaction is asked to wait until older
releases it.
Suppose T1 needs a resource held by T2 and T3 also needs the resource held by
T2, with TS(T1)=5, TS(T2)=8 and TS(T3)=10, then T1 being older waits and
T3 being younger dies. After the some delay, the younger transaction is
restarted but with the same timestamp.
This ultimately prevents a deadlock to occur.
To summarize

2. Deadlock detection:
• In deadlock detection mechanism, an algorithm that examines the state of the
system is invoked periodically to determine whether a deadlock has occurred or
not. If deadlock is occurrence is detected, then the system must try to recover
from it.
• Deadlock detection is done using wait for graph method.
Wait for graph
• In this method, a graph is created based on the transaction and their lock. If the
created graph has a cycle or closed loop, then there is a deadlock.
• The wait for the graph is maintained by the system for every transaction which
is waiting for some data held by the others. The system keeps checking the
graph if there is any cycle in the graph.
• This graph consists of a pair G = (V, E), where V is a set of vertices and E is a
set of edges.
• The set of vertices consists of all the transactions in the system.
Consider following transactions, We will draw a wait for graph for this scenario
and check for deadlock.

We will use three rules for designing the wait-for graph -


Rule 1: If T1 has Read operation and then T2 has Write operation then draw an
edge T1->T2.
Rule 2: If T1 has Write operation and then T2 has Read operation then draw an
edge T1->T2
Rule 3: If T1 has Write operation and then T2 has Write operation then draw an
edge T1->T2
Let us draw wait-for graph
Step 1: Draw vertices for all the transactions

Step 2: We find the Read-Write pair from two different transactions reading
from top to bottom. If such as pair is found then we will add the edges between
corresponding directions. For instance –
Step 3:

4)Serializability
• When multiple transactions run concurrently, then it may lead to inconsistency
of data (i.e. change in the resultant value of data from different transaction).
• Serializability is a concept that helps to identify which non serial schedule and
find the transaction equivalent to serial schedule.
• For example:
• In above transaction initially T1 will read the values from database as A= 100,
B= 100 and modify the values of A and B, transaction T2 will read the modified
value i.e. 90 and will modify it to 80 and perform write operation. Thus at the
end of transaction T1 value of A will be 90 but at end of transaction T2 value of
A will be 80. Thus conflicts or inconsistency occurs here. This sequence can be
converted to a sequence which may give us consistent result. This process is
called serializability.
Difference between Serial schedule and Serializable schedule
• There are two types of serializabilities: conflict serializability and view
serializability

Conflict Serializability
• Definition: Suppose T1 and T2 are two transactions and I1 and I2 are the
instructions in T1 and T2 respectively. Then these two transactions are said to be
conflict Serializable, if both the instruction access the data item d, and at least
one of the instruction is write operation.
• What is conflict?: In the definition three conditions are specified for a
conflict in conflict serializability -
1) There should be different transactions
2) The operations must be performed on same data items
3) One of the operation must be the Write(W) operation

5)ARIES Algorithm
• ARIES is a recovery algorithm.
• It stands for Algorithm for Recovery and Isolation Exploiting Semantics.
• It is based on Write Ahead Log (WAL) protocol.
• When database crashes during some transaction processing, we have the logs
that got saved to the disk.
• ARIES has 3 phases that occur in the following order -
1) Analysis:
• Scan the log from start to reconstruct the transaction and dirty page table.
Dirtypages contain data that has been changed but not yet written to disk.
• The active transactions which were present at the time of crash are identified.
• During analysis phase the log is scanned forward from the checkpoint record
to construct snapshot of what system looks like at the time of crash.
2) Redo :
• This phase is started only after completion of analysis phase.
• The log is read forward and each update is redone.
3) Undo :
• This phase is started after redo phase.
• The log is scanned backward and updates to corresponding active transactions
are undone.
Advantages:
1) It is simple and flexible.
2) It supports concurrency control protocol.
3) Independent recovery of every page.

You might also like