0% found this document useful (0 votes)
89 views62 pages

Subject: Database Management System CODE: 4CS4 - 05 UNIT: 04 Transaction Processing

This document discusses transaction processing in a database management system. It defines a transaction as a single logical unit of work that accesses and possibly modifies database contents. Transactions have four key properties: atomicity, consistency, isolation, and durability. The document describes these properties and explains transaction states, concurrent execution of transactions, and schedule types. It also covers topics like data integrity constraints and how isolation ensures transactions execute concurrently without interfering with each other.

Uploaded by

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

Subject: Database Management System CODE: 4CS4 - 05 UNIT: 04 Transaction Processing

This document discusses transaction processing in a database management system. It defines a transaction as a single logical unit of work that accesses and possibly modifies database contents. Transactions have four key properties: atomicity, consistency, isolation, and durability. The document describes these properties and explains transaction states, concurrent execution of transactions, and schedule types. It also covers topics like data integrity constraints and how isolation ensures transactions execute concurrently without interfering with each other.

Uploaded by

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

SUBJECT: DATABASE MANAGEMENT SYSTEM

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.

• Set of changes that must all be made together.


Transaction concept…
• A transaction access the data with a series of Read and Write operations.

• Read operation: Reading of Database Object means

Disk Main Memory Program Variable

• Write operation: Writing of Database Object means

Program Variable Main Memory Disk


Transaction properties…

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.

Then amount has been deducted from A but not


added to B.

This results in an inconsistent database state.


FAILS
So in this case whole transaction needs to be roll
backed (Abort)
Transaction properties…
Consistency:
• It says that that integrity constraints must be maintained so that the database is
consistent before and after the transaction.
• It refers to the correctness of a database.

