DBMS Unit 4
DBMS Unit 4
A transaction is a logical unit of work that contains one or more SQL statements. It ensures that a
group of operations either all succeed together or all fail together, maintaining the consistency of the
database.
• Data consistency
Example of a transaction:
sql
CopyEdit
BEGIN;
COMMIT;
A reliable DBMS must ensure ACID properties for transactions. These are:
A – Atomicity:
Example:
If the debit from Account A succeeds but the credit to Account B fails, the system must undo the
debit to maintain correctness.
C – Consistency:
I – Isolation:
• Each transaction should act as if it were the only one in the system.
Example:
Two people transferring funds at the same time should not see each other’s partial changes.
Isolation Levels:
• Read Uncommitted
• Read Committed
• Repeatable Read
• Serializable
D – Durability:
• Once a transaction is committed, its effects are permanent, even if a system crash follows.
Example:
After a successful money transfer, even if the power goes out, the changes remain intact.
3. States of a Transaction:
Command Description
sql
CopyEdit
BEGIN;
SAVEPOINT qty_update;
ROLLBACK TO qty_update;
COMMIT;
1. Introduction to Concurrency Control
Concurrency control is the technique used to ensure that concurrent execution of transactions does
not lead to inconsistency of data.
Without proper concurrency control, the database might face several problems:
Inconsistent Analysis (Phantom Aggregate functions show wrong results due to concurrent
Read) inserts/deletes.
3. Serializability in DBMS
What is Serializability?
Serializability is the gold standard for correctness in concurrency control. A schedule (order of
operations) is said to be serializable if its outcome is equivalent to some serial execution (i.e.,
transactions executed one after another, with no interleaving).
Types of Serializability:
a) Conflict Serializability:
Example:
text
CopyEdit
b) View Serializability:
a) Lock-Based Protocols
b) Timestamp Ordering
Rules:
Phases:
1. Read
2. Validation
3. Write
5. Deadlocks in Concurrency
A deadlock occurs when two or more transactions wait indefinitely for resources locked by each
other.
Schedule:
css
CopyEdit
css
CopyEdit
Conclusion:
In a multi-user environment, transactions often access and modify the same data simultaneously.
Lock-based concurrency control is a method used in DBMS to synchronize concurrent transactions
by locking data items to prevent conflicts and ensure data consistency.
2. What is a Lock?
A lock is a mechanism used to restrict access to a data item. When a transaction holds a lock on a
data item, other transactions may be blocked from accessing that item depending on the type of
lock.
Types of Locks:
Two-Phase Locking (2PL) is a concurrency control protocol that ensures conflict serializability by
following two phases:
Phases of 2PL:
1. Growing Phase:
2. Shrinking Phase:
Example of 2PL:
css
CopyEdit
Benefits:
• Easy recovery.
A deadlock occurs when two or more transactions are waiting for each other to release locks,
forming a cycle of dependencies.
Example of Deadlock:
css
CopyEdit
→ Deadlock
Method Description
8. Limitations
Conclusion:
Lock-based concurrency control, particularly through the Two-Phase Locking protocol, ensures
transaction isolation and serializability. However, it must be complemented with effective deadlock
handling strategies to ensure smooth and efficient database operations in concurrent environments.
1. Introduction to Concurrency Control
Definition:
Timestamps Used:
Name Description
read_TS(X) The largest timestamp of a transaction that successfully read data item X
Working:
Assume transaction T has timestamp TS(T). When T tries to read or write a data item X, the following
rules are applied:
1. Read(X):
• If TS(T) < write_TS(X) → T is rolled back (because X has been modified by a newer
transaction).
2. Write(X):
Advantages:
• Ensures serializability.
Disadvantages:
Definition:
Optimistic Concurrency Control assumes that conflicts are rare. Transactions proceed without
restrictions and only check for conflicts at the end, during the validation phase.
OCC Phases:
1. Read Phase:
2. Validation Phase:
o The system checks whether the transaction’s read/write operations conflict with
other concurrently running transactions.
3. Write Phase:
Example:
Advantages:
• No locks, so no deadlocks.
• High parallelism.
Disadvantages:
Lock Usage No No
Conclusion
Both timestamp-based and optimistic concurrency control methods offer alternatives to traditional
lock-based mechanisms. While they prevent deadlocks and enable high concurrency, they are best
used in environments with low data contention to avoid excessive rollbacks or validation failures.
Database Recovery Management
(12 Marks)
Database Recovery refers to the techniques and procedures used to restore a database to a correct
state after a failure. Failures may occur due to hardware errors, software bugs, power outages,
transaction errors, or system crashes.
2. Types of Failures
Transaction Due to logical errors (e.g., divide by zero) or system errors (e.g., deadlock
Failure timeout)
System Crash Software crashes or hardware failures (CPU, memory) causing the system to halt
Disk Failure Physical issues with storage like bad sectors or complete crash
3. Recovery Concepts
a. ACID Properties
Recovery ensures:
b. Transaction States
4. Recovery Techniques
A. Log-Based Recovery
sql
CopyEdit
• Write-Ahead Logging (WAL): Logs are written before actual data updates.
sql
CopyEdit
<T1, START>
<T1, COMMIT>
• Updates are not written to the database until the transaction commits.
C. Immediate Update
• If failure happens:
D. Shadow Paging
5. Checkpointing
• Reduces recovery time by limiting how far back logs must be scanned.
Steps:
4. Resume transactions.
Recovery systems must handle multiple transactions executing at once. This involves:
• Ensuring serializability
7. Recovery Algorithms
8. Conclusion
Database recovery management is essential to maintain the integrity and durability of data in the
presence of failures. Through mechanisms like logging, checkpointing, and shadow paging, DBMS
can recover reliably and ensure that transactions complete correctly or rollback cleanly.