DBMS
DBMS
Unit-5
Syllabus
Concurrency Control
om
s
1. Data Consistency: Ensures the database remains accurate and reliable despite
simultaneous transactions.
2. Isolation: Guarantees that one transaction's intermediate states are not visible to
others.
3. Fair Resource Sharing: Allows multiple users to perform transactions efficiently.
4. Prevention of Anomalies: Resolves conflicts like lost updates, dirty reads, and other
concurrency-related issues.
Concurrency Problems
1. Dirty Read
Definition: A transaction reads uncommitted changes made by another transaction.
Impact: Inconsistent and unreliable data.
Prevention: Use the Read Committed isolation level or higher.
3. Phantom Read
M
Definition: A transaction re-executes a query and gets a different result set because
ul
another transaction has added or removed rows that match the query criteria.
ti
4. Lost Update
Definition: Two transactions read the same data and update it concurrently, but one
update overwrites the other, leading to a loss of data.
Impact: One update is lost, leading to incorrect results.
Prevention: Use locks or the Serializable isolation level.
5. Deadlock
Definition: Two or more transactions wait indefinitely for resources locked by each other,
creating a circular wait.
Impact: Transactions are unable to proceed, leading to a system stall.
Prevention: Deadlock detection and recovery mechanisms (e.g., timeout, resource
ordering).
on these timestamps.
ti
Here, T2 overwrites T1’s result, leading to a lost update. The correct balance should be ₹11,000, but
it’s now incorrect.
Example: Multiple transactions can read the balance of a bank account, but no one
can modify it until the lock is released.
ul
Symbol: S
ti
At
Lock Compatibility
Lock-Based Protocols:
Example:
Scenario: T1 wants to update the account balance by depositing ₹200, and T2 wants to read the
balance.
Steps:
1. T1 acquires a lock on the account balance.
2. T1 updates the balance: 500 + 200 = 700.
3. T1 releases the lock.
M
Problem: While T1 is holding the lock, T2 must wait, even though it only wants to read the data and
doesn't interfere with the update. This makes it inefficient.
ti
At
Description: Before a transaction starts executing, it first claims all the locks it
needs for the data items involved. It requests all the locks at once, and if all the locks
s
are granted, it proceeds with the operations. If any lock is not granted, the
transaction is rolled back.
Usage: This protocol is less flexible because it locks all data at the beginning, which
can lead to inefficiencies or deadlocks.
Example:
Scenario: T1 wants to update the balance, and T2 wants to read the balance.
Steps:
1. Before starting, T1 requests a write lock on the balance.
2. Ifthe system grants the lock, T1 updates the balance: 500 + 200 = 700.
3. After completing the update, T1 releases the lock.
4. T2 can now acquire a read lock and read the updated balance (700).
Problem: Pre-claiming locks upfront may cause deadlocks (e.g., T1 and T2 waiting for each other's
locks) or make transactions unnecessarily wait.
3. Two-Phase Locking Protocol (2PL):
Description: This is the most commonly used protocol in databases. It consists of two
phases:
1. Growing Phase: The transaction can acquire locks but cannot release any locks.
2. Shrinking Phase: Once a transaction releases any lock, it cannot acquire any more
locks.
Scenario: T1 is transferring ₹200 from account A to account B, and T2 is reading the balance of
account A.
M
Steps:
ul
Advantages: Transactions follow a systematic order: lock first, work, then release.
Problem: T2 cannot read the balance of account A while T1 is holding the lock.
This is a stricter version of the 2PL protocol. It makes sure that the transaction keeps
the locks until it’s completely finished and committed.
How it works:
A transaction holds on to its locks and doesn’t release them until it is ready to
commit (finish and confirm the work).
Once it commits, it releases all its locks at once.
Why it’s useful:
Scenario: T1 is transferring ₹200 from account A to account B, and T2 is reading the balance of
account A.
Steps:
1. T1 acquires a write lock on account A and account B (Growing Phase).
2. T1performs the transfer: Debit ₹200 from A: 500 - 200 = 300. & Credit ₹200 to B: 1000 + 200 =
1200.
3. T1 holds all locks until it commits.
4. After the transaction commits, T1 releases all locks (Shrinking Phase).
Advantage: Prevents cascading rollbacks. If T1 fails halfway, other transactions like T2 won’t see
incomplete or incorrect data.
M
ul
ti
At
om
s
How it works:
Read Timestamp (RTS): The largest timestamp of any transaction that successfully read
the data.
Write Timestamp (WTS): The largest timestamp of any transaction that successfully
wrote the data.
4. Rules:
If a transaction tries to read or write in a way that violates timestamp order, it is
aborted and restarted with a new timestamp.
Example:
M
Since T2 > T1, the operation is valid because it respects the timestamp order.
ti
But if T2 tries to write A before T1 completes, it would violate the timestamp order (because
At
How it differs:
1. Write operations by a transaction are delayed until all earlier transactions (with smaller
timestamps) have finished.
2. Read operations are delayed if there is a pending write operation by an earlier
transaction.
Avoids issues like cascading rollbacks, which occur in basic timestamp ordering.
Scenario:
Two transactions T1 (TS = 1) and T2 (TS = 2) are working on a bank account balance (initial =
ul
₹500).
ti
At
1. T1 (TS = 1) reads the balance (₹500) and writes a new balance (₹400).
RTS(A) = 1, WTS(A) = 1.
s
1. T1 (TS = 1) reads the balance (₹500) and writes a new balance (₹400).
Result: Final balance = ₹300, but changes are only applied after T1 commits.
Validation-Based Protocols AKTU - 2021-22 & 2022-23 & 2023-24
1. Read Phase:
M
The transaction reads data from the database and performs calculations or operations
ul
locally.
ti
2. Validation Phase:
Before committing, the system validates whether the transaction can be executed
s
3. Write Phase:
If the transaction passes the validation phase, it writes changes to the database
permanently.
If the validation fails, the transaction is aborted and restarted.
Differences from 2-Phase Commit (2PC)
Initial database:
ul
Steps:
om
1. T1 (Read Phase):
Reads A = ₹500, B = ₹1000.
s
2. T2 (Read Phase):
Reads A = ₹500, B = ₹1000.
Plans to credit ₹300 to A and debit ₹300 from B.
3. Validation Phase:
T1 checks:
No other transaction modified A or B after T1 read them.
T2 checks:
T1 modified A and B, which conflicts with T2's plans.
4. Write Phase:
T1 passes validation and writes changes:
A = ₹300, B = ₹1200.
T2 fails validation, aborts, and restarts.
Multiple Granularity
What is Granularity?
Granularity refers to the size of the data that can be locked in a database.
Small Granularity: Locking a single record (fine-grained).
Large Granularity: Locking an entire table or database (coarse-grained).
Problem:
ul
If a transaction (T1) needs to lock the entire database, locking every single
ti
record is inefficient.
If another transaction (T2) needs just one record, locking the whole database
At
Solution:
Multiple granularity allows locking different parts of the database (like records,
s
files, or areas) depending on the needs of the transaction. This balance improves
both efficiency and concurrency.
Hierarchy of Locks
The database is divided into levels, like a tree:
1. Database (highest level).
2. Area (e.g., a category in the database).
Intention Locks
ti
At
om
1. Intention-Shared (IS):
s
Database: IS mode.
om
1.
2. Area A1: IS mode.
Concurrency in MGL
T1, T3, and T4 can run simultaneously because their operations don’t conflict.
M
1. Data Versions:
Each data item has multiple versions, each with a timestamp or identifier.
A new version is created when a transaction updates the data.
Older versions remain available for transactions that need them.
Read Operation: A transaction reads the version of the data valid at its start time.
Write Operation: A transaction creates a new version with an updated value and
timestamp.
Key Benefits
M
ul
1. Read Consistency:
ti
Readers can access the version of the data item valid at the start of their transaction,
even if other transactions update the data simultaneously.
At
2. No Blocking Reads:
om
3. Reduced Conflicts:
Transactions can proceed without waiting for each other unless they modify the same
data.
Drawbacks
1. Increased Storage:
Multiple versions require more storage space.
2. Complexity:
Managing versions and ensuring they are valid for the right transactions can be
challenging.
3. Garbage Collection:
M
Old versions that are no longer needed must be removed, adding overhead.
ul
ti
When multiple transactions are running simultaneously, the system must ensure that the
database remains consistent, even if there’s a system failure (like a crash). Recovery
techniques help restore the database to a consistent state.
1. Immediate Update Techniques
Definition: Changes made by a transaction are immediately written to the database as soon as they
are performed, even before the transaction commits.
Benefit: Reduces recovery time since changes are already in the database.
Challenge: If the system crashes before the transaction commits, the database may become
inconsistent.
Log-Based Recovery
A log is a sequential record of all operations performed by transactions. It is critical for recovery.
3.During Recovery:
At
Example:
s
Recovery steps:
UNDO T1: Revert balance to ₹5000 using its UNDO log.
REDO T2: Reapply balance update to ₹4800 using its REDO log.
Checkpoints
During recovery, the system starts from the last checkpoint instead of scanning the entire log.
2. Reduces Overhead:
Single/Multi-User Environments
M
Single-User Environment:
ul
Multi-User Environment:
om
3.Undo Tablespace:
Oracle uses undo segments to store the original state of modified data. This enables:
Impact: Even if a transaction is rolled back, Oracle ensures that other users' views of the
ul
Oracle employs sophisticated algorithms to handle write conflicts between transactions. When
a conflict occurs:
s
The database determines the transaction with the higher priority based on timing or
access order.
The lower-priority transaction may be delayed or restarted.
Key Concepts
A transaction can acquire a lock on a data item only if it has already locked its
parent in the graph.
ul
3. Deadlock Avoidance:
At
2. A transaction locks data items starting from the highest node in the hierarchy.
3. The transaction can only lock children of a locked parent.
4. When finished, the transaction releases locks in a reverse order (from leaf to root).
Example
D
Hierarchy of Data Items in a Library System:
Database → Section → Book → Page S
B
M
Transactions:
1. T1: Read a specific page (P1) of a book (B1):
ul
P
ti
Deadlock Prevention:
If T1 locks Page P1, T2 cannot directly jump to modify Page P1 without following the
lock hierarchy.
Transactions must respect the lock order, preventing circular dependencies.