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

Concurrency Control

concurrency control

Uploaded by

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

Concurrency Control

concurrency control

Uploaded by

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

Concurrency Control

Concurrency Control

• When more than one transactions are running simultaneously there


are chances of a conflict to occur which can leave database to an
inconsistent state. To handle these conflicts we need concurrency
control in DBMS, which allows transactions to run simultaneously but
handles them in such a way so that the integrity of data remains
intact.
Problems of concurrency control

• Several problems can occur when concurrent transactions are


executed in an uncontrolled manner. Following are the three
problems in concurrency control.

• Dirty read
• Unrepeatable read
• Lost updates
Dirty read
• Reading the data written by an
uncommitted transaction is called as dirty
read
• In this example,
• T2 reads the dirty value of A written by
the uncommitted transaction T1.
• T1 fails in later stages and roll backs.
• Thus, the value that T2 read now stands to
be incorrect.
• Therefore, database becomes
inconsistent.
Unrepeatable read
• This problem occurs when a transaction
gets to read unrepeated i.e. different
values of the same variable in its
different read operations even when it
has not updated its value. Example:
• T1 reads the value of X (= 10 say).
• T2 reads the value of X (= 10).
• T1 updates the value of X (from 10 to 15
say) in the buffer.
• T2 again reads the value of X (but = 15).
Lost updates(w/w conflict)
• This problem occurs when multiple
transactions execute concurrently and
updates from one or more transactions get
lost. example:
• T1 reads the value of A (= 10 say).
• T2 updates the value to A (= 15 say) in the
buffer.
• T2 does blind write A = 25 (write without
read) in the buffer.
• T2 commits.
• When T1 commits, it writes A = 25 in the
database.
Reasons for using Concurrency control
method is DBMS:
• To apply Isolation through mutual exclusion between conflicting
transactions
• To resolve read-write and write-write conflict issues
• To preserve database consistency through constantly preserving
execution obstructions
• The system needs to control the interaction among the concurrent
transactions. This control is achieved using concurrent-control
schemes.
• Concurrency control helps to ensure serializability
Conflict Example

• You and your brother have a joint bank account, from which you both
can withdraw money. Now let’s say you both go to different branches
of the same bank at the same time and try to withdraw 5000 INR,
your joint account has only 6000 balance. Now if we don’t have
concurrency control in place you both can get 5000 INR at the same
time but once both the transactions finish the account balance would
be -4000 which is not possible and leaves the database in inconsistent
state.
Solution of Conflicts: Locks

• A lock is kind of a mechanism that ensures that the integrity of data is


maintained. There are two types of a lock that can be placed while
accessing the data so that the concurrent transaction cannot alter the
data while we are processing it.
• Shared Lock (S)
• Exclusive Lock(X)
• 1. Shared Lock(S): Shared lock is placed when we are reading the
data, multiple shared locks can be placed on the data but when a
shared lock is placed no exclusive lock can be placed.
• example, when two transactions are reading Steve’s account balance,
let them read by placing shared lock but they can't write at the same
time.
• 2. Exclusive Lock(X): Exclusive lock is placed when we want to read
and write the data. This lock allows both the read and write
operation, Once this lock is placed on the data no other lock (shared
or Exclusive) can be placed on the data until Exclusive lock is released.

• For example, when a transaction wants to update the Steve’s account


balance, let it do by placing X lock on it but if a second transaction
wants to read the data(S lock) don’t allow it
Lock Compatibility Matrix
• __________________________
• | | S | X |
• |-------------------------
• | S | True | False |
• |-------------------------
• | X | False | False |

• A transaction may be granted a lock on an item if the requested lock is


compatible with locks already held on the item by other transaction.
• Any number of transactions can hold shared locks on an item, But if any
transaction holds an exclusive on the item no other transaction may hold
any lock on the item
Lock Based Protocol
• If a lock cannot be granted, the requesting transaction is made to wait
till all incompatible locks held by other transaction have been
released. The lock is then granted.
• Example:
• T1: lock-S(A); // Grant-S(A,T1)
• read (A);
• unlock(A);
• lock-S(B); // Grant-S(B,T1)
• read (B);
• unlock(B);
• display(A+B)
Pitfalls of Lock Based Protocol
Neither T3 nor T4 can make progress —
executing lock-S(B) causes T4 to wait
for T3 to release its lock on B, while
executing lock-X(A) causes T3 to wait
for T4 to release its lock on A.

• Such a situation is called a deadlock.


– To handle a deadlock one of T3 or T4
must be rolled back and its locks
released.
Pitfalls of Lock Based Protocol
• Starvation is also possible if concurrency control manager is badly
designed. For example:
• – A transaction may be waiting for an X-lock on an item, while a
sequence of other transactions request and are granted an S-lock on
the same item.
• – The same transaction is repeatedly rolled back due to deadlocks.
Two Phase Locking Protocol
• The two-phase locking protocol divides the execution phase of the
transaction into three parts.
• In the first part, when the execution of the transaction starts, it seeks
permission for the lock it requires.
• In the second part, the transaction acquires all the locks. The third
phase is started as soon as the transaction releases its first lock.
• In the third phase, the transaction cannot demand any new locks. It
only releases the acquired locks.
Two Phase Locking Protocol
There are two phases of 2PL:
• Growing phase: In the growing phase, a new lock on the data item
may be acquired by the transaction, but none can be released.
• Shrinking phase: In the shrinking phase, existing lock held by the
transaction may be released, but no new locks can be acquired.
• In the example, if lock conversion is allowed then the following phase
can happen:
• Upgrading of lock (from S(a) to X (a)) is allowed in growing phase.
• Downgrading of lock (from X(a) to S(a)) must be done in shrinking
phase.
Example
The following way shows how
unlocking and locking work with 2-
PL.
Transaction T1:
• Growing phase: from step 1-3
• Shrinking phase: from step 5-7
• Lock point: at 3
Transaction T2:
• Growing phase: from step 2-6
• Shrinking phase: from step 8-9
• Lock point: at 6

You might also like