dbms lec
dbms lec
Unit – 4
Transaction processing
Course Outcomes
Course Description Mapping with Program
Outcomes Outcomes and Program
Specific Outcomes
CO1 PO[1,2,3,4,6,12]
Understand the data abstraction, data independence, various database models,
integrity constraints, and data operations.
CO2 PO[1,2,3,4,5,6,7,12]
Use SQL and relational algebra to create and manage databases, incorporating
advanced features like nested sub queries, views, and triggers.
CO3 Apply functional dependencies and normalization techniques to design efficient PO[1,2,4,6,12]
and consistent databases.
CO5 Understand and apply storage structures, indexing, query processing, and PO[1,2,3,4,5,6,9,11,12]
advanced database concepts
Assessment and Evaluation Plan
Example:
Consider an online shopping system where a transaction includes two
operations:
Deducting money from the buyer’s account.
Adding money to the seller’s account.
Both operations must either complete fully or not
at all. If the buyer’s account is debited but the
seller’s account is not credited (or vice versa), it
would lead to data inconsistency. By treating
these two actions as a transaction, we ensure
that either both happen or neither does.
Atomicity
Consistency
Isolation
Durability
If the system crashes after deducting from Account A but before adding to Account
B, the database should roll back all the changes, meaning:
Account A’s balance should not reflect the 1200 INR deduction.
Account B’s balance should not reflect the 1200 INR addition.
This ensures that the transaction is atomic, and the database will not be left in an
inconsistent state where only part of the transaction is completed. Instead, the
transaction will be either fully applied (if both steps succeed) or fully reverted (if any
part fails). Prepared by : Dr. Ahmad Jamal
Consistency
Consistency in the context of database transactions refers to the property that a
transaction must take the database from one valid state to another, maintaining
all predefined rules, constraints, and relationships defined in the database
schema.
The four primary isolation levels are defined by the SQL standard:
1: Read Uncommitted: A transaction can read data that has been modified by
another transaction but not yet committed.
Problem: This allows dirty reads, where a transaction can read data that might later
be rolled back, leading to inconsistent results.
Problem: This prevents dirty reads, but non-repeatable reads can still occur. This means a value
read by a transaction may change if the transaction re-reads the same data, as other
transactions may have modified it in the meantime.
Example: Transaction 1 reads a balance, but before it commits, Transaction 2 updates the
balance. When Transaction 1 reads the balance again, it sees a different value.
3: Repeatable Read: A transaction can read the same data multiple times and will always see
the same value, even if other transactions modify that data.
Problem: This prevents non-repeatable reads but still allows phantom reads. A phantom read
occurs when a transaction reads a set of rows that satisfy a certain condition, but another
transaction inserts or deletes rows that would affect that set of rows.
Example: Transaction 1 reads all records for customers in a certain city, but Transaction 2
inserts a new customer. When Transaction 1 queries again, it seesPrepared by : result
a different Dr. Ahmad
dueJamal
to the
Transaction Isolation Levels
4: Serializable: The highest isolation level. It ensures that transactions are executed in such a
way that the final outcome is equivalent to some serial execution of the transactions (i.e., one
transaction completes before another begins).
Problem: This level provides the highest level of isolation but also the lowest concurrency and
performance. It prevents dirty reads, non-repeatable reads, and phantom reads.
Example: Transaction 1 and Transaction 2 both try to read and modify the same data, but they
are executed one after the other, ensuring no conflicts or inconsistencies.
Initial State:
Account A has a balance of 1000 INR.
Account B has a balance of 500 INR.
Transaction Steps:
Step 1: Deduct 200 from Account A, updating its balance to 800.
Step 2: Add 200 to Account B, updating its balance to 700.
Step 3: Commit the transaction.
Recovery Process:
After the system restarts, the database uses its transaction logs or checkpoint data to confirm the committed
transaction, reapplying any committed changes if needed, ensuring Account A and Account B reflect their
updated balances.
COMMIT;
Syntax:
ROLLBACK;
Effect Changes are saved to the database Discards all changes made since the
and visible to others. transaction began or since a savepoint.
Use Case Used when a transaction completes Used when a transaction fails or an error
successfully. occurs.
Scope Applies to the entire transaction. Can apply to the entire transaction or a
savepoint.
Error Handling Does not handle errors; assumes the Used to recover from errors by restoring the
transaction is correct. database to a previous state.
Prepared by : Dr. Ahmad Jamal
Concurrency in Transactions
Concurrency in transactions refers to the simultaneous execution of multiple transactions in a database. It is
essential in multi-user environments to improve performance and ensure the efficient use of system
resources. However, it introduces challenges in maintaining data consistency, integrity, and isolation.
Advantages of Concurrency
Increased Throughput: Multiple transactions can be executed concurrently, maximizing resource utilization.
Reduced Waiting Time: Users do not need to wait for other transactions to complete, improving response
time.
Improved Scalability: Supports multiple users and operations effectively.
Challenges in Concurrency
Dirty Read: A transaction reads data that has been modified but not yet committed by another transaction.
Non-Repeatable Read: A transaction reads the same data multiple times but gets different results because
another transaction modified it in the meantime.
Phantom Read: A transaction reads a set of rows, but the result changes because another transaction
added or deleted rows.
Lost Update: Two transactions modify the same data simultaneously, and one of the updates is lost.
Prepared by : Dr. Ahmad Jamal
Challenges in Concurrency
Phases of 2PC
2 - Prepare Phase:
Coordinator sends PREPARE to all participants.
Participants vote: YES (ready) or NO (not ready).
3 - Decision Phase:
If all vote YES: Coordinator sends COMMIT.
If any vote NO: Coordinator sends ROLLBACK.
Advantages of 2PC
Atomicity Across Nodes: Ensures that all systems commit or rollback as a single unit.
Consistency: Prevents partial commits, maintaining data integrity.
Disadvantages of 2PC
Blocking Protocol: Participants remain locked until the coordinator decides, reducing concurrency.
Single Point of Failure: If the coordinator crashes, the system can be left in an uncertain state.
Overhead: Increased communication and logging requirements. Prepared by : Dr. Ahmad Jamal
Two-Phase Commit (2PC) : Example
Money Transfer
A transaction involves transferring 500 from a bank account managed by System A to
another account in System B:
Coordinator sends PREPARE to System A and System B.
Both systems reply YES (ready to commit).
Coordinator sends COMMIT to both systems.
If System B had replied NO, the coordinator would send ROLLBACK to both systems,
reverting all changes.
To address 2PC’s blocking issue, the Three-Phase Commit Protocol introduces a third phase, adding a
timeout mechanism and reducing uncertainty in case of coordinator failure.
1. Locking Mechanisms
2. Timestamp Ordering
3. Optimistic Concurrency Control
4. Multiversion Concurrency Control (MVCC)
5. Serializability
6. Two-Phase Locking (2PL)
7. Query Serialization