Database 1
Database 1
1. read(A) 4. read(B)
2. A := A – 50 5. B := B + 50
3. write(A) 6. write(B)
Cont’d.
According to the number of users who can use the system concurrently a
DBMS is classified into two.
1. Single-User : A DBMS is single-user if at most one user at a time can use
the system
2. Multiuser: if many users can use the system and accesses the database
concurrently.
Database systems used in banks, insurance agencies, stock exchanges,
supermarkets, and many other applications are multiuser systems.
In these systems, hundreds or thousands of users are typically operating on the
data-base by submitting transactions concurrently to the system.
Concurrency Control
when two or more users are accessing the database simultaneously and at least
one is updating data, there may be interference that can result in
inconsistencies.
Concurrency control is the process of managing simultaneous operations on
the database without having them interfere with one another.
Why Concurrency Control is needed?
A major objective in developing a database is to enable many users to access
shared data concurrently.
The objective of concurrency control is to ensure the serializability of
transactions in a multiuser database environment.
Problems of Concurrent Sharing
Assume that you have a product whose current quantity value is 35. Also
assume that two concurrent transactions, T1 and T2, occur that update the
quantity value for some item in the database. The transactions are as below:
Transaction Computation
T1: Buy 100 units quantity =quantity +100
T2 : Sell 30 units quantity=qunatitiy-30
Cont’d.
The sequence depicted below shows how the lost update problem can arise.
Note that the first transaction (T1) has not yet been committed when the
second transaction (T2) is executed.
Uncommitted data occurs when two transactions, T1 and T2, are executed
concurrently and the first transaction (T1) is rolled back after the second
transaction (T2) has already accessed the uncommitted data.
Example: let’s use the same transactions described during the lost updates
discussion. T1 is forced to roll back due to an error. T1 transaction is rolled
back to eliminate the addition of the 100 units. Because T2 subtracts 30 from
the original 35 units, the correct answer should be 5.
Cont’d.
Following table shows how the uncommitted data problem can arise when
the ROLLBACK is completed after T2 has begun its execution.
Transaction 1 Transaction 2
Select sum(quantity) from product; Update product set quantity=quantity+10 where pid = 1003
Update product set quantity =quantity-10 where pid=1004
Commit
Cont’d.
The following table shows the serial execution of those transactions under
normal circumstances
Below Table demonstrates that inconsistent retrievals are possible during the
transaction execution, making the result of T1’s execution incorrect.
Time Transaction Action Value Total
1 T1 Read quantity for pid=1001 8 8
2 T1 Read quantity for pid=1002 32 40
3 T2 Read quantity for pid=1003 15
4 T2 Quantity=qunatity+10
5 T2 Write quantity for pid=1003 25
6 T1 Read quantity for pid=1003 25 65
7 T1 Read quantity for pid=1004 23 88
8 T2 Read quantity for pid=1004 23
9 T2 Quantity=qunatity-10
10 T2 Write quantity for pid=1004 13
11 T2 Commit
4. The Unrepeatable Read Problem
A transaction T1 reads the same item twice and the item is changed by
another transaction T2 between the two reads. Hence, T1 receives different
values for its two reads of the same item.
For example, during an airline reservation transaction, a customer inquiry
about seat availability on several flights.
When the customer decides on a particular flight, the transaction then reads
the number of seats on that flight a second time before completing the
reservation, and it may end up reading a different value for the item.
Concepts of Serializability
To determine the appropriate order, the scheduler bases its actions on
concurrency control algorithms, such as locking or time stamping
methods.
Transactions T1 T2 Result
Read Read No conflict
Read Write Conflict
Operations
Write Read Conflict
Write Write Conflict
Characterizing Schedules Based on Serializability
Eg: Let T1 transfer $50 from A to B, and T2 transfer 10% of the balance from
A to B.
AA-50 TempA*0.1
Write(A) AA-temp
Read(B) Write(A)
B=B+50 Read(B)
Write(B) B=B+temp
Read(A) Write(B)
TempA*0.1 Read(A)
AA-temp AA-50
Write(A) Write(A)
Read(B) Read(B)
B=B+temp B=B+50
Write(B) Write(B)
Cont.
For eg: Let T1 transfer $50 from A to B, and T2 transfer 10% of the balance
from A to B.
TI T2 TI T2
Read(A) Read(A)
AA-50 AA-50
Read(A)
Write(A)
TempA*0.1
Read(A)
AA-temp
TempA*0.1
Write(A)
AA-temp
Read(B)
Write(A)
Read(B) Write(A)
B=B+50 Read(B)
Write(B) B=B+50
Read(B) Write(B)
B=B + temp
B=B + temp
Write(B)
Write(B)
When are two schedules considered equivalent?
1. Result Equivalency
2. Conflict Equivalency
3. View Equivalence
Cont.
1. Result Equivalent
Two schedules are called result equivalent if they produce the same final
state of the database.
However two different schedules may accidently produce the same final
state. Hence result equivalence is not used to define equivalence of
schedules.
Cont.
2. Conflict Equivalent
Two schedules are said to be conflict equivalent if the order of any two
conflicting operations is the same in both schedules.
For any 2 given schedules, say S1 and S2 if the order of all possible conflicting
operations is the same in both then it is said to be conflict equivalent.
Cont.
Note:
The algorithm looks at only the read item and write item operations in a
schedule to construct a precedence graph.
There is one node in the graph for each transaction Ti in the schedule.
Algorithm For Testing Conflict Serializability of a Schedule
1. For each transaction Ti participating in schedule S, create a node and labelled it Ti in the
precedence graph.
2. For each case in S where Tj executes a read item(X) after Ti executes a write item(X),
create an edge (Ti→ Tj) in the precedence graph.
3. For each case in S where Tj executes a write item(X) after Ti executes a read item(X),
create an edge (Ti→Tj) in the precedence graph.
4. For each case in S where Tj executes a write item(X) after Ti executes a write item(X),
create an edge (Ti→ Tj) in the precedence graph.
5. The schedule S is serializable if and only if the precedence graph has no cycles.
(Cycle means - the sequence starts and ends at the same node).
Exercise
T1 T2 T3
R(X)
R(X)
W(X)
R(X)
W(X)
Cont.
T1 T2 T3
R(X)
R(X)
W(X)
R(X)
W(X)
We use topological sorting to find the equivalent serial schedule for the
conflict serializable schedule.
Steps of Topological Sorting
Step 1: Consider the In-degree of the nodes. Find the nodes with in-
degree zero.
In-degree= No of edges coming to the node.
For the given example, In-degree of T1, I=0 and In-degree of T2, I=1
Steps of Topological Sorting
Step 2: Ignore the node whose in-degree is zero and note it down, T1.
Step 4: Consider the remaining nodes and continue the above steps again.
Exercise 1
Consider the following graph for three transactions and specify whether it
supports conflict serializable schedule and write the equivalent serial
schedule
Step 1:
Indegree of T1 –0
Indegree of T2 –1
Indegree of T3 –2
Therefore consider T1 first and delete/ignore T1 and all edges from T1.
Step 2:
Indegree of T2 –0
Indegree of T3 –1
Therefore consider T2 first and delete/ignore T2 and all edges from T2.
Step 3:
T3
Indegree of T3 –0
Therefore consider T3
Consider the following concurrent schedule for three transactions and specify whether it is
conflict serializable schedule. If it is a conflict serializable schedule find the equivalent
serial schedule.
T1 T2 T3
R(X)
W(X)
W(X)
W(X)
Cont.
T1 T2 T3
T1 T2
R(X)
W(X)
W(X) T3
W(X)
There is cycle or loop in precedence graph. Therefore the given schedule is NOT conflict
serializable schedule
Class Work
Consider the following two schedules S1 and S2 for the transactions T1 and
T2. Now determine which among the schedules are conflict serializable
schedule.
Concurrent Schedule (S1) Concurrent Schedule (S2)
T1 T2 T1 T2
R(x) R(x)
R(y) R(x) R(x)
W(y) W(y)
W(x) R(y)
W(x)
View Serializable Schedule
View Equivalent
Two schedules is said to be view equivalent if the following three conditions hold:
1. Initial Reads: If T1 reads the initial data X in S1, then T1 also reads the initial data X in S2.
2. W-R Conflict: If T1 reads the value written by T2 in S1, then T1 also reads the value written by
T2 in S2.
3. Final Write: If T1 performs the final write on the data value in S1, then it also performs the final
write on the data value in S2.
Cont.
Initial Reads: In S1 there are only two initial reads, one from T2 and one from T3.
Similarly in the given serial schedule also there are two initial reads exactly one
from T2 and T3 on data item X. So the initial read condition is satisfied here.
W-R Conflict: In S1 there is one write- read conflict from T3 to T1. Similarly there
is write-read schedule from T3 to T1. So the second condition is also satisfied.
Final write: In S1 the final write on X is from T1, similarly in schedule S2 also the
final write on X is from T1. So the final condition is also satisfied.