Unit -4 - part 3
Unit -4 - part 3
UNIT – IV
Slide 1- 1
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe
Concurrent Execution in DBMS
In a multi-user system, multiple users can access and use the same database
at one time, which is known as the concurrent execution of the database.
It means that the same database is executed simultaneously on a multi-user
system by different users.
Example:
Example : Consider the below diagram where two transactions TX and TY, are
performed on the same account A where the balance of account A is $300.
The dirty read problem occurs when one transaction updates an item of the
database, and somehow the transaction fails, and before the data gets
rollback, the updated database item is accessed by another transaction.
There comes the Read-Write Conflict between both transactions.
Example :
A lock is a variable associated with a data item that describes the status of
the item with respect to possible operations that can be applied to it.
There is one lock for each data item in the database.
Locks are used as a means of synchronizing the access by concurrent
transactions to the database items.
Two operations, lock_item and unlock_item, are used with binary locking.
A transaction requests access to an item X by first issuing a lock_item(X)
operation.
When the transaction is completed using the item, it issues an unlock_item(X)
operation, which sets LOCK(X) back to 0 (unlocks the item) so that X may be
accessed by other transactions.
1. Shared lock:
It is also known as a Read-only lock.
In a shared lock, the data item can only read by the transaction.
It can be shared between the transactions because when the transaction holds a
lock, then it can't update the data on the data item.
2. Exclusive lock:
In the exclusive lock, the data item can be both reads as well as written by the
transaction.
This lock is exclusive, and in this lock, multiple transactions do not modify the
same data simultaneously.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 19
Lock-Based Protocol
Lock Compatibility Matrix –
T2: lock-S(A);
read (A);
unlock(A);
lock-S(B);
read (B);
unlock(B);
display(A+B)
Pre-claiming Lock Protocols evaluate the transaction to list all the data items on
which they need locks.
Before initiating an execution of the transaction, it requests DBMS for all the
lock on all those data items.
If all the locks are granted then this protocol allows the transaction to begin.
When the transaction is completed then it releases all the lock.
If all the locks are not granted then this protocol allows the transaction to rolls
back and waits until all the locks are granted.
Drawback:
Starvation
Starvation is the situation when a transaction needs to wait for an indefinite
period to acquire a lock.
Following are the reasons for Starvation:
When waiting scheme for locked items is not properly managed
In the case of resource leak
The same transaction is selected as a victim repeatedly
Deadlock
Deadlock refers to a specific situation where two or more processes are waiting
for each other to release a resource or more than two processes are waiting for
the resource in a circular chain.
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.
The two phases are known as the growing and shrinking phases.
Growing Phase: In this phase, we can acquire new locks on data items, but
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
Problem :
The likelihood of establishing deadlocks is one bad result.
If a precedence graph contains a single edge Ti → Tj, then all the instructions
of Ti are executed before the first instruction of Tj is executed.
Example: Explanation:
Read(A): In T1, no subsequent writes to A, so no new edges
Read(B): In T2, no subsequent writes to B, so no new edges
Read(C): In T3, no subsequent writes to C, so no new edges
Write(B): B is subsequently read by T3, so add edge T2 → T3
Write(C): C is subsequently read by T1, so add edge T3 → T1
Write(A): A is subsequently read by T2, so add edge T1 → T2
Write(A): In T2, no subsequent reads to A, so no new edges
Write(C): In T1, no subsequent reads to C, so no new edges
Write(B): In T3, no subsequent reads to B, so no new edges
T1
T2 T3
The precedence graph for schedule S1 contains a cycle that's why Schedule S1
is non-serializable.
Example: Explanation:
Read(A): In T4,no subsequent writes to A, so no new edges
Read(C): In T4, no subsequent writes to C, so no new edges
Write(A): A is subsequently read by T5, so add edge T4 → T5
Read(B): In T5,no subsequent writes to B, so no new edges
Write(C): C is subsequently read by T6, so add edge T4 → T6
Write(B): A is subsequently read by T6, so add edge T5 → T6
Write(C): In T6, no subsequent reads to C, so no new edges
Write(A): In T5, no subsequent reads to A, so no new edges
Write(B): In T6, no subsequent reads to B, so no new edges
Conflicting operations:
Two operations are said to be conflicting if all conditions satisfy:
They belong to different transaction
Example
Consider the following schedule:
S1: R1(A), W1(A), R2(A), W2(A), R1(B), W1(B), R2(B), W2(B)
Example
Similarly, swapping non-conflicting operations W2(A) and W1(B) in S11,
the schedule becomes
S11: R1(A), W1(A), R1(B), W2(A), R2(A), W1(B), R2(B), W2(B)
Example
T1 T2
Read(A)
Write(A)
Read(B)
Write(B)
Read(A)
Write(A)
Read(B)
Write(B)
View Equivalent
Two schedules S1 and S2 are said to be view equivalent if they satisfy the
following conditions:
1. Initial Read
2. Updated Read
3. Final Write
1. Initial Read
An initial read of both schedules must be the same.
Suppose two schedule S1 and S2.
In schedule S1, if a transaction T1 is reading the data item A, then in S2,
transaction T1 should also read A.
Above two schedules are view equivalent because Initial read operation in S1 is
done by T1 and in S2 it is also done by T1.
2. Updated Read
In schedule S1, if Ti is reading A which is updated by Tj then in S2 also, Ti
should read A which is updated by Tj.
Above two schedules are not view equal because, in S1, T3 is reading A
updated by T2 and in S2, T3 is reading A updated by T1.
3. Final Write
A final write must be the same between both the schedules.
In schedule S1, if a transaction T1 updates A at last then in S2, final writes
operations should also be done by T1.
Above two schedules is view equal because Final write operation in S1 is done
by T3 and in S2, the final write operation is also done by T3.
schedule S1
The first schedule S1 satisfies all three conditions, so we don't need to check
another schedule.
Hence, view equivalent serial schedule is:
T1 → T2 → T3
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 63