Subject: Database Management System CODE: 4CS4 - 05 UNIT: 04 Transaction Processing
Subject: Database Management System CODE: 4CS4 - 05 UNIT: 04 Transaction Processing
CODE: 4CS4 - 05
UNIT: 04
TRANSACTION PROCESSING
Delivered By:
Sandeep Tuli
CE, PIET
CONTENT
1. Transaction: Introduction
2. Transaction: Properties
1. Example
3. Transaction States
4. Concurrent Executions
5. Schedule
1. Types of Schedule
6. Serializability
1. Conflict Serializability
2. View Serializability
Transaction…
• A transaction is a single logical unit of work which accesses and possibly
modifies the contents of a database.
Transaction…
• A Single logical unit of work on a database
a. An entire program
b. A portion of a program
c. A single command
• Is a sequence of one or more SQL statements that are combined together to form a
single unit of work.
A
• Atomicity
C
• Consistency
I
• Isolation
D
• Durability
Transaction properties…
Atomicity:
• Means, that either the entire transaction takes place at once or doesn’t happen at all.
• There is no midway i.e. transactions do not occur partially.
• It involves the following two operations.
—Abort: If a transaction aborts, changes made to database are not visible.(ROLLBACK)
—Commit: If a transaction commits, changes made are visible.
(Consistent State: One where all data integrity constraints are satisfied)
Transaction properties…
Consistency:
Before transaction
A/c A is : 2000
A/c B is : 3000
The total amount before and after the transaction must be maintained.
• You can't delete a record from a primary table if matching records exist in a related table.
• You can't change a primary key value in the primary table if that record has related
records.
• You can't enter a value in the foreign key field of the related table that doesn't exist in the
primary key of the primary table.
• However, you can enter a Null value in the foreign key, specifying that the records are
unrelated.
Transaction properties…
Isolation:
• This property ensures that multiple transactions can occur concurrently without leading to
the inconsistency of database state.
• If Multiple transactions are executed in parallel, Changes occurring in one transaction will
not be visible to any other transaction until that particular change in that transaction is
written to memory or has been committed.
• This property ensures that the execution of transactions concurrently will result in a state
that is equivalent to a state achieved these were executed serially in some order.
Transaction properties…
Isolation:
• After a transaction finished successfully, changes made by the transaction persist and will
not be lost (e.g. on system failure)
• This property ensures that once the transaction has completed execution, the updates and
modifications to the database are stored in and written to disk, Then that changes must
persist even if a system failure occurs.
Transaction properties…
Aborted:
If the transaction has been rolled back and the Database has been restored to its
state prior to the start of transaction.
Committed:
Transaction has successfully completed.
Transaction…
• Two main issues to deal with transactions are:
1. Concurrent execution of multiple transactions
2. Failures of various kinds, such as hardware failures and system crashes
• A transaction is a unit of work that should be processed reliably without interference from other
INTERLEAVING
Concurrent execution
Advantages of Concurrent execution:
T1 T2 T1 T2 T1 T2
A = A * 10; A = A * 10; A = A * 10;
B = B - 10; B = B - 10;
A = A / 10;
A = A / 10; A = A / 10; B = B - 10;
B = B * 4; B = B * 4;
B = B * 4;
INITIALLY:
A = 100
B = 100
IN S1: A AND B ARE? 45, 155 IN S2: A AND B ARE? 45, 155 IN S3: A AND B ARE? 90, 160
Schedule 1 and 2 are? (Equivalent or Non-Equivalent) EQUIVALENT
Schedule 1 and 3 are? (Equivalent or Non-Equivalent) NON-EQUIVALENT
Schedule - types
Schedule - 1 Schedule - 2 Schedule - 3
T1 T2 T1 T2 T1 T2
A = A * 10; A = A * 10; A = A * 10;
B = B - 10; B = B - 10; INITIALLY:
A = A / 10;
A = A / 10; A = A / 10;
A = 100
B = B - 10;
B = B * 4; B = B * 4; B = 100
B = B * 4;
IN S1: A AND B ARE? 100, 360 IN S2: A AND B ARE? 100, 360 IN S3: A AND B ARE? 100, 360
T1 T2 T1 T2 T1 T2
A = A * 10; A = A * 10; A = A * 10;
B = B - 10; B = B - 10;
A = A / 10;
A = A / 10; A = A / 10; B = B - 10;
B = B * 4; B = B * 4;
B = B * 4;
Schedules-3 is serializable schedule which is equivalent to a Serial schedule
Schedules-1 and Serial schedule Schedules-2.
Serializability
• Serializability is a concurrency scheme where the concurrent transaction
is equivalent to one that executes the transactions serially.
1. Conflict Operations:
Those Read/Write operations, whose order cannot be changed without affecting the
consistency of data are called conflict operations.
Conflict Serializability…
Rules for Conflict Operations:
If two transactions just reads a data object then they do not conflict and order is not
important so can be swapped
If two transactions either read or write completely separate data objects then they do
not conflict and order is not important can be swapped
If one transactions writes a data objects and other transaction either reads or writes the
same data object then they conflict and order of execution is important.
Two actions on the same data objects conflict if at least one of them is write can not
be swapped.
Conflict Serializability…
READ(X) WRITE(X)
READ(X) NO YES
Read(A)
Write(A)
Read(A)
Conflict:
Cannot Swap
Read(B)
Write(A)
No Conflict:
Write(B) So Swap
Read(B)
Write(B)
Conflict Serializability…
Conflict Operations:
Example
Consider the Following schedule. Are there any conflict operations in T1 and T2?
Find out the serial schedule for it.? Make it conflict serializable.
T1 T2
Read(A)
Write(A)
Read(A)
Read(B)
Write(A)
Write(B)
Read(B)
Write(B)
Conflict Serializability…
T1 T2 T1 T2
Read(A) Read(A)
Write(A) Write(A)
Read(B)
Read(A)
Read(B) No Conflict: Read(A)
T1 T2 T1 T2
Read(A) Read(A)
Write(A) Write(A)
Read(B) Read(B)
Write(B)
Read(A)
Write(B) No Conflict: Read(A)
So Swap Write(A)
Write(A) Read(B)
Read(B) Write(B)
Write(B)
Conflict Serializability…
INITIAL SCHEDULE – S1 FINAL SCHEDULE – S2
T1 T2 T1 T2
Read(A)
Write(A) Read(A)
Write(A)
Read(A) Read(B)
Read(B) Write(B)
Write(A)
Read(A)
Write(B) Write(A)
Read(B) Read(B)
Write(B) Write(B)
The final schedule after swaps is a serial schedule and which is equivalent to the initial schedule.
Conflict Serializability
Three terms needs to be understand
1. Conflict Operations.
2. Conflict Equivalent.
3. Conflict Serializability.
1. Conflict Equivalent Schedule:
If a schedule S, can be transformed into a schedule S’ (say) by a series of swaps of non-
conflicting operations we say that S and S’ are conflict equivalent.
Schedule - S Schedule – S’
T1 T2 T1 T2
Read(A) Read(A)
Write(A) Write(A)
Read(B) Read(B)
Write(B) Write(B)
Conflict Serializability
Three terms needs to be understand
1. Conflict Operations.
2. Conflict Equivalent.
3. Conflict Serializability.
1. Conflict Serializable Schedule:
If a schedule S is conflict serializable if it is conflict equivalent to a serial schedule (S’)
So S is a conflict serializable as it is conflict equivalent to S’ which is serial schedule.
Schedule - S Schedule – S’
T1 T2 T1 T2
X=X+5; X=X+5;
X=X*1; Y=Y-100;
Y=Y-100; X=X*1;
Y=Y*20;
Y=Y*20
Conflict Serializability
Testing for conflict serializability
To test the conflict serializability, we can draw a Graph G = (V,E) where V = Vertices =
number of transactions E = Edges = for conflicting pair.
Steps :
Create node for each transaction.
Find the conflict pairs (RW, WR, WW) on same variable by different
transactions.
Draw edge from the schedule for each conflict pair such that for example, W2(B),
R1(B) is conflict pair, draw edge from T2 to T1
i.e. T2 must be executed before T1.
Testing conditions for conflict serializability of schedule
If precedence graph is cyclic ------------ non conflict serializable schedule
If precedence graph is a acyclic --------------- conflict serializable schedule
Example
Check whether the schedule is conflict serializable or not?
S: R1(A); R2(A); R1(B); R2(B); R3(B); W1(A); W2(B)
T1 T2 T3
R1(A)
R2(A)
R1(B)
R2(B)
R3(B)
W1(A)
W2(B)
W2(B)
Example
Check whether the schedule is conflict serializable or not?
S: R1(A); R2(A); R3(B); W1(A); R2(C); R2(B); W2(B)
T1 T2 T3
R1(A)
R2(A)
R3(B)
W1(A)
R2(C)
R2(B)
W2(B)
W2(B)
view Serializability
Two schedules S1 and S2 are said to be view equivalent: if below conditions are
satisfied:
1) Initial Read:
S1 S2
e.g: S1 And S2 are not View Equivalent as Transaction T2 is reading A from initial
database but in S2, T3 is reading A initially.
view Serializability
2) Updated Read:
T1 T2 T3 T1 T2 T3
W(A) W(A)
W(A) R(A)
R(A) W(A)
S1 S2
e.g.: Two schedule are not view equal as in S1 :T3 is reading A updated by T2, in S2
T3 is reading A updated by T1.
view Serializability
3) Final write:
If a transaction T1 updated A at last in S1, then in S2 also T1 should perform final write
operations.
T1 T2 T1 T2
R(A) R(A)
W(A) W(A)
W(A) W(A)
S1 S2
e.g.: Two schedule are not view as Final write operation in S1 is done by T1 while
in S2 done by T2.
view Serializability
View Serializability: If a schedule is view equivalent to its serial schedule then the
given schedule is said to be View Serializable.
Example:
Lets check the three conditions of view
serializability:
1. Initial Read
2. Update Read
3. Final Write
A lost update problem occurs when two transactions that access the same database items have their
operations in a way that makes the value of some database item incorrect.
In other words, if transactions T1 and T2 both read a record and then update it, the effects of the first
update will be overwritten by the second update.
PROBLEMS WITH CONCURRENCY CONTROL
T1 T2 Value of variables T1 T2 Value of variables
Read(L,b) b=5000 Read(L,b) b=5000
b = b+1000 b=6000 Read(L,b) B 5000
Write(b,L) L=6000 b = b+1000 B 6000
Read(L,b) b=6000 b = b-2000 B 3000
b = b-2000 b=4000 Write(b,L) B 6000
Write(b,L) L=4000 Write(b,L) L 3000
A dirty read problem occurs when one transaction updates a database item and then the transaction
fails for some reason. The updated database item is accessed by another transaction before it is
changed back to the original value.
In other words, a transaction T1 updates a record, which is read by the transaction T2.
Then T1 aborts and T2 now has values which have never formed part of the stable database.
PROBLEMS WITH CONCURRENCY CONTROL
T1 T2 Value of variables
Read(L,b) L ,B=5000
b = b+1000 B 6000
Write(b,L) L 6000
Read(L,b) B 6000
b = b-2000 B 4000
Roll Back L 5000
Write(b,L) L 4000
Unrepeatable read (or inconsistent retrievals) occurs when a transaction calculates some summary
(aggregate) function over a set of data while other transactions are updating the data.
The problem is that the transaction might read some data before they are changed and other data
after they are changed, thereby yielding inconsistent results.
RECOVERABLE SCHEDULE
What…
If a transaction Tj reads a data items previously written by a transaction Ti , the commit
operation of Ti appears before the commit operation of Tj. Then Schedule is called
Recoverable Schedule.
The following schedule is not recoverable if T9 commits immediately after the read.
Why???
Because failure of transactions might bring the system into an irreversibly
inconsistence state
CASCADING ROLLBACK
If T10 fails, T11 and T12 must also be rolled back.
Can lead to the undoing of a significant amount of
work