Professional Documents
Culture Documents
Concurency Control Full
Concurency Control Full
UNIT – IV
A DBMS uses a transaction log to keep track of all transactions that update the
database.
The transaction log is a buffer and it stores the following information:
a) A record for the beginning of the transaction.
b) For each transaction component (SQL statement) the information is stored like
i. The type of operation being performed (update,delete,insert)
ii. The names of the objects affected by the transaction (The name of the
table)
iii. The before and after values for the fields being updated.
c) The ending (COMMIT) of the transaction.
The information stored in this log is used by DBMS when a system failure occurred or
abnormal termination of a program or network failure or disk crash etc.
When recovery process is completed, the DBMS will write in the log all committed
transactions.
Committed transactions are not rolled back.
Although using a transaction log increase the processing overhead of a DBMS but it
restores a corrupted database.
****************************************************************************
SRIDHARA JAYA SUDHA
Unit – IV (chapter – I)
Concurrency Control:
In multi user database system many transactions are executing simultaneously. The
coordination among those transactions is called concurrency control.
The objective of concurrency control is to ensure the serializability of transaction in a
multi-user database environment.
In the normal circumstances the serial execution of two transactions given below:
2
Unit – IV (chapter – I)
T1 Read QOH 35
T1 QOH=35+100
T2 QOH=135-30
But when T1 and T2 concurrent transaction updating the same data element then lost
T1 Read QOH 35
T2 Read QOH 35
T1 QOH=35+100
T2 QOH=35-30
(Lost update)
T2 Write QOH 5
The first transaction T1 has not yet bean committed when the second transaction (T2)
is executed.
b) Uncommitted data:-
3
Unit – IV (chapter – I)
T1 Read QOH 35
T1 QOH=35+100
Roll Back 35
T2 Read QOH 35
T2 QOH=135-30
T2 Write QOH 5
But uncommitted data problem arises when T2 has begun before the ROLLBACK of T1.
4
Unit – IV (chapter – I)
T1 Read QOH 35
T1 QOH=35+100
T2 QOH=135-30
T1 Roll Back 35
T2 QOH 105
c) Inconsistent retrievals:-
Inconsistent retrievals occur when a transaction is accessing the data before another
transaction starts and accessing the data after finishing the another transaction.
For example
1. T1 calculates the total quantity on hand of the products stored in the
PRODUCT table.
2. At the same time T2 updates the quantity on hand (QOH) for two products in
PRODUCT table
The 2 transactions are as shown in below
Transaction (T1) Transaction (T2)
Select sum(QOH) from product; update product set QOH=QOH+10 where Pid=’P10’;
Update product set QOH=QOH-10
where Pid=’P14’;
commit;
So in general circumstances results are as follows.
Pid Before After
QOH QOH
5
Unit – IV (chapter – I)
P1 8 8
P2 32 32
P10 15 15+10=25
P14 23 23-10=13
P18 8 8
P20 6 6
Total 92 92
But an inconsistent retrieval can occur if transaction T1 is reading some data before
they are changed and other data after they are changed.
The inconsistent retrieval is shown below:
Transaction Action Value Total
T2 QOH=15+10
T2 QOH=23-10
(Commit)
6
Unit – IV (chapter – I)
The Scheduler:-
The Scheduler is a special DBMS process which is used to maintain serializability
and isolation of transactions.
Serializability:-
When multiple transactions are executed, then the DBMS must schedule the
concurrent execution of the transaction’s operations. This property is reffred as
serializability.
This property is important in multi-user and distributed databases.
The scheduler’s main job is to create a serializable schedule of a transaction’s
operations.
When transactions are non serializable then they are executed on first come, first
served basis by the DBMS. The problem with this approach is processing time of
CPU is wasted.
The scheduler also facilitates data isolation to insure that two transactions do not
update the same data element at the same time.
Suppose T1 and T2 are the two transactions executing concurrently on the same
data item, then in the following situations conflictions will occur.
7
Unit – IV (chapter – I)
View serializability:
• For each data item x, if transaction Ti reads the initial value of x in schedule S1,
then transaction Ti must also read the initial value of x in schedule S2.
• For each read operation on data item x by transaction Ti in schedule S1, if the
value read by Ti has been written by transaction Tj , then transaction Ti must also
read the value of x produced by transaction Tj in schedule S2.
• For each data item x, if the last write operation on x was performed by
transaction Ti in schedule S1, the same transaction must perform the final write on
data item x in schedule S2.
Recoverability:
In addition, the durability property states that once a transaction commits, its
changes cannot be undone.
8
Unit – IV (chapter – I)
To avoid the problems of concurrent transactions the DBMS provides the following
methods:
1) Locking methods
2) Time stamping methods
3) Optimistic methods
A transaction acquires a lock before accessing the data, when the transaction is
completed then the lock is released (unlock), so that another transaction can lock
the data item.
Ex: Suppose T1 and T2 are two transactions. T1 is accessing a data item, then
Transaction T2 cannot access that data item.
All lock information is managed by a lock manager, which is responsible for
assigning and policing the locks used by the transactions.
Lock Types:-
The DBMS use different lock types like
a) Binary Locks
b) Shared/Exclusive Locks.
a) Binary Locks:-
Using binary locks the lost update problem is eliminated in concurrency control
because the lock released until the WRITE statement is completed.
But binary locks are considered as too restrictive.
For example the DBMS will not allow two transactions to read the same database
object even though neither transaction updates the database.
b) Shared/Exclusive Locks:-
A shared lock exists when concurrent transactions are granted for read operation.
A common lock is used for concurrent transaction. A shared lock produces no
conflict as long as all the concurrent transactions are read only.
An exclusive lock exists when transaction is granted for write operation. The
exclusive lock must be used when conflicts exists. (One transaction is READ and
other is WRITE).
So a shared lock is issued when a transaction wants to read data from the database
and an exclusive lock is issued when a transaction wants to write a data item.
Using Shared/Exclusive locking concept a lock can have 3 states
a) Unlocked
b) Shared (read)
c) Exclusive (write)
Examples for shared/Exclusive locks:
1) If transaction T1 has shared lock on data item X and transaction T2 wants to read
data item X. So T2 may also obtain a shared lock on data item X.
Read Read
Data Item X
T1 T2
Shared lock
2) If transaction T1 has Shared or Exclusive lock on data item X and transaction T2
wants an exclusive lock to update data item X. But an exclusive lock cannot be granted
to transaction T2 and it must wait till T1 is saved. So “The exclusive lock is granted if
and only if no other locks are held on the data item”.
Write unable to access
Data Item X
T1 T2
10
Unit – IV (chapter – I)
Exclusive lock
Shared/Exclusive locks are more efficient but these increase the lock manager’s
overhead because of the following reason.
a) The type of the lock must be held before a lock is granted.
Shared/Exclusive locks can lead to two major problems:
a) The resulting transaction schedule might not be serializable.
b) The schedule might create deadlock.
****************************************************************************
In two phase locking protocol the transaction acquires all the locks until it reaches
its locked point. When the locked point is reached, the data are modified to
conform to the transaction requirements. Finally the transaction is completed, and
then it releases all the locks which are acquired.
The two phase locking increase the transaction processing cost but the drawback is
it might create deadlocks.
*************************************************************************
11
Unit – IV (chapter – I)
Deadlocks in DBMS
In a database, a deadlock is an unwanted situation in which two or more transactions are
waiting indefinitely for one another to give up locks.
Deadlock is said to be one of the most feared complications in DBMS as it brings the
whole system to a Halt.
Example – let us understand the concept of Deadlock with an example :
Suppose, Transaction T1 holds a lock on some rows in the Students table and needs to
update some rows in the Grades table. Simultaneously, Transaction T2 holds locks on
those very rows (Which T1 needs to update) in the Grades table but needs to update the
rows in the Student table held by Transaction T1.
Now, the main problem arises. Transaction T1 will wait for transaction T2 to give up lock,
and similarly transaction T2 will wait for transaction T1 to give up lock.
As a consequence, all activity comes to a halt and remains at a standstill forever unless the
DBMS detects the deadlock and aborts one of the transactions.
12
Unit – IV (chapter – I)
The DBMS tests the database for deadlocks. If a deadlock is found, one of the
transactions is aborted and the other transaction continues.
This method is used when there is less probability of dead locks.
c) DeadLock Avoidance:-
The transaction must obtain all of the locks if needs before it can be executed. This
technique avoids the rollback of conflicting transactions.
*************************************************************************
The wait/die and wound/wait scheme are used in time stamping method.
Example:
Assume that we have two conflicting transactions:
T1 and T2, each with a unique time stamp.
Suppose T1 has time stamp of 1154 and T2 has a time stamp of 1956. So T1 is the
older transaction and T2 is newer (younger) transaction.
13
Unit – IV (chapter – I)
a) If the transaction requesting is the older of the two transactions, it will wait until
the other transactions is completed.
b) If the transaction requesting is the younger of the two transactions, it will die
(rollback) and is rescheduled using the same time stamp.
a) If the transaction requesting is the older of the two transactions, it will preempt
(wound) the younger transaction (by rolling it back). The younger transaction is
rescheduled using the same time stamp.
b) If the transaction requesting is the younger of the two transactions, it will wait until
the other transaction is completed.
Wait/die scheme and wound/wait scheme are shown in the following table.
Transaction owing Transaction Wait/die scheme Wound/wait scheme
requesting
T2 (1956) T1 (1154) T1 waits until T2 is T1 preempts
completed (rollback) T2 and T2
is rescheduled using
the same time stamp
T1 (1154) T2 (1956) T2 dies (rollback) T2 waits until T1 is
and T2 is rescheduled completed.
using the same time
stamp
****************************************************************************
The optimistic approach is based on the assumption that the majority of the
database operations do not conflict.
The optimistic approach requires neither locking nor time stamping techniques.
Using an optimistic approach, each transaction moves through three phases.
They are
a) Read Phase
b) Validation Phase
c) Write Phase.
During the read phase, the transaction reads the database, executes the needed
computations, and makes the updates to a private copy of the database values. All
the update operations of the transaction are recorded in a temporary update file,
which is not accessed by the remaining transactions.
During the validation phase the transaction is validated to ensure that the changes
made will not affect the integrity and consistency of the database.
During the write phase, If the validation test is positive, the transaction goes to the
write operations. If the validation test is negative, the transaction is restarted and
the changes are discarded.
Multi version Timestamp Ordering:
Versioning of data can also be used to increase concurrency, as different users may
work concurrently on different versions of the same object instead of having to
wait for each others’ transactions to complete.
The basic timestamp ordering protocol discussed in the previous section assumes
that only one version of a data item exists, and so only one transaction can access a
data item at a time.
This restriction can be relaxed if we allow multiple transactions to read and write
different versions of the same data item, and ensure that each transaction sees a
consistent set of versions for all the data items it accesses.
In multi version concurrency control, each write operation creates a new version of
a data item while retaining the old version. When a transaction attempts to read a
data item, the system selects the correct version of the data item according to the
timestamp of the requesting transaction.
15
Unit – IV (chapter – I)
Lock granularity indicates the level of lock use. Locking can take place at the
following level.
a) Database level
b) Table level
c) Page level
d) Row level
e) Field (attribute level)
a) Database level:-
In database level lock the entire database is locked. So if transaction T1 is accessing
that database, then transaction T2 can not access it.
Database
Table A
Table B
This level of locking is good for batch processes but it is not suitable for multi user
DBMS. Because thousands of transactions had to wait for the previous transaction
is to be completed. So the data access would be slow.
Table level:-
In table level lock the entire table is locked that means if transaction T1 is accessing
a table then transaction T2 can not access the same table.
Database
Table A
16
Unit – IV (chapter – I)
Table B
Table A
Page 1
T1 is accessing
Page 2
T2 is accessing
In the above fig. T1 and T2 access the same table while locking different disk pages.
If T1 is locking page1 then T2 cannot access the rows in the page1 till T1 unlocks.
Row level:-
A row level lock is much less restrictive than the other locks. The DBMS allows
concurrent transactions to access different rows of the same table.
The row level locking approach improves the availability of data.
17
Unit – IV (chapter – I)
But row level locking requires high overhead because a lock exist for each row in a
table of the database.
Field level:-
The field level lock allows concurrent transactions to access the same row but
different fields (attributes) within that row.
Although field level locking is the most flexible in multi user data access, but it is
rarely implemented in a DBMS because it requires an extreme High Level
computer overhead.
*************************************************************************
***
18