(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.

Total before T occurs = 2000 + 3000 = 5000.


Total after T occurs = 1300 + 3700 = 5000.
Data integrity constraints..
Various types of integrity constraints are-
• Domain Integrity Constraint
• Entity Integrity Constraint
• Referential Integrity Constraint
1. Domain Integrity Constraint-
Domain integrity means the definition of a valid set of values for an attribute.

2. Entity Integrity Constraint-


This rule states that in any database relation value of attribute of a primary key can't
be null.

3.Referential Integrity Constraint-


It states that if a foreign key exists in a relation then either the foreign key value
must match a primary key value of some tuple in its home/master relation or the
foreign key value must be null.
Data integrity constraints..
P.K. STU-ADD (MASTER TABLE) F.K. STU-MARKS (CHILD TABLE)
R.NO Name Address Place PIN R.NO Subject Exam-Date MARKS
1 Rakshit G.Kailash Delhi 90 1 Java 12-02-09 95
2 Shilpa South Ex. Delhi 80 2 Java 12-02-09 80
3 Rajan Ramganj Jaipur 92 2 C++ 15-03-10 81
4 Rajat Adarash Nagar Ajmer 34 3 Win-98 25-07-11 79

Can the STU-MARKS table contain a row for Roll No = 5 ? NO


Can the delete the row of Roll no = 02 from STU-ADD table ? NO
Can the delete the row of Roll no = 04 from STU-ADD table ? YES
Data integrity constraints..
The rules are:

• 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:

Value read by T’’ ?


X = 50,000 &
Y = 500
Sum computed by
T’’: (X+Y = 50, 000+500=50, 500)

is thus not consistent with the sum at end of


Consider two transactions T and T”. transaction:
Let X= 500, Y = 500.

After T: X = 50,000 & Y = 450 and so


After T’’ : X+Y = 50, 000 + 450 = 50, 450
Transaction properties…
Durability:

• 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…

S.No. Property Meaning

1. Atomicity All or Nothing

2. Consistency No Violation of Integrity constraints

3. Isolation Concurrent Changes invisible

4. Durability Committed Update/Modification


Persist, even if system fails
Transaction states…

TRANSACTION STATE DIAGRAM


Transaction states…
 Active:
 Initial State
 Transaction is in this state while it is executing.
 Partially committed:
 When the final statement is executed.
 Failed:
 When the normal execution is no longer possible.
 Logical error (e.g. bad input), system error (Deadlock), System crash.

 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

users and without loss of data due to failures.

Examples of transactions are:

• Withdrawing cash at an A.T.M.

• Making an airline reservation and

• Registering for a course.


Concurrent execution
• Concurrent execution of the transaction means executing more then one
transaction at the same time.
• For that we use the technique of interleaving execution of two
transactions.
• Interleaving: Firstly T1 executes then T2 finally T1.
T1 T2
Read(A);
Write(A);
Read(B);
Write(B);
Read(C);
Write(C);

INTERLEAVING
Concurrent execution
Advantages of Concurrent execution:

1. Increased CPU Utilization: also known as System Throughput: means


average number of transactions completed in given time.
2. Better Average Response Time: short transactions will take less time in
compare with serial execution.
3. Better Average Turnaround Time: TAT is time between transaction
submission and transaction completion.
4. Better Average Waiting Time: WT means time required by transaction
to wait to get executed.
5. Better disk Utilization: disk will be idle for lesser amount of time
because of overlapping of I/O activities.
Concurrent execution
Concurrency control schemes –
Mechanisms to achieve isolation
i.e., to control the interaction among the concurrent transactions in order to
prevent them from destroying the consistency of the database

 Will study in next unit


schedule
Schedule:

 A chronological execution sequence of a transaction is called a schedule.


 A schedule can have many transactions in it, each comprising of a number
of instructions/tasks.
 A schedule for a set of transactions must consist of all instructions of
those transactions
 Must preserve the order in which the instructions appear in each
individual transaction.
Schedule - types
Types of Schedule:

 Complete Schedule: if a schedule contains with an abort or a commit


for each transaction in schedule.

 Serial Schedule: in which the operations from a set of concurrent


transactions are not interleave

 Non - Serial Schedule:in which the operations from a set of concurrent


transactions are interleaved.
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;
A = A / 10;
A = A / 10; A = A / 10; B = B - 10;
B = B * 4; B = B * 4;
B = B * 4;

Schedule – 1 and Schedule – 2 are ? (Serial or Non-Serial) SERAIL SCHEDULE


Schedule – 3 is ? (Serial or Non-Serial) NON-SERAIL SCHEDULE
Schedule – 1 is ? (Interleaved or Non-interleaved) NON-INTERLEAVED
Schedule – 2 is ? (Interleaved or Non-interleaved) NON-INTERLEAVED
Schedule – 3 is? (Interleaved or Non-interleaved) INTERLEAVED
Schedule - types
Types of Schedule:

 Equivalent Schedule: Two schedule are


equivalent if they produce the same result
on the database state.

Let T1 transfer 50 from A to B, and T2


transfer 10% of the balance from A to B.

The following is a serial schedule in


which T1 is followed by T2…..
Schedule - types
Schedule - 1 Schedule - 2 Schedule - 3

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

Schedule 1 and 2 are? (Equivalent or Non-Equivalent) EQUIVALENT


Schedule 1 and 3 are? (Equivalent or Non-Equivalent) EQUIVALENT

Schedules-3 is equivalent to Schedules-1 and Schedules-2.


Schedule - types
Types of Schedule:

 A non – serial schedule that is equivalent to some


Serializable schedule:
serial execution of transactions is known as serializable schedule.
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;
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.

• Process of converting a non-serial schedule into its equivalent serial


schedule is known as serializability.

• Two Types of Serializability are :


1. Conflict serializability
2. View serializability
Conflict Serializability
Three terms needs to be understand
1. Conflict Operations.
2. Conflict Equivalent.
3. Conflict Serializability.

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…

Operations are conflict, if they satisfy all three of the following


conditions :
 They belong to different transactions
 They access the same data item
 At least one of the operation is a write operation.

READ(X) WRITE(X)

READ(X) NO YES

WRITE(X) YES YES


Conflict Serializability…
Conflict Operations:
T1 T2

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)

Write(A) So Swap Write(A)

Write(B) Write(B) No Conflict:


Read(B) So Swap
Read(B)
Write(B) Write(B)
Conflict Serializability…

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:

If a transaction T1 reading data item A from initial database in S1 then in S2 also T1


should read A from initial database.
T1 T2 T3 T1 T2 T3
R(A) R(A)
W(A) W(A)
R(A) R(A)
R(B) R(B)

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:

