Unit - 5
Unit - 5
• The part of the DBMS that keeps track of the locks issued to transactions is called
the lock manager.
• The lock manager maintains a lock table, with the data object identifier as the key.
• The DBMS also maintains a descriptive entry for each transaction in a transaction
table, and among other things, the entry contains a pointer to a list of locks held by
the transaction.
• A lock table entry for an object which can be a page or a record contains the
information like: the number of transactions currently holding a lock on the object
(this can be more than one if the object is locked in shared mode), the nature of the
lock (shared or exclusive), and a pointer to a queue of lock requests.
Implementing Lock and Unlock Requests
• According to the Strict 2PL protocol, before a transaction T reads or writes a
database object O, it must obtain a shared or exclusive lock on O and must hold on
to the lock until it commits or aborts.
• When a transaction needs a lock on an object, it issues a lock request to the lock
manager:
1. If a shared lock is requested, the queue of requests is empty, and the object is
not currently locked in exclusive mode, the lock manager grants the lock and
updates the lock table entry for the object (indicating that the object is locked in
shared mode, and incrementing the number of transactions holding a lock by one).
2. If an exclusive lock is requested, and no transaction currently holds a lock on
the object (which also implies the queue of requests is empty), the lock manager
grants the lock and updates the lock table entry.
• When a transaction aborts or commits, it releases all its locks. When a lock on an
object is released, the lock manager updates the lock table entry for the object and
examines the lock request at the head of the queue for this object.
• If this request can now be granted, the transaction that made the request is woken
up and given the lock.
• Indeed, if there are several requests for a shared lock on the object at the front of
the queue, all of these requests can now be granted together.
• Note that if T1 has a shared lock on O, and T2 requests an exclusive lock, T2's
request is queued.
• Now, if T3 requests a shared lock, its request enters the queue behind that of T2,
even though the requested lock is compatible with the lock held by T1.
• This rule ensures that T2 does not starve, that is, wait indefinitely while a stream of
other transactions acquire shared locks and thereby prevent T2 from getting the
exclusive lock that it is waiting for.
Atomicity of Locking and Unlocking
• To ensure atomicity of lock and unlock operations when several instances of the
lock manager code can execute concurrently, access to the lock table has to be
guarded by an operating system synchronization mechanism such as a semaphore.
• If suppose that a transaction requests an exclusive lock.
• The lock manager checks and finds that no other transaction holds a lock on the
object and therefore decides to grant the request.
• But in the meantime, another transaction might have requested and received a
conflicting lock!
• To prevent this, the entire sequence of actions in a lock request call (checking to
see if the request can be granted, updating the lock table, etc.) must be
implemented as an atomic operation.
Deadlocks
• Consider the following example
• Transaction T1 sets an exclusive lock on object A, T2 sets an exclusive lock on
B, T1 requests an exclusive lock on B and is queued, and T2 requests an
exclusive lock on A and is queued.
• Now, T1 is waiting for T2 to release its lock and T2 is waiting for T1 to release
its lock. Such a cycle of transactions waiting for locks to be released is called a
deadlock.
• Clearly, these two transactions will make no further progress. Worse, they hold
locks that may be required by other transactions.
• The DBMS must either prevent or detect (and resolve) such deadlock
situations.
Deadlock Prevention
• We can prevent deadlocks by giving each transaction a priority and ensuring that
lower priority transactions are not allowed to wait for higher priority transactions
(or vice versa).
• One way to assign priorities is to give each transaction a timestamp when it starts
up.
• The lower the timestamp, the higher the transaction's priority, that is, the oldest
transaction has the highest priority.
• If a transaction Ti requests a lock and transaction Tj holds a conflicting lock, the
lock manager can use one of the following two policies:
• Wait-die: If It has higher priority, it is allowed to wait; otherwise it is aborted.
• Wound-wait: If It has higher priority, abort Tj; otherwise Ti waits. In the wait-
die scheme, lower priority transactions can never wait for higher priority
transactions
• In the wound-wait scheme, higher priority transactions never wait for lower
priority transactions. In either case no deadlock cycle can develop.
• Transactions having lower timestamp value is having higher priority, this ensures
that the oldest transaction will get all the locks that it requires.
• The wait-die scheme is non preemptive; only a transaction requesting a lock can be
aborted.
Deadlock Detection
• Deadlocks tend to be rare and typically involve very few transactions.
• This observation suggests that rather than taking measures to prevent
deadlocks, it may be better to detect and resolve deadlocks as they arise.
• In the detection approach, the DBMS must periodically check for deadlocks.
• When a transaction Ti is suspended because a lock that it requests cannot be
granted, it must wait until all transactions Tj that currently hold conflicting
locks release them.
• The lock manager maintains a structure called a waits-for graph to detect
deadlock cycles.
• The nodes correspond to active transactions, and there is an arc from Ti to Tj if
(and only if) Ti is waiting for Tj to release a lock.
• The lock manager adds edges to this graph when it queues lock requests and
removes edges when it grants lock requests.
• The waits-for graph is periodically checked for cycles, which indicate deadlock.
• A deadlock is resolved by aborting a transaction that is on a cycle and
releasing its locks; this action allows some of the waiting transactions to
proceed.
CONCURRENCY CONTROLWITHOUT LOCKING
• Locking is the most widely used approach to concurrency control in a DBMS, but
it is not the only one.
• We now consider some alternative approaches.
Timestamp-Based Protocols
• The locking protocols that we have described thus far determine the order
between every pair of conflicting transactions at execution time by the first
lock that both members of the pair request that involves incompatible modes.
• Another method for determining the serializability order is to select an
ordering among transactions in advance.
• The most common method for doing so is to use a timestamp-ordering
scheme.
Timestamps
• With each transaction Ti in the system, we associate a unique fixed timestamp,
denoted by TS (4).
• This timestamp is assigned by the database system before the transaction Ti starts
execution.
• If a transaction Ti has been assigned timestamp TS(Ti), and a new transaction Q
enters the system, then TS(4) < TS(4).
• There are two simple methods for implementing this scheme:
• Use the value of the system clock as the timestamp; that is, a transaction's
timestamp is equal to the value of the clock when the transaction enters the system.
• Use a logical counter that is incremented after a new timestamp has been assigned;
that is, a transaction's timestamp is equal to the value of the counter when the
transaction enters the system.
● The timestamps of the transactions determine the serializability order.
● Thus, if TS (4) <TS(T), then the system must ensure that the produced schedule is
equivalent to a serial schedule in which transaction Ti appears before transaction Q.
● To implement this scheme, we associate with each data item Q two timestamp
values:
o W-timestamp (Q) denotes the largest timestamp of any transaction that executed
write (Q) successfully.
o R-timestamp(8) denotes the largest timestamp of any transaction that executed
read(Q) successfully.
● These timestamps are updated whenever a new read (Q) or write (Q) instruction is
executed.
● Advantages: This protocol ensures serializabilty and ensures freedom from
deadlock.
Disadvantages: Starvation may occur due to continuously getting aborted and
restarting the transaction.
Thomas Write Rule:
Outdated writes are ignored in the Thomas write rule .It works same as Time
stamp ordering when R-W,W-R conflicts occurred. It allows more no.of
schedules than time stamp ordering as it is flexible than time stamp ordering
protocol.
• ARIES stands for “Algorithm for Recovery and Isolation Exploiting Semantics.” It
was designed to support the needs of industrial strength transaction processing
systems. ARIES uses logs to record the progress of transactions and their actions
which cause changes to recoverable data objects. The log is the source of truth and
is used to ensure that committed actions are reflected in the database, and that
uncommitted actions are undone.
• Conceptually the log is a single ever-growing sequential file (append-only). Every
log record has a unique log sequence number (LSN), and LSNs are assigned in
ascending order.
The ARIES recovery procedure consists of three main steps:
• Analysis
The analysis step identifies the dirty (updated) pages in the buffer (Note 6), and the set of
transactions active at the time of the crash. The appropriate point in the log where the
REDO operation should start is also determined
• REDO
The REDO phase actually reapplies updates from the log to the database. Generally, the
REDO operation is applied to only committed transactions. However, in ARIES, this is not
the case. Certain information in the ARIES log will provide the start point for REDO, from
which REDO operations are applied until the end of the log is reached. In addition,
information stored by ARIES and in the data pages will allow ARIES to determine whether
the operation to be redone has actually been applied to the database and hence need not be
reapplied. Thus only the necessary REDO operations are applied during recovery.
UNDO
During the UNDO phase, the log is scanned backwards and the operations of
transactions that were active at the time of the crash are undone in reverse order. The
information needed for ARIES to accomplish its recovery procedure includes the log,
the Transaction Table, and the Dirty Page
Media Recovery
1)Analysis
2)Redo
3)Undo
Undoing – If a transaction crashes, then the recovery manager may undo transactions
i.e. reverse the operations of a transaction.
Deferred update – This technique does not physically update the database on disk
until a transaction has reached its commit point. Before reaching commit, all
transaction updates are recorded in the local transaction workspace. If a transaction
fails before reaching its commit point, it will not have changed the database in any
way so UNDO is not needed.
Immediate update – In the immediate update, the database may be updated by some
operations of a transaction before the transaction reaches its commit point.
● Full database backup – In this full database including data and database, Meta
information needed to restore the whole database, including full-text catalogs are
backed up in a predefined time series.
● Differential backup – It stores only the data changes that have occurred since last
full database backup. When same data has changed many times since last full
database backup, a differential backup stores the most recent version of changed
data. For this first, we need to restore a full database backup.
● Transaction log backup – In this, all events that have occurred in the database,
like a record of every single statement executed is backed up. It is the backup of
transaction log entries and contains all transaction that had happened to the
database. Through this, the database can be recovered to a specific point in time. It
is even possible to perform a backup from a transaction log if the data files are
destroyed and not even a single committed transaction is lost.