DBMS
DBMS
MAY -24
Q3) a) Explain two phase locking protocol with example. [6]
The Two-Phase Locking Protocol (2PL) ensures conflict serializability by dividing the
execution of a transaction into two distinct phases:
1. Growing Phase:The transaction acquires all the locks it needs (shared or
exclusive).No lock is released during this phase.
2. Shrinking Phase:The transaction releases locks it no longer requires.No new
locks are acquired in this phase.
This protocol ensures that once a transaction releases a lock, it cannot request any more
locks, preventing cyclic dependencies.
Example:Consider two transactions T1T_1T1 and T2T_2T2:
• T1T_1T1: R(A),W(A),R(B),W(B)R(A), W(A), R(B), W(B)R(A),W(A),R(B),W(B)
• T2T_2T2: R(B),W(B),R(A),W(A)R(B), W(B), R(A), W(A)R(B),W(B),R(A),W(A)
Schedule following 2PL:
1. T1T_1T1: Acquires S lock on AAA → Reads AAA.
2. T1T_1T1: Upgrades to X lock on AAA → Writes AAA.
3. T2T_2T2: Waits for T1T_1T1 to release AAA.
4. T1T_1T1: Acquires S lock on BBB → Reads BBB.
5. T1T_1T1: Upgrades to X lock on BBB → Writes BBB.
6. T1T_1T1: Releases locks on AAA and BBB.
7. T2T_2T2: Acquires S lock on BBB → Reads BBB, and so on.
Conflict Serializability:
• The schedule is equivalent to executing T1T_1T1 followed by T2T_2T2, ensuring
serializability.
b) What are the ACID properties of the transaction? [6]
Atomicity:A transaction is an indivisible unit; all its operations are executed, or none
are.Example: In a fund transfer, either both debit and credit operations complete, or
neither does.
Consistency:Transactions transform the database from one consistent state to
another while preserving integrity constraints.Example: A bank database with a rule
that total account balance should remain constant before and after a transaction.
Isolation:Transactions execute independently without interference, ensuring
intermediate states are invisible to other transactions.Example: Two users transferring
money should not affect each other's operations.
Durability:Once a transaction commits, its changes are permanent, even in the event
of a system crash.Example: After a successful withdrawal, the new account balance
persists despite server failures.
b) Explain how beadlock occurs? Which are the actions required for the
deadlock recovery process? [6]
How Deadlock Occurs:Deadlock occurs when two or more transactions are waiting
indefinitely for resources held by each other, creating a cycle of dependency.
• Example:
o Transaction T1 holds Resource R1 and waits for Resource R2.
o Transaction T2 holds Resource R2 and waits for Resource R1.
Actions for Deadlock Recovery:
1. Deadlock Detection:Use a wait-for graph to identify cycles among transactions.
2. Transaction Abortion:Abort one or more transactions involved in the deadlock
to break the cycle.Prefer aborting transactions with fewer updates or lower
priority.
3. Resource Preemption:Temporarily take resources from one transaction and
assign them to another to resolve the deadlock.
4. Timeout-Based Recovery:If a transaction waits beyond a certain threshold, it is
assumed to be in deadlock and is aborted.
c) Define the following terms. [5]
i) Concurrency ii) Timestamp iii) Timestamp ordering iv) Schedule v) Transaction
i) Concurrency:The simultaneous execution of multiple transactions to improve the
performance of a database system.Ensures correctness using protocols like locking or
timestamp ordering.
ii) Timestamp:A unique identifier assigned to each transaction or operation, typically
based on the time of its initiation.Used to maintain the sequence of operations and
ensure consistency.
iii) Timestamp Ordering:A concurrency control protocol that ensures transactions are
executed in the order of their timestamps.Older transactions are given higher priority to
prevent conflicts.
iv) Schedule:The sequence in which database operations (read/write) of transactions are
executed.Example: Serial schedules and non-serial schedules.
v) Transaction:A logical unit of work performed by a database, which must be either
fully completed (commit) or completely undone (rollback).Properties of transactions are
governed by ACID principles (Atomicity, Consistency, Isolation, Durability).
Schedule Given:
T1: R(X), W(X), W(Z)
T2: W(Y)
T3: W(X)
Step 1: View Serializability Rules
A schedule is view serializable if it can be transformed into a serial schedule (executing
transactions one after another) while preserving the following:
1]Initial Read: The first read operation on a data item must match the serial
schedule.2]Write-After-Read: If a transaction reads a value written by another, the
same order must exist in the serial schedule.3]Final Write: The last write operation on
any data item must match the serial schedule.
Step 2: Analyze the Schedule 1]T1 reads and writes X, and writes Z. 2]T2 writes Y.
3]T3 writes X after T1. 4]The final write on X is by T3, and the final write on Y is by T2.
Justification:The order of operations on X ensures no conflicts with view serializability
because T1 writes first, and T3 writes last.The operations on Y (T2) are independent of
others.There are no violations of initial read, write-after-read, or final write rules.
Conclusion:The schedule is view serializable as it can be rearranged into a serial order:
T1 → T2 → T3 or T1 → T3 → T2.
State Diagram:
Start
↓
Active
↙ ↘
Aborted Partially Committed
↓ ↓
Failed Committed
• From Active, a transaction can either commit (move to Partially Committed) or
fail (move to Failed/Aborted).
• From Partially Committed, it moves to Committed if successful.
• From Aborted, it may restart or terminate entirely.
DEC - 23
Q3) State and explain the ACID Properties. During its execution, a transaction
passes through several states, untill it finally commits or aborts.
a) List all possible sequenices of states through which a transaction may
pass. Explain the situations when each state transaction occurs. [6]
ACID Properties of a Transaction
1. Atomicity:Ensures that a transaction is treated as a single unit. All operations
must either execute completely or not at all.Example: A bank transfer must debit
one account and credit another; if one operation fails, both are rolled back.
2. Consistency:Guarantees that the database transitions from one valid state to
another, maintaining all rules and constraints.Example: Transferring funds
between accounts must ensure the total funds remain constant.
3. Isolation:Ensures transactions do not interfere with each other. Intermediate
states of a transaction are invisible to other transactions.Example: While a
transaction updates an account, another transaction should not access it until the
update is committed.
4. Durability:Ensures that committed changes are permanent, even in case of a
system crash.Example: After a transaction commits, the updated data remains
saved.
Serial Schedule
Definition:
A schedule is serial if the operations of each transaction are executed sequentially, with
no interleaving of operations from other transactions. It preserves the integrity of
transactions because each transaction is executed independently of others.
Example of Serial Schedule:
Transactions:T1:
Read(A); A = A + 10; Write(A);T2:
Read(B); B = B * 2; Write(B);
Serial Schedule:1]Execute all operations of T1: T1: Read(A) → A = A + 10 → Write(A)
2]Then execute all operations of T2: T2: Read(B) → B = B * 2 → Write(B)
Properties of Serial Schedule:1]Consistency:Produces a consistent state of the database
since transactions do not interfere with one another.2]No Concurrency Issues:No
conflicts arise because transactions are executed sequentially.
Advantages of Serial Schedule:Simple to implement.Always maintains database
consistency.
Disadvantages of Serial Schedule:Low efficiency due to lack of concurrency.Slower
performance in multi-user environments.
2. View Serializability
• Definition: A schedule is view-serializable if it produces the same final result as a
serial schedule, even if operations cannot be swapped as in conflict serializability.
• Conditions for View Serializability:
1. Initial Reads: Each transaction reads the same initial value as in the serial
schedule.
2. Writes: The write operations result in the same final value for all data
items.
3. Read-After-Write Dependency: The transactions that read a value written
by another transaction must do so in the same order as in the serial
schedule.