If Ti is reading A which is updated by Tj in S1 then in S2 also Ti should read A which is


updated by Tj.

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

In schedule S1, transaction T1 first reads the data


item X. In S2 also transaction T1 first reads the data
item X.
Lets check for Y. In schedule S1, transaction T1 first
reads the data item Y. In S2 also the first read
operation on Y is performed by T1.
We checked for both data items X & Y and the initial
read condition is satisfied in S1 & S2.
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:

2. Update Read

In S1, transaction T2 reads the value of X, written by


T1. In S2, the same transaction T2 reads the X after it
is written by T1.
In S1, transaction T2 reads the value of Y, written by
T1. In S2, the same transaction T2 reads the value of
Y after it is updated by T1.
The update read condition is also satisfied for both
the schedules.
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:

3. Final Write

In schedule S1, the final write operation on X is done


by transaction T2. In S2 also transaction T2 performs
the final write on X.
Lets check for Y. In schedule S1, the final write
operation on Y is done by transaction T2. In schedule
S2, final write on Y is done by T2.
We checked for both data items X & Y and the final
write condition is satisfied in S1 & S2.
view Serializability

Result:  Since all the three conditions are satisfied


S1 and S2 are view equivalent.
Also, as we know that the schedule S2 is the serial schedule of S1, thus we can say
that the schedule S1 is view serializable schedule.

NOTE: EVERY CONFLICT SERIALIZABLE SCHEDULE IS ALSO VIEW


SERIALIZABLE. BUT VICE VERSA IS NOT TRUE.
PROBLEMS WITH CONCURRENCY CONTROL
When concurrent transactions occurs in an uncontrolled manner then several problems
can occur:

1. Lost Update Problem

2. Dirty Read Problem

3. Unrepeatable Read Problem


PROBLEMS WITH CONCURRENCY CONTROL
1. Lost Update Problem OR WW conflicts

Transaction- A Time Transaction- B


----- t0 ---- At time t1 , Transactions-A reads value of X.
At time t2 , Transactions-B reads value of X.
Read X t1 ---- At time t3, Transactions-A writes value of X on the basis  of the value seen at
---- t2 Read X time t1.
At time t4, Transactions-B writes value of X on the basis  of the value seen at
Update X t3 ----
time t2.
---- t4 Update X So, Update of Transactions-A is lost at time t4,because Transactions-B
---- t5 ---- overwrites it without looking at its current value.

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

T2 Overwrites the value of L


updated by T1
SO T1 Lost its updates.
PROBLEMS WITH CONCURRENCY CONTROL
2. Dirty Read Problem: OR Uncommitted data OR WR CONFLICT
Transaction- A Time Transaction- B
At time t1 , Transactions-B writes value of X.
---- t0 ---- At time t2 , Transactions-A reads value of X.
---- t1 Update X
At time t3 , Transactions-B rollbacks. So,it changes the value of X back to that
of prior to t1.
Read X T2 ---- So,Transaction-A now has value which has never become part of the stable
---- T3 Rollback database.
---- T4 ----

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

T2 is reading the value which


has been rolled back :
DIRTY READ`
PROBLEMS WITH CONCURRENCY CONTROL
3. Unrepeatable Read OR Inconsistent Retrievals OR RW CONFLICT

Transaction- A Time Transaction- B


----- t0 ----
A1 = 200, A2 = 250, A3 = 150 Read (A1) t1 ----
  Sum = A1 (200)
Read (A2)
Sum = Sum + A2 (450) t2 ----
---- t3 Read (A3)
Transaction-A is summing all balances while, ---- t4 A3 = A3-50 (100)
Transaction-B is transferring an amount 50 ---- t5 Read (A1)
from Account-3 to Account-1. ---- t6 Update Balance of Acc-1
 A1 = A1+50 (250)
------ t7 COMMIT 
Read (A3)
Sum = Sum + A3 (550) t8 ----
PROBLEMS WITH CONCURRENCY CONTROL
3. Unrepeatable Read OR Inconsistent Retrievals:

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

Cascading rollback – a single transaction failure leads to a series of transaction


rollbacks.
Consider the following schedule where none of the transactions has yet
committed (so the schedule is recoverable)
THANKS

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy