Module 5 Notes DBMS
Module 5 Notes DBMS
MODULE: 5
CONTENTS:
● Transaction Processing:
● Concurrency Control in Databases
● Database Recovery Techniques
Transaction processing
· Introduction to Transaction Processing
· Transaction and System concepts,
· Desirable properties of Transactions,
· Characterizing schedules based on recoverability,
· Characterizing schedules based on Serializability,
· Transaction support in SQL.
Textbook : Fundamentals of Database Systems, Ramez Elmasri & Shamkant B. Navathe, 7th
Edition, Pearson.
In a multi-user environment, several transactions may execute at the same time. This is known as
concurrent execution of transactions. While concurrency improves system performance and
resource utilization, it may lead to problems such as lost updates, dirty reads, and inconsistent data
if not properly controlled. Therefore, concurrency control techniques are required to ensure that the
outcome of executing transactions concurrently is the same as if they were executed one after
another.
In addition, transactions may fail due to system crashes, power failures, or logical errors. To
maintain database correctness, the system must be able to recover from such failures. Recovery
techniques ensure that the database is restored to a consistent state by undoing incomplete
transactions and redoing committed ones.
Thus, a Transaction Processing System provides mechanisms for concurrency control and recovery,
ensuring the properties of atomicity, consistency, isolation, and durability (ACID), which are
essential for reliable database operation.
o Single-user DBMS: Only one user can access the system at a time.
o Banking systems
o Insurance systems
Role of Multiprogramming
· This interleaved execution of processes is shown in Figure 20.1, where processes A and
B are executed in an interleaved manner.
Advantages of Interleaving
· Interleaving keeps the CPU busy when one process is waiting for I/O operations.
· The CPU switches to another process instead of remaining idle during I/O time, as
illustrated in Figure 20.1.
Parallel Processing
5.1.2 Transactions, Database Items, Read and Write Operations, and DBMS Buffers
o Insertion
o Deletion
o Update (modification)
o Retrieval
· Types of transactions:
o A disk block
o A record
· Each data item has a unique name (e.g., disk block address or record ID).
Steps in read_item(X):
· Copy disk block to main memory buffer (if not already present).
Steps in write_item(X):
Actual database update happens when the buffer is written back to disk (step 4).
DBMS Buffers and Buffer Replacement
· Common policy:
· A transaction has:
· Example:
§ Read-set = {X, Y}
§ Write-set = {X, Y}
o Inconsistent database
o Data conflicts
Several problems can occur when concurrent transactions execute in an uncontrolled manner
Example:
We consider an Airline reservation DB. Each records is stored for an airline flight which includes
Number of reserved seats among other information.
Transaction T1
· Transfers N reservations from one flight whose number of reserved seats is stored in the
database item named X to another flight whose number of reserved seats is stored in the database
item named Y.
Transaction T2
Occurs when two transactions that access the same DB items have their operations interleaved in a
way that makes the value of some DB item incorrect. Suppose that transactions T1 and T2 are
submitted at approximately the same time, and suppose that their operations are interleaved as
shown in Figure below
·Final value of item X is incorrect because T2 reads the value of X before T1 changes it in the
database, and hence the updated value resulting from T1 is lost.
For example:
N = 5 (T1 transfers 5 seat reservations from the flight corresponding to X to the flight corresponding
to Y)
· Occurs when one transaction updates a database item and then the transaction fails for some
reason
· Meanwhile the updated item is accessed by another transaction before it is changed back to
its original value
3. The Incorrect Summary Problem
If one transaction is calculating an aggregate summary function on a number of db items while other
transactions are updating some of these items, the aggregate function may calculate some values
before they are updated and others after they are updated.
Transaction T reads the same item twice and gets different values on each read, since the item was
modified by another transaction T` between the two reads. for example, if during an airline
reservation transaction, a customer inquires about seat availability on several [Link] the
customer decides on a particular flight, the transaction then reads the number of seats on that flight
a second time before completing the reservation, and it may end up reading a different value for the
item.
5.1.4 Why Recovery Is Needed
Whenever a transaction is submitted to a DBMS for execution, the system is responsible for making
sure that either
1. All the operations in the transaction are completed successfully and their effect is recorded
permanently in the database or
2. The transaction does not have any effect on the database or any other transactions. In the first
case, the transaction is said to be committed, whereas in the second case, the transaction is aborted
If a transaction fails after executing some of its operations but before executing all of them, the
operations already executed must be undone and have no lasting effect.
Types of failures
A hardware, software, or network error occurs in the computer system during transaction execution
Hardware crashes are usually media failures for example, main memory failure.
Some operation in the transaction may cause it to fail, such as integer overflow or division by zero
During transaction execution, certain conditions may occur that necessitate cancellation of the
transaction For example, data for the transaction may not be found
The concurrency control may decide to abort a transaction because it violates serializability or
several transactions are in a state of deadlock
5. Disk failure:
Some disk blocks may lose their data because of a read or write malfunction or because of a disk
read/write head crash.
Failures of types 1, 2, 3, and 4 are more common than those of types 5 or [Link] a failure of
type 1 through 4 occurs, the system must keep sufficient information to quickly recover from the
failure. Disk failure or other catastrophic failures of type 5 or 6 do not happen frequently; if they do
occur, recovery is a major task.
A transaction is an atomic unit of work that should either be completed in its entirety or not done at
all. For recovery purposes, the system keeps track of start of a transaction, termination, commit or
aborts.
READ or WRITE: specify read or write operations on the database items that are executed as part
of a transaction
END_TRANSACTION: specifies that READ and WRITE transaction operations have ended and
marks the end of transaction execution.
ROLLBACK: signals that the transaction has ended unsuccessfully, so that any changes or effects
that the transaction may have applied to the database must be undone, and the database is restored
to its previous consistent state.
Transaction States (as shown in Figure 20.4)
o Recovery protocols ensure that the transaction’s updates can be safely recorded
in the system log.
· If all required checks are successful, the transaction reaches the Commit point
and enters the committed state.
o In this state:
· After completion (either committed or rolled back), the transaction enters the
Terminated state.
· The log (or journal) keeps track of all transaction operations that affect the values of
database items.
· The log is stored on disk, so it is not affected by system failures except in the case of disk
failure or catastrophic failure.
· One or more main memory buffers hold the most recent portion of the log file.
· Log records are first written to the log buffer in main memory.
· When the log buffer becomes full, or when certain predefined conditions occur, the
contents of the log buffer are appended to the end of the log file on disk. In addition, the log is
periodically backed up to archival storage (such as tape) to protect against catastrophic
failures.
In the following log records, T represents a unique transaction identifier generated by the system.
1. [start_transaction, T]
Indicates that transaction T has started execution.
3. [read_item, T, X]
Indicates that transaction T has read the value of database item X.
4. [commit, T]
Indicates that transaction T has completed successfully and its effects can be
permanently recorded in the database.
5. [abort, T]
Indicates that transaction T has been aborted and its effects must be undone.
[Link] Commit Point of a Transaction
· Log entries are first written into a log buffer in main memory.
o When it is full, or
o Only log records already written to disk are used for recovery.
o Any part of the log not yet written to disk must be written to disk.
· This process is called force-writing the log buffer to disk before commit.
o Data pages
o Index pages
o Log pages
· The cache is divided into separate domains, each handling one type of page.
· Page replacement inside each domain uses LRU (Least Recently Used).
· Advantages:
· Limitation:
· Variations:
o GRU (Group LRU) replaces pages from the lowest-priority domain first.
· Used for queries that repeatedly scan the same pages, such as nested-loop joins.
· Frequently accessed pages (hot set) are kept in memory and not replaced until
processing finishes.
DBMIN Method
· Similar to the working set concept in operating systems, but applied per file in a query
Transactions must satisfy a set of important properties known as the ACID properties, which are
enforced by the DBMS through its concurrency control and recovery mechanisms. These
properties ensure that database transactions are executed reliably and maintain the correctness of
the database.
· Atomicity
· Consistency Preservation
· Isolation
o A transaction should execute as if it is running in isolation from other
transactions.
o Even when many transactions run concurrently, the execution of one transaction
should not affect others.
· Durability (Permanency)
o These changes must not be lost even in the case of System failure, Power
failure, Crash.
Schedule: When transactions are executing concurrently in an interleaved fashion, then the order
of execution of operations from all the various transactions is known as a schedule (or history).
● A schedule (or history) S of n transactions T₁, T₂, …, Tₙ is an ordering of the operations of
these transactions.
● Operations from different transactions may be interleaved in the schedule. For each
transaction Tᵢ, the operations must appear in the same order as they occur in Tᵢ itself.
● Total ordering: for any two operations in the schedule, one must occur before the other.
● Although schedules with partial ordering of operations are possible theoretically, total
ordering is assumed for simplicity.
For recovery and concurrency control, the important transaction operations are:
read_item(X)
write_item(X)
commit
abort
Notations for schedules:
bᵢ begin_transaction of Tᵢ
rᵢ(X) read_item(X) by Tᵢ
wᵢ(X) write_item(X) by Tᵢ
eᵢ end_transaction of Tᵢ
cᵢ commit of Tᵢ
aᵢ abort of Tᵢ
Transactions T₁ and T₂ are executed in an interleaved manner. Only read and write operations are
shown.
Schedule Sb:
Example:
● The operations r1(X) and w2(X) conflict, as do the operations r2(X) and w1(X), and the
operations w1(X) and w2(X).
● The operations r1(X) and r2(X) do not conflict, since they are both read operations;
● The operations w2(X) and w1(Y) do not conflict because they operate on distinct data items
X and Y; and
● The operations r1(X) and w1(X) do not conflict because they belong to the same transaction.
Intuitively, two operations are conflicting if changing their order can result in a different outcome.
read-write conflict:
write-write conflict:
Complete Schedule:
Given Schedules
Schedule Sc – Nonrecoverable
● Transaction T2 reads X written by T1.
● T2 commits before T1 commits.
● Later, T1 aborts.
● The value of X read by T2 becomes invalid.
● Since T2 is already committed, it would need to be rolled back.
● Rolling back a committed transaction is not allowed.
Therefore, Sc is a nonrecoverable schedule.
Schedule Sd – Recoverable
● T2 reads X from T1, but
● T2 commits only after T1 commits.
● If T1 commits, the value read by T2 is valid.
● No committed transaction needs to be rolled back. Therefore, Sd is a recoverable schedule.
Recoverable Schedules
In a recoverable schedule, a committed transaction is never rolled back. This preserves the
durability property of transactions. However, cascading rollback (cascading abort) can still
occur. Cascading rollback happens when a transaction reads data from another transaction
that later aborts, forcing it to abort as well.
Cascadeless Schedules
A schedule is cascadeless if a transaction reads only items written by committed transactions.
Since all read values are from committed transactions, No cascading rollback occurs. To ensure
this, read operations are delayed until the writing transaction commits or aborts.
Strict Schedules
A strict schedule is more restrictive than cascadeless. In a strict schedule, a transaction cannot read
or write a data item X, until the last transaction that wrote X has committed or aborted.
● The system can safely restore the before image (old value) of the data item.
● This simple undo method always works correctly for strict schedules.
Original value of X = 9
If T1 aborts, restoring the before image sets X back to 9
But T2 had already written X = 8
This leads to incorrect results
Hence, Sf is cascadeless but not strict
Now we characterize the types of schedules that are always considered to be correct when
concurrent transactions are executed. Such schedules are known as serializable schedules.
Suppose that two users—for example, two airline reservations agents—submit to the DBMS
transactions T1 and T2 in Figure 20.2 at approximately the same time. If no interleaving of
operations is permitted, there are only two possible outcomes:
1. Execute all the operations of transaction T1 (in sequence) followed by all the
operations of transaction T2 (in sequence).
2. Execute all the operations of transaction T2 (in sequence) followed by all the
operations of transaction T1 (in sequence).
These two schedules are called serial schedules.
If interleaving of operations is allowed, there will be many possible orders in which the system
can execute the individual operations of the transactions.
The concept of serializability of schedules is used to identify which schedules are correct when
transaction executions have interleaving of their operations in the schedules.
● Schedules A and B in Figures 20.5(a) and (b) are called serial because the operations of each
transaction are executed consecutively, without any interleaved operations from the other
transaction.
● In a serial schedule, entire transactions are performed in serial order: T1 and then T2 in
Figure 20.5(a), and T2 and then T1 in Figure 20.5(b).
● Schedules C and D in Figure 20.5(c) are called nonserial because each sequence interleaves
operations from the two transactions.
Serial Schedule: A schedule is serial if transactions execute one after another with no
interleaving of operations (e.g., T1 fully executes before T2, or vice versa).
● In practice, DBMS allows interleaving of operations to improve performance and resource
utilization. Serializability ensures correctness despite interleaving.
● A schedule is serializable if its effect on the database is equivalent to some serial schedule,
even though operations may be interleaved.
● Serializable schedules are considered correct because they preserve database consistency
as if transactions were executed serially.
● Serializability prevents problems such as:
❖ Lost updates
❖ Inconsistent reads
❖ Incorrect final database states
Conflict Equivalence
Two schedules are conflict-equivalent if they contain the same operations and all conflicting
operations occur in the same order.
Correctness of Schedules
● A nonserial schedule is considered correct if and only if it is serializable.
● This is because serial schedules are inherently correct.
● To determine whether a nonserial schedule is serializable, we must define when two
schedules are equivalent.
Result Equivalence
● Two schedules are result equivalent if they produce the same final database state.
● Limitations:
○ The same final state may occur by coincidence for specific initial values.
○ Does not guarantee equivalence for all database states.
○ Schedules may involve different transactions.
● Hence, result equivalence is unreliable for defining serializability.
Types of Schedule Equivalence:
1. Conflict Equivalence
2. View Equivalence
Two schedules are said to be conflict equivalent if the relative order of any two conflicting
operations is the same in both schedules.
Conflicting Operations
Types of Conflicts
If conflicting operations occur in different orders in two schedules, the effects may differ.
Hence, such schedules are not conflict equivalent.
Examples:
Read–Write Conflict
Schedule S1: r1(X) → w2(X)
Schedule S2: w2(X) → r1(X)
Result:
r1(X) may read different values in S1 and S2
Not conflict equivalent
Write–Write Conflict
Schedule S1: w1(X) → w2(X)
Schedule S2: w2(X) → w1(X)
Result:
Final value of X may differ
Future reads of X may read different values
Not conflict equivalent.
Conflict Serializability:
A schedule S is said to be (conflict) serializable if it is conflict equivalent to some serial schedule
S′. Conflict equivalence means that the relative order of all conflicting operations is the same in
both schedules.
Reordering Operations
● In a nonserial schedule, nonconflicting operations can be reordered without changing the
effect of the schedule.
Refer to the figure 20.5. Schedule D is equivalent to serial schedule A (T1 → T2).
Observations:
● r2(X) reads the value of X written by T1 in both schedules.
● Other read_item operations read values from the initial database state.
● T1 is the last writer of Y in both schedules.
● T2 is the last writer of X in both schedules.
Since all conflicting operations occur in the same order, schedule D is conflict equivalent to A. Therefore,
schedule D is serializable.
Operations r1(Y) and w1(Y) do not conflict with r2(X) and w2(X): They access different data items.
Hence, they can be moved before r2(X) and w2(X) to form the serial order T1 → T2.
○ T1 → T2
○ T2 → T1
● Conflicts preventing reordering:
○ r2(X) and w1(X) conflict
Cannot move r2(X) down to form T1 → T2
○ w1(X) and w2(X) conflict
Cannot move w1(X) down to form T2 → T1
● Since no equivalent serial schedule can be formed, schedule C is not serializable.
● Schedules A and B:
○ Are already serial schedules
○ Their precedence graphs have no cycles. Therefore, they are serializable
● Schedule C:
○ Its precedence graph contains a cycle
○ Hence, schedule C is not serializable
● Schedule D:
○ Its precedence graph has no cycle
○ Therefore, schedule D is serializable
○ The equivalent serial schedule is T1 followed by T2
● Figure 20.8(a) shows the read_item and write_item operations of three transactions.
● Two schedules are formed:
○ Schedule E
○ Schedule F
Analysis of Schedule E
Analysis of Schedule F
● In general:
○ A serializable schedule may have more than one equivalent serial schedule
● If the precedence graph allows multiple valid transaction orderings, then:
○ Multiple serial schedules are equivalent
● Figure 20.8(f) illustrates a precedence graph with two equivalent serial schedules
Steps:
Figure 20.8(a) shows the read_item and write_item operations in each transaction. Two schedules
E and F for these transactions are shown in Figures 20.8(b) and (c), respectively, and the precedence
graphs for schedules E and F are shown in Figures 20.8(d) and (e). Schedule E is not serializable
because the corresponding precedence graph has cycles. Schedule F is serializable, and the serial
schedule equivalent to F is shown in Figure 20.8(e). Although only one equivalent serial schedule
exists for F, in general there may be more than one equivalent serial schedule for a serializable
schedule. Figure 20.8(f) shows a precedence graph representing a schedule that has two equivalent
serial schedules. To find an equivalent serial schedule, start with a node that does not have any
incoming edges, and then make sure that the node order for every edge is not violated.
● The order in which operations execute depends on factors such as system load, transaction
submission time, and process priorities.
● Since these factors are dynamic and unpredictable, it is hard to determine in advance
whether a schedule will be serializable.
● Moreover, checking serializability after execution is impractical because, if the schedule
turns out to be nonserializable, the system may need to roll back transactions, which is costly
and inefficient.
● To address this problem, DBMSs adopt concurrency control protocols instead of testing
schedules after execution.
● These protocols enforce specific rules during transaction execution to ensure serializability
automatically.
● This proactive approach avoids the need to analyze completed schedules and prevents
incorrect executions from occurring in the first place.
● Since transactions are continuously entering the system, it is difficult to define the exact
beginning and end of a schedule.
● Therefore, only the operations of committed transactions are considered. A schedule is
regarded as serializable if its committed projection is equivalent to some serial schedule,
because the DBMS guarantees correctness only for committed transactions.
Two schedules S and S′ are said to be view equivalent if the following three conditions hold:
1. The same set of transactions participates in S and S′, and S and S′ include the same operations
of those transactions.
2. For any operation ri(X) of Ti in S, if the value of X read by the operation has been written by
an operation wj(X) of Tj (or if it is the original value of X before the schedule started), the
same condition must hold for the value of X read by operation ri(X) of Ti in S′.
3. If the operation wk(Y) of Tk is the last operation to write item Y in S, then wk(Y) of Tk must
also be the last operation to write item Y in S′.
Other Types of Equivalence of Schedules
Serializability can be too restrictive for some applications where correctness does not require strict
serial order.
In debit-credit transactions, updates involve addition and subtraction, which are commutative
operations (order does not affect final result).
A schedule may be non-serializable but still correct if each read–update–write sequence is not
improperly interrupted.
Traditional serializability ignores operation semantics, but correctness in some systems depends on
the meaning of operations.
In long-duration or distributed applications (e.g., CAD systems), relaxed consistency models like
eventual consistency are sometimes preferred over strict serializability.
Chapter-22
Concurrency Control in Databases:
we discuss a number of concurrency control techniques that are used to ensure the
noninterference or isolation property of concurrently executing transactions.
● Some of the main techniques used to control concurrent execution of transactions are
based on the concept of locking data items.
● A lock is a variable associated with a data item that describes the status of the item
with respect to possible operations that can be applied to it.
● Generally, there is one lock for each data item in the database.
● Locks are used as a means of synchronizing the access by concurrent transactions to
the database items.
❖ Several types of locks are used in concurrency control. To introduce locking concepts
gradually, first we discuss binary locks, which are simple but are also too restrictive for
database concurrency control purposes and so are not used much.
❖ Then we discuss shared/exclusive locks—also known as read/write locks—which provide
more general locking capabilities and are used in database locking schemes.
Binary Locks
● A binary lock can have two states or values: locked and unlocked (or 1 and 0, for
simplicity). A distinct lock is associated with each database item X.
● If the value of the lock on X is 1, item X cannot be accessed by a database operation that
requests the item. If the value of the lock on X is 0, the item can be accessed when requested,
and the lock value is changed to 1. We refer to the current value (or state) of the lock
associated with item X as lock(X).
● Two operations, lock_item and unlock_item, are used with binary locking.
● A transaction requests access to an item X by first issuing a lock_item(X) operation.
● If LOCK(X) = 1, the transaction is forced to wait.
● If LOCK(X) = 0, it is set to 1 (the transaction locks the item) and the transaction is
allowed to access item X.
● When the transaction is through using the item, it issues an unlock_item(X) operation,
which sets LOCK(X) back to 0 (unlocks the item) so that X may be accessed by other
transactions. Hence, a binary lock enforces mutual exclusion on the data item.
Lock Operation Properties
Lock Table
● It is desirable to relax conditions 4 and 5 in the preceding list in order to allow lock
conversion; that is, a transaction that already holds a lock on item X is allowed under certain
conditions to convert the lock from one locked state to another.
● For example, it is possible for a transaction T to issue a read_lock(X) and then later to
upgrade the lock by issuing a write_lock(X) operation.
● If T is the only transaction holding a read lock on X at the time it issues the write_lock(X)
operation, the lock can be upgraded; otherwise, the transaction must wait.
● It is also possible for a transaction T to issue a write_lock(X) and then later to downgrade
the lock by issuing a read_lock(X) operation.
● When upgrading and downgrading of locks is used, the lock table must include transaction
identifiers in the record structure for each lock (in the locking_transaction(s) field) to store
the information on which transactions hold locks on the item.
● The descriptions of the read_lock(X) and write_lock(X) operations in Figure 21.2 must be
changed appropriately to allow for lock upgrading and downgrading.
● Using binary locks or read/write locks in transactions, as described earlier, does not
guarantee serializability of schedules on its own. Figure 21.3 shows an example where the
preceding locking rules are followed but a nonserializable schedule may result. This is
because in Figure 21.3(a) the items Y in T1 and X in T2 were unlocked too early. This
allows a schedule such as the one shown in Figure 21.3(c) to occur, which is not a
serializable schedule and hence gives incorrect results.
● To guarantee serializability, we must follow an additional protocol concerning the
positioning of locking and unlocking operations in every transaction.
● A transaction is said to follow the two-phase locking protocol if all locking operations
(read_lock, write_lock) precede the first unlock operation in the transaction.
● Such a transaction can be divided into two phases: an expanding or growing (first) phase,
during which new locks on items can be acquired but none can be released; and a shrinking
(second) phase, during which existing locks can be released but no new locks can be
acquired.
● If lock conversion is allowed, then upgrading of locks (from read-locked to write-locked)
must be done during the expanding phase, whereas downgrading of locks (from write-locked
to read-locked) must be done in the shrinking phase.
1. Transactions T1 and T2 in Figure 21.3(a) do not follow the two-phase locking protocol
because the write_lock(X) operation follows the unlock(Y) operation in T1, and similarly
the write_lock(Y) operation follows the unlock(X) operation in T2.
2. If we enforce two-phase locking, the transactions can be rewritten as T1′ and T2′, as shown
in Figure 21.4.
3. Now, the schedule shown in Figure 21.3(c) is not permitted for T1′ and T2′ (with their
modified order of locking and unlocking operations) under the rules of locking described in
Section 21.1.1 because T1′ will issue its write_lock(X) before it unlocks item Y.
4. Consequently, when T2′ issues its read_lock(X), it is forced to wait until T1′ releases the
lock by issuing an unlock(X) in the schedule.
5. However, this can lead to deadlock. It can be proved that, if every transaction in a schedule
follows the two-phase locking protocol, the schedule is guaranteed to be serializable,
obviating the need to test for serializability of schedules. The locking protocol, by enforcing
two-phase locking rules, also enforces serializability.
6. Two-phase locking may limit the amount of concurrency that can occur in a schedule
because a transaction T may not be able to release an item X after it is through using it if T
must lock an additional item Y later. Or, conversely, T must lock the additional item Y
before it needs it so that it can release X.
7. Hence, X must remain locked by T until all items that the transaction needs to read or
write have been [Link] then can X be released by [Link], another transaction
seeking to access X may be forced to wait, even though T is done with [Link], if Y
is locked earlier than it is needed, another transaction seeking to access Y is forced to wait
even though T is not using Y yet.
8. This is the price for guaranteeing serializability of all schedules without having to check
the schedules themselves.
9. Although the two-phase locking protocol guarantees serializability (that is, every schedule
that is permitted is serializable), it does not permit all possible serializable schedules (that
is, some serializable schedules will be prohibited by the protocol).
Strict 2PL
➢ In practice, the most popular variation of 2PL is strict 2PL, which guarantees strict
schedules.
➢ In this variation, a transaction T does not release any of its write-locks until after it commits
or aborts.
➢ Hence, no other transaction can read or write an item that is written by T unless T has
committed, leading to a strict schedule for recoverability.
Strict 2PL is not deadlock-free.
A more restrictive variation of strict 2PL is rigorous 2PL, which also guarantees strict
schedules.
In this variation, a transaction T does not release any of its locks (exclusive or shared) until
after it commits or aborts, and so it is easier to implement than strict 2PL.
1. The former holds write-locks until it commits, whereas the latter holds all locks (read and
write).
2. Also, the difference between conservative and rigorous 2PL is that the former must lock
all its items before it starts, so once the transaction starts it is in its shrinking phase.
3. The latter does not unlock any of its items until after it terminates (by committing or
aborting), so the transaction is in its expanding phase until it ends
Usually, the concurrency control subsystem itself is responsible for generating the
read_lock and write_lock [Link] example, suppose the system is to enforce the strict
2PL [Link], whenever transaction T issues a read_item(X), the system calls the
read_lock(X) operation on behalf of [Link] the state of LOCK(X) is write_locked by some
other transaction T′, the system places T in the waiting queue for item [Link], it grants
the read_lock(X) request and permits the read_item(X) operation of T to execute.
● On the other hand, if transaction T issues a write_item(X), the system calls the
write_lock(X) operation on behalf of T.
● If the state of LOCK(X) is write_locked or read_locked by some other transaction T′, the
system places T in the waiting queue for item X.
● If the state of LOCK(X) is read_locked and T itself is the only transaction holding the read
lock on X, the system upgrades the lock to write_locked and permits the write_item(X)
operation by T.
● Finally, if the state of LOCK(X) is unlocked, the system grants the write_lock(X) request
and permits the write_item(X) operation to execute.
● After each action, the system must update its lock table appropriately.
● The use of locks can also cause two additional problems: deadlock and starvation.
21.1.3 Dealing with Deadlock and Starvation
● Deadlock occurs when each transaction T in a set of two or more transactions is waiting for
some item that is locked by some other transaction T′ in the set.
● Hence, each transaction in the set is in a waiting queue, waiting for one of the other
transactions in the set to release the lock on an item.
● But because the other transaction is also waiting, it will never release the lock.
● A simple example is shown in Figure 21.5(a), where the two transactions T1′ and T2′ are
deadlocked in a partial schedule.
● T1′ is in the waiting queue for X, which is locked by T2′, whereas T2′ is in the waiting
queue for Y, which is locked by T1′.
Rule
· If Tj is not waiting for another item, then Ti waits.
· If Tj is already waiting, then Ti is aborted.
· A transaction never waits for another blocked transaction.
· Because of this, deadlock cannot occur.
Timeouts
Starvation
● Another problem that may occur when locking is used is [Link] occurs when
a transaction cannot proceed for an indefinite period of time while other transactions in the
system continue normally.
● This may occur if the waiting scheme for locked items is unfair and gives priority to some
transactions over others.
● One solution for starvation is to use a fair waiting scheme, such as a first-come-first-served
queue.
● In this scheme, transactions are allowed to lock an item in the order in which they originally
requested the lock.
● Another scheme allows some transactions to have priority over others but increases the
priority of a transaction the longer it waits, until it eventually gets the highest priority and
proceeds.
● Starvation can also occur because of victim selection if the algorithm repeatedly selects the
same transaction as the victim, causing it to abort and never finish execution.
● The algorithm can use higher priorities for transactions that have been aborted multiple
times to avoid this problem.
● The wait-die and wound-wait schemes avoid starvation because they restart an aborted
transaction with its original timestamp, so the possibility that the same transaction is aborted
repeatedly is small.
● The use of locking, combined with the two-phase locking protocol, guarantees
serializability of schedules. The serializable schedules produced by two-phase
locking have their equivalent serial schedules based on the order in which
executing transactions lock the items they acquire.
● If a transaction needs an item that is already locked, it may be forced to wait
until the item is released.
● Some transactions may be aborted and restarted because of the deadlock
problem.A different approach to concurrency control involves using
transaction timestamps to order transaction execution for an equivalent serial
schedule.
● 21.2.1 Timestamps
· read_item(X) operation
· write_item(X) operation
Whenever the basic TO algorithm detects two conflicting operations that occur in the
incorrect order, it rejects the later of the two operations by aborting the transaction that
issued it. The schedules produced by basic TO are hence guaranteed to be conflict
serializable. As mentioned earlier, deadlock does not occur with timestamp ordering.
● Strict Timestamp Ordering is a variation of the basic Timestamp Ordering (TO) method. It
ensures that schedules are strict and conflict [Link] helps in easy recoverability of
transactions.
● In this method, when a transaction T issues read_item(X) or write_item(X) and TS(T) >
write_TS(X), the operation cannot execute [Link] read or write operation of T is
[Link] operation waits until the transaction T′ that wrote X (where TS(T′) =
write_TS(X)) commits or aborts.
Chapter-23
Recovery Concepts
1. Meaning of Recovery
1. Recovery is the process of restoring the database to the most recent consistent state
before failure.
2. Failures may occur due to transaction errors, system crashes, or disk failures.
3. To perform recovery, the DBMS maintains information about all changes made by
transactions.
4. This information is stored in a system log (transaction log).
5. The log file records every update operation performed by transactions.
2. Recovery Strategies
1. Catastrophic failure means severe damage to the database (e.g., disk crash).
2. In this situation, the database stored on disk becomes physically damaged.
3. Recovery is done using a backup copy of the database stored in archival storage.
4. Archival storage may include:
○ Magnetic tapes
○ External storage systems
○ Large capacity offline storage
5. The recovery steps include:
○ Restore the previous backup copy of the database.
○ Use the log file to redo the operations of committed transactions.
6. This reconstruction continues up to the time of failure.
UNDO
REDO
1. Used when a transaction has committed, but its updates were not yet written to disk.
2. The recovery process reapplies those operations from the log.
3. This ensures all committed changes are reflected in the database.
4. For non-catastrophic failures, a full backup is not required.
5. Recovery is performed using online system logs.
1. Deferred Update
2. Immediate Update
Concept
Process
Failure Handling
1. If a transaction fails before commit, it has not changed the database on disk.
2. Therefore:
○ UNDO is not required.
3. However, if a committed transaction’s updates are not yet written to disk, they must be
REDONE from the log.
Algorithm Name
1. In immediate update, database items may be updated before the transaction commits.
2. Updates may be written directly to the database.
Important Rule
1. Before writing changes to the database, the update must be recorded in the log file.
2. The log record must be force-written to disk first.
Failure Handling
1. If a transaction fails before commit, some changes may already exist in the database.
2. These changes must be undone.
3. If a committed transaction’s updates are missing on disk, they must be redone.
Algorithm Name
1. Another variation requires that all updates must be written to the database before
commit.
2. Because updates are already stored on disk before commit:
○ REDO is not required.
3. Only UNDO is required if a transaction fails.
4. This technique is called the UNDO / NO-REDO algorithm.
Idempotent operation means performing an operation multiple times gives the same result as
performing it once.
Importance in Recovery
•Deferred update means postponing all database updates on disk until the transaction
successfully completes and reaches the commit point.
•During transaction execution, updates are stored only in the log file and cache buffers. After
the transaction commits and the log is force-written to disk, the updates are applied to the
database on disk.
•If a transaction fails before commit, the database remains unchanged; therefore UNDO is not
required.
•Only REDO-type log entries are recorded in the log. These entries contain the After Image
(AFIM) of the updated item. UNDO entries are unnecessary because no update occurs on disk
before commit.
•This method works well mainly for short transactions with few updates. For larger
transactions, updates must remain in cache buffers until commit, causing many buffers to be
pinned and possibly leading to buffer space shortage.
1.A transaction cannot update the database on disk before commit; modified buffers remain
pinned (No-Steal policy).
2.A transaction cannot commit until all its REDO log entries are written to the log and the log
buffer is force-written to disk (WAL rule).
•Since updates are written to disk only after commit, UNDO is never required. However,
REDO is required if a system crash occurs after commit but before the updates are written to
disk.
•In multiuser systems, concurrency control and recovery are related. When strict two-phase
locking is used, write locks are held until the transaction commits, ensuring strict and
serializable schedules.
oThe system keeps a commit list (transactions committed after the last checkpoint) and an
active list (transactions that started but did not commit).
oDuring recovery, REDO all WRITE operations of committed transactions from the log in their
original order.
•REDO operation: From a log entry [write_item, T, X, new_value], the value of item X in the
database is set to new_value (AFIM).
•Example: If checkpoint occurs at t₁ and system crashes at t₂, transaction T1 committed before
the checkpoint and needs no REDO. Transactions T2 and T3 committed after the checkpoint,
so their updates must be redone. Transactions T4 and T5 did not commit and are ignored or
canceled.
•If a data item is updated multiple times by committed transactions after the checkpoint, only
the latest update is redone during recovery by scanning the log from the end and maintaining a
list of items already recovered.
•If a transaction is aborted (for example, due to deadlock detection), it is simply resubmitted,
since it has not modified the database on disk.
•Advantages: No UNDO operations are needed and cascading rollback does not occur because
transactions never read values written by uncommitted transactions.
•Drawbacks: Reduced concurrency due to locks held until commit and possible high buffer
space requirements to store updates until commit.
22.3 Recovery Techniques Based on Immediate Update
● ·In Immediate Update, when a transaction performs an update, the change may be
written to the database on disk before the transaction commits. However, it is not
necessary that every update be written immediately.
● Because updates may reach the disk before commit, the system must be able to undo
the effects of failed transactions. This is done by rolling back the transaction using
UNDO log entries that store the Before Image (BFIM) of the data item.
● These techniques follow the steal strategy, which allows modified buffers in main
memory to be written to disk before the transaction commits.
● Immediate update methods are divided into two types:
UNDO/NO-REDO: All updates of a transaction are written to disk before commit, so
redo is not required. This method uses the steal/force strategy.
UNDO/REDO: A transaction may commit before all updates are written to disk, so
recovery requires undoing uncommitted transactions and redoing committed
transactions. This method uses the steal/no-force strategy and is the most commonly
used in practice.
● In multiuser systems, recovery depends on the concurrency control protocol. With strict
two-phase locking, transactions cannot read or write items modified by uncommitted
transactions, though deadlocks may occur, requiring transaction abort and undo.
● The recovery algorithm RIU_M (Recovery using Immediate Update – Multiuser) uses
checkpoints and maintains two lists: committed transactions and active transactions
since the last checkpoint.
● During recovery, the system first undoes all write operations of active (uncommitted)
transactions in reverse log order, and then redoes all write operations of committed
transactions in log order.
● UNDO operation: From the log entry [write_item, T, X, old_value, new_value], the
value of item X is restored to old_value (BFIM).
● For efficiency, redo is applied only to the latest update of each item, and undo
operations are applied at most once by maintaining lists of processed items.
**********************End of Module-5********************************