CH-2 Transaction ProcessingSS Update
CH-2 Transaction ProcessingSS Update
Introduction of Transaction
Properties of Transaction
2
Transaction
3
What is a Transaction?
An atomic unit of Database access, which is either completely executed or not executed at
all.
A transaction is a unit of program under execution that accesses and possibly updates various
data items.
Transaction also logical unit of database processing that includes one or more access
Credit card processing, Supermarket scanning, Academic registration, Billing and many other
4
applications.
Cont’d
If the database operations in a transaction do not update the database but only retrieve data, the
consistent state.
Aborted: If the transaction does not execute successfully. The database must be restored
to the consistent state it was before the transaction started. Such a transaction is called
Partially committed state:- shows the end of read/write operation but this will not ensure
Committed state:- ensure that all the changes done on a record by a transaction were done
persistently.
Failed state:- happens when transaction is aborted during its active state or if one of the
rechecking is fails.
7
Transaction ACID Properties
To ensure data integrity, data consistency, DBMS should maintain the following ACID
properties:
Atomicity
Atomicity - is atomic unit of processing, it is either performed in its entirely or not
performed at all.
“All or Nothing”
// done
Else aborted 8
Cont’d
Example:
Transfer of amount 750 birr from account A to account B. Before execution of transaction
account A= 5000 birr and Account B= 8000 birr any failure (power failure, disk failure, hardware
error..)
The failure occurs after complete of write(A) operation but before the execution of write (B)
operation.
To have consistency data the DBMS have a capability of restore to the old value. 9
Consistency
A correct execution of the transaction must take the database from one consistent state to another.
From the above example before transaction, transaction account A= 5000 birr and Account B=
8000 birr.
After execution complete Account A=4250 and Account B=8750 that means equivalent with
concurrently access the shared data in the database at the same time)
The data used during the execution of one transaction can not be used by any other transaction until
T1 T2 Description
Read(A) 5000$
A=A-750$ 4250$
Write(A) Store to A
Read(A) 4250$
Temp=A*0.2 850$
A=A-Temp 3400$
Write(A) Store to A
Read(B) 8000$
B=B+750$ 8750$
Write(B) Store to B
Read(B) 8750$
B=B+ Temp 9600$
Write(B) Store to B
Cont’d
Schedule 2 Inconsistent state
T1 T2 Description
Read(A) 5000$
A=A-750$ 4250$
Read(A) 5000$
Temp=A*0.2 1000$
A=A-Temp 4000$
Write(A) Store to A=4000$
Read(B)
Write(A) 4250$
Read(B) 8000$
B=B+750$ 8750$ 4000$+9750$=13750$
Write(B) Store to B=8750$ So the transaction lost 750$
It leads to inconsistence
B=B+ Temp 9750$
Write(B) Store to B=9750$
Durability
Once a transaction changes the database and the changes are committed, these changes must never
Failures are generally classified as transaction, system, and media failures. There are several
5. Disk failure
making sure that either all the operations in the transaction are completed successfully and
their effect is recorded permanently in the database, or that the transaction does not have any
In the first case, the transaction is said to be committed, whereas in the second case, the
transaction is aborted.
more files that are managed separately from the actual database files.
The transaction log is subject to common dangers such as disk-full conditions and disk
crashes.
A DBMS uses a transaction log to keep track of all transactions that update the database.
The information stored in this log is used by the DBMS for a recovery requirement triggered
Although using a transaction log increases the processing overhead of a DBMS, the ability to
17
Concurrency Control
18
Concurrency Control
When two or more users are accessing the database simultaneously and at least one is updating
Concurrency control is the process of managing simultaneous operations on the database without
A major objective in developing a database is to enable many users to access shared data
20
Problems of Concurrent Sharing
The simultaneous execution of transactions over a shared database can create several data
Concurrent access of database case problem during writing rather than read operation.
The three main problems are lost updates, uncommitted data, and inconsistent retrieval.
the same data element and one of the updates is lost (overwritten by the other transaction.)
21
Example 1
Some problem that occur current execution is uncontrolled Suppose account A =1000
seqno T1 T2 Decsrpitoin
1 Read(A) Read(1000)
2 A=A+100 A=1100
Read(A) Read(1000)
3
4
A=A-200 A=800
-no
5 Write(A)1100
Write(A)800
6
Transactions Not Execute Serially
Example 2
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
23
Cont’d
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.
26
Example
Following table shows how the uncommitted data problem can arise when
the ROLLBACK is completed after T2 has begun its execution.
For example, an inconsistent retrieval would occur if transaction T1 calculated some summary
(aggregate) function over a set of data while another transaction (T2) was updating the same data.
The problem is that the transaction might read some data before they are changed and other data
29
Example
T1 would like to add the values of A =100, B=200, C=300
After the values are read by T1 and before its completion, T2 update the value of
B=500.At the end of execution of two transaction T1 will come up with the sum of 600,
While it should be 900 since B is updated to 500.
T1 T2
Sum=0;
Read item(A)
Sum=Sum+A
Read item(B)
Sum=Sum+B
-no Read item(B)
B=500
Read item(C)
Sum=Sum+C
Example
To illustrate that problem, assume the following conditions:
2. At the same time, T2 updates the quantity for two of the items in the PRODUCT
table.
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
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
34
Characterizing of Schedules Based on Serializability
35
Concepts of Schedule
Schedule: – it is a sequence of instructions that specify the chronological order in which
A schedule for a set of transactions must consist of all instructions of those transactions.
The scheduler is a special DBMS process that establishes the order in which the operations
The scheduler interleaves the execution of database operations to ensure serializability and
isolation of transactions.
36
Cont’d
To determine the appropriate order, the scheduler bases its actions on concurrency control
Additionally, the scheduler facilitates data isolation to ensure that two transactions do not update
Transactions T1 T2 Result
Read (x) Read (x) No conflict
Read (x) Write (x) Conflict
Operations
Write (x) Read (x) Conflict
Write (x) Write (x) Conflict
Cont’d
T1 T2 T1 T2
Read (x) Read (x)
S1 S2
Read (x) Write(x)
No conflict Conflict
T1 T2
Write (x)
S3
Write (x)
Conflict
Characterizing Schedules Based on Serializability
Serial Schedule: A schedule S is serial if, for every transaction T participating in the
schedule, all the operations of T are executed consecutively in the schedule.
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’d
For eg: Let T1 transfer $50 from A to B, and T2 transfer 10% of the balance
from A to B.
Read(A) TempA*0.1
TempA*0.1 AA-temp
AA-temp Write(A)
Write(A) Read(B)
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)
Serial Non-Serial
S1 S2
Example 1
Suppose Two transaction T1 and T2 running concurrently. T1:Read(A) T1:Read(A)
T2:Read(B)
T2:B=B-200
T2:Write(B)
T1:Read(B)
T1:B=B+100
T1:Write(B)
T2:Read(C)
T2:C=C+200
T2:Write(C)
Non-Serial
When are two schedules considered equivalent?
1. Result Equivalency
2. Conflict Equivalency
3. View Equivalence
Cont’d
1. Result Equivalent
Two schedules are called result equivalent if they produce the same final state of
the database.
Two schedules S1 and S2 are said to be result equivalent if they produce the same
final database state, in other words, they must produce the same result after
execution.
However two different schedules may accidently produce the same final state.
S1 S2
Proof it, is that Result
T1 T2 T1 T2
Equivalent Schedule or
Read (x) Read (x)
x=x-5 not?
x=x+10
Write (x) Write (x)
Read(x) Read(x) Value of (X)=100
x=x-7 x=x+8
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.
S1: r1(A),w1(A),r2(A),w2(A),r1(B),w1(B),r2(B),w2(B)
If Oi and Oj are two operations in a transaction and Oi< Oj (Oi is executed before Oj), same order will follow in the schedule as
well. Using this property, we can get two transactions of schedule S1 as:
Swapping non-conflicting operations R2(A) and R1(B) in S1, the schedule becomes,
Similarly, swapping non-conflicting operations W2(A) and W1(B) in S11, the schedule becomes,
S12 is a serial schedule in which all operations of T1 are performed before starting any operation of T2. Since S has been
transformed into a serial schedule S12 by swapping non-conflicting operations of S1, S1 is conflict serializable.
Conflict Serializable Schedule
Cont’d
Any given concurrent schedule is said to be conflict serializable schedule if it is conflict equivalent to one
of the possible serial schedule.
Two schedules are said to be conflict equivalent if all the conflicting operations in both the schedule get
executed in the same order. If a schedule is a conflict equivalent to its serial schedule then it is called
Conflict Serializable Schedule.
Precedence Graph
Precedence Graph is a graph used to test the Conflict Serializability of a Schedule.
The algorithm looks at only the read item and write item operations in a schedule to
construct a precedence graph.
It is a directed graph G = (N, E) that consists of a set of nodes N = {T1, T2, ..., Tn }
and a set of directed edges E = {e1,e2, ..., em }.
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).
Cont’d
Consider the following concurrent schedule for three transactions and specify whether it is
conflict serializable schedule.
There is no cycle or loop in precedence graph. Therefore the given schedule is a conflict
serializable schedule
Topological Sorting
The process of ordering the nodes of an acrylic graph is known as topological sorting.
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= Number 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
Hence the serial schedule equivalent to the conflict serializable schedule is T1, T2
and T3.
Exercise:2
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’d
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
Class Work
1, Which of the following schedules is (conflict) serializable? For each 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’d
Consider the following example:
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 conflict from T3 to T1 in S2. 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.