Unit 4 Material
Unit 4 Material
ACID Properties
1. Atomicity
2. Consistency
3. Isolation
4. Durability
1. Active: This is the initial state of every transaction. In this state, the
transaction is being executed. The transaction remains in this state as
long as it is executing SQL statements.
2. Partially Committed: When a transaction executes its final statement,
it is said to be in a 'partially committed' state. At this point, the
transaction has passed the modification phase but has not yet been
committed to the database. If a failure occurs at this stage, the
transaction will roll back.
3. Failed: If a transaction is in a 'partially committed' state and a problem
occurs that prevents the transaction from committing, it is said to be in
a 'failed' state. When a transaction is in a 'failed' state, it will trigger a
rollback operation.
4. Aborted: If a transaction is rolled back and the database is restored to
its state before the transaction began, the transaction is said to be
'aborted.' After a transaction is aborted, it can be restarted again, but
this depends on the policy of the transaction management component
of the DBMS.
5. Committed: When a transaction is in a 'partially committed' state and
the commit operation is successful, it is said to be 'committed.' At this
point, the transaction has completed its execution and all of its updates
are permanently stored in the database.
6. Terminated: After a transaction reaches the 'committed' or 'aborted'
state, it is said to be 'terminated.' This is the final state of a transaction.
Note: The actual terms and the number of states may vary depending on the
specifics of the DBMS and the transaction processing model it uses. However,
the fundamental principles remain the same.
| AccountID | Balance |
|-----------|---------|
| 1 | 1000 |
| 2 | 2000 |
| AccountID | Balance |
|-----------|---------|
| 1 | 800 |
| 2 | 2200 |
T1 | T2
----------|-----------
Read(A) |
A = A+50 |
| Read(A)
| A = A+100
Write(A) |
| Write(A)
T1 | T2
------------------|-----------
Read(A) |
A = A+50 |
Write(A) |
| Read(A)
| A = A+100
| Write(A)
Read(A)(rollbacks)|
| commit
Result: T2 has a "dirty" value, that was never committed in T1 and doesn't
actually exist in the database.
T1 | T2
----------|----------
Read(A) |
| Read(A)
| A = A+100
| Write(A)
Read(A) |
Result: Within the same transaction, T1 has read two different values for the
same data item. This inconsistency is the unrepeatable read.
To manage concurrent execution and ensure the consistency and reliability of
the database, DBMSs use concurrency control techniques. These typically
include locking mechanisms, timestamps, optimistic concurrency control, and
serializability checks.
Serializability in DBMS
Schedule is an order of multiple transactions executing in concurrent
environment.
Serial Schedule: The schedule in which the transactions execute one after
the other is called serial schedule. It is consistent in nature.
For example: Consider following two transactions T1 and T2.
T1 | T2
----------|----------
Read(A) |
Write(A) |
Read(B) |
Write(B) |
| Read(A)
| Write(A)
| Read(B)
| Write(B)
All the operations of transaction T1 on data items A and then B executes and
then in transaction T2 all the operations on data items A and B execute.
Non Serial Schedule: The schedule in which operations present within the
transaction are intermixed. This may lead to conflicts in the result or
inconsistency in the resultant data.
For example- Consider following two transactions,
T1 | T2
----------|----------
Read(A) |
Write(A) |
| Read(A)
| Write(B)
Read(A) |
Write(B) |
| Read(B)
| Write(B)
Types of Serializability
1. Conflict Serializability
2. View Serializability
Conflict Serializability
Conflict serializability is a form of serializability where the order of non-
conflicting operations is not significant. It determines if the concurrent
execution of several transactions is equivalent to some serial execution of
those transactions.
Two operations are said to be in conflict if:
T1 | T2
----------|----------
Read(A) | Read(A)
Read(A) | Read(B)
Write(B) | Read(A)
Read(B) | Write(A)
Write(A) | Write(B)
T1 | T2
----------|----------
Read(A) | Write(A)
Write(A) | Read(A)
Write(A) | Write(A)
A schedule is conflict serializable if it can be transformed into a serial schedule
(i.e., a schedule with no overlapping transactions) by swapping non-conflicting
operations. If it is not possible to transform a given schedule to any serial
schedule using swaps of non-conflicting operations, then the schedule is not
conflict serializable.
T1 | T2
----------|----------
Read(A) |
| Read(A)
Write(A) |
| Read(B)
| Write(B)
R1(A) conflicts with W1(A), so there's an edge from T1 to T1, but this is
ignored because they´re from the same transaction.
R2(A) conflicts with W1(A), so there's an edge from T2 to T1.
No other conflicting pairs.
The graph has nodes T1 and T2 with an edge from T2 to T1. There are no
cycles in this graph.
Decision: Since the precedence graph doesn't have any cycles,Cycle is a path
using which we can start from one node and reach to the same node. the
schedule S is conflict serializable. The equivalent serial schedules, based on
the graph, would be T2 followed by T1.
View Serializability
View Serializability is one of the types of serializability in DBMS that ensures
the consistency of a database schedule. Unlike conflict serializability, which
cares about the order of conflicting operations, view serializability only cares
about the final outcome. That is, two schedules are view equivalent if they
have:
Initial Read: The same set of initial reads (i.e., a read by a transaction
with no preceding write by another transaction on the same data item).
Updated Read: For any other writes on a data item in between, if a
transaction Tj reads the result of a write by transaction Tiin one
schedule, then Tj should read the result of a write by Ti in the other
schedule as well.
Final Write: The same set of final writes (i.e., a write by a transaction
with no subsequent writes by another transaction on the same data
item).
Let's understand view serializability with an example:
Consider two transactions T1 and T2:
Schedule 1(S1):
| Transaction T1 | Transaction T2 |
|---------------------|---------------------|
| Write(A) | |
| | Read(A) |
| | Write(B) |
| Read(B) | |
| Write(B) | |
| Commit | Commit |
Schedule 2(S2):
| Transaction T1 | Transaction T2 |
|---------------------|---------------------|
| | Read(A) |
| Write(A) | |
| | Write(A) |
| Read(B) | |
| Write(B) | |
| Commit | Commit |
Here,
| | NL | IS | IX | S | SIX | X |
|:-----:|:-----:|:------:|:-----:|:------:|:-----:|:------:|
| NL | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
| IS | ✓ | ✓ | ✓ | ✓ | ✓ | ✗ |
| IX | ✓ | ✓ | ✓ | ✗ | ✗ | ✗ |
| S | ✓ | ✓ | ✗ | ✓ | ✗ | ✗ |
| SIX | ✓ | ✓ | ✗ | ✗ | ✗ | ✗ |
| X | ✓ | ✗ | ✗ | ✗ | ✗ | ✗ |
a. A Transaction must first lock the Root Node and it can be locked in any
mode.
b. Locks are granted as per the Compatibility Matrix indicated above.
c. A Transaction can lock a node in S or IS mode if it has already locked all
the predecessor nodes in IS or IX mode.
d. A Transaction can lock a node in X or IX or SIX mode if it has already
locked all the predecessor nodes in SIX or IX mode.
e. A transaction must follow two-phase locking. It can lock a node, only if
it has not previously unlocked a node. Thus, schedules will always be
conflict-serializable.
f. Before it unlocks a node, a Transaction has to first unlock all the children
nodes of that node. Thus, locking will proceed in top-down manner and
unlocking will proceed in bottom-up manner. This will ensure the
resulting schedules to be deadlock-free.
Benefits of using multiple granularity
Flexibility: Offers flexibility to transactions in deciding the appropriate
level of locking, which can lead to improved concurrency.
Performance: Reduces contention by allowing transactions to lock only
those parts of the data that they need.
It should check the states of all the transactions, which were being
executed.
A transaction may be in the middle of some operation; the DBMS must
ensure the atomicity of the transaction in this case.
It should check whether the transaction can be completed now or it
needs to be rolled back.
No transactions would be allowed to leave the DBMS in an inconsistent
state.
There are two types of techniques, which can help a DBMS in
recovering as well as maintaining the atomicity of a transaction −
Maintaining the logs of each transaction, and writing them onto some
stable storage before actually modifying the database.
Maintaining shadow paging, where the changes are done on a volatile
memory, and later, the actual database is updated
Log-Based Recovery
Log-based recovery is a widely used approach in database management
systems to recover from system failures and maintain atomicity and durability
of transactions. The fundamental idea behind log-based recovery is to keep a
log of all changes made to the database, so that after a failure, the system can
use the log to restore the database to a consistent state.
1. Transaction Logging:
For every transaction that modifies the database, an entry is made in the log.
This entry typically includes:
All the changes in main memory (buffer) up to that point are written to
disk.
A special entry is made in the log indicating a checkpoint. This helps in
reducing the amount of log that needs to be scanned during recovery.
4. Recovery Process
5. Commit/Rollback
Once a transaction is fully complete, a commit record is written to the log. If
a transaction is aborted, a rollback record is written, and using the log, the
system undoes any changes made by this transaction.
Initialization
When the transaction begins, the database system creates a copy of the
current page table. This copy is called the shadow page table.
The actual data pages on disk are not duplicated; only the page table entries
are. This means both the current and shadow page tables point to the same
data pages initially.
On Transaction Commit
Once the transaction reaches a commit point, the shadow page table is
discarded, and the current page table becomes the new "truth" for the
database state.
The old data pages that were modified during the transaction (and which the
shadow page table pointed to) can be reclaimed.
The recovery system consists of recovery data which is the update history of
transactions.
Recovery utility is software that must run for recovering when database is in
bad state, operation
or database disallowed.
Basic Concepts in Recovering the System:
Recovery: It is a process to restore database to a consistent state after it has
met with
a failure.
Failure: It is a database inconsistency that is visible.
Transaction Recovery: It is a process to restore that last consistent state of
data
items modified by failed transactions.
Transaction Log: It maintains execution history of concurrent transactions in
the
form of following record;
(transaction_id, operation, date item, before image, after image)
BFIM and AFIM: The value of a database object before its update is called as
before
image (BFIM) and the value of that object after its update is called as after
image
(AFIM).
Transaction directories: During the execution of a transaction two directories
are
maintained:
o Current directory: The entries in this directory points to the most recent
database pages on disk.
o Shadow directory: It points to the old entries. It gets its entry by the current
directory.
Recovery log entries: There are two types of recovery log entries
o Undo type log entry: It includes the BFIM of a data being updated. It is
required for undo operation.
o Redo type log entry: It includes the AFIM of a data item being updated. It is
needed for the redo operation.
Recovery approaches: Steal/no – steal approach
o Steal: In this case updated pages are allowed to be written to disk before the
transaction commits. It is a form of immediate update.
o No steal: Updated pages cannot be written to the disk before the transaction
commits. It is a kind of deferred update.
Recovery approaches: Force/no force approach
o Force: In this case transaction writes immediately all updated pages to the
disk
when the transaction commits.
o No force: Pages updated by the transaction are not written immediately to the
disk.
Recovery management: Recovery management has two components
o Recovery manager: It keeps track of transactions, handles commit and abort
operations. It also takes care of system checkpoint and restart.
o Log manager: It provides log services to the recovery manager and other
components that may need its service.
Causes of Transaction Failure: The causes are as follows.
Logical Errors: These are defined as fatal errors in transaction
DBMS Error: It is due to deadlock detection and rollback system enters in
bad state.
System Crash: Power out, OS failure, H/W malfunction.
I/O: It is like a disk failure.
4.2 ARIES ALGORITHM
Algorithms for Recovery and Isolation Exploiting Semantics, or ARIES is a
recovery algorithm
designed to work with a no-force, steal database approach; it is used by IBM
DB2, Microsoft
SQL Server and many other database systems.
Three main principles lie behind ARIES
Write ahead logging: Any change to an object is first recorded in the log, and
the log
must be written to stable storage before changes to the object are written to disk.
Repeating history during Redo: On restart after a crash, ARIES retraces the
actions of a
database before the crash and brings the system back to the exact state that it
was in
before the crash. Then it undoes the transactions still active at crash time.
Logging changes during Undo: Changes made to the database while undoing
transactions are logged to ensure such an action isn't repeated in the event of
repeated
restarts.
ARIES perform three steps after crash
Analysis: Finds all pages that have not been written to disk (dirty pages) and
all
active transactions at the time of crash.
Redo: Repeats all the statements in the log (at an appropriate point) and
restore the
database to a state similar to before crash has occurred.
Undo: Undoes the operations of transactions those did not commit.
Information sources of ARIES recovery
Log record: Each log record has a log sequence number (LSN) which is
monotonically increasing. It indicates the address of the log record on the disk.
There
are different logging actions like write, commit, abort, undo and ending a
transaction
which are recorded in log record.
Transaction table: It contains an entry for each active transaction. In recovery
process it is rebuild.
Dirty page table: It contains an entry for each dirty page in the buffer. It also
includes the page ID and the LSN corresponding to the earliest update to that
page.
ARIES Compensation Log Record (CLR)
This record is written just before the change recorded in update log is undone.
It describes the action taken to undo the actions recorded in the
corresponding update
record.
It contains field undoNextLSN, the LSN of the next log record that is to be
undone
for the transaction that wrote the update record.
It describes an action that will never be undone.
CLR contains information needed to reapply or redo, but not to reverse it.
4.3 LOG BASED RECOVERY
Log file is a sequential file that contains a record of actions taken by an entity. A
log is kept on
stable storage. There are two log records used by log based recovery technique:
Undo log records: It contains log entries of all write operations before update.
Redo log records: It contains log entries of all write operations after update.
The algorithm for log based recovery is as follows:
When transaction Ti starts, it registers itself by writing a <Ti start> log record
Before Ti executes write (X), a log record < Ti, X, V1, V2> is written, where
V1 is the
value of X before the write, and V2 is the value to be written to X.
When Ti finishes its last statement, the log record <Ti commit> is written
We assume for now that log records are written directly to stable storage (
that is, they are
not buffered)
The BFIM is not overwritten by AFIM until all undo log records for the
updating
information is force written to the disk.
The commit operation of a transaction cannot be completed until all the redo
and undo
log are force written to the disk.
Two approaches using logs
o Deferred database modification: This scheme records all modifications to the
log,
but defers all the writes to after partial commit.
o Immediate database modifications: This scheme allows database updates of an
uncommitted transaction to be made as the writes are issued.
4.4 TRANSACTION AND DIRTY PAGE TABLE
Dirty page table is used to represent information about dirty buffer pages during
normal
processing. It is also used during restart recovery. It is implemented using
hashing or via the
deferred- writes queue mechanism. Each entry in the table consists of two
fields:
PageID and
RecLSN
During normal processing , when a non-dirty page is being fixed in the buffers
with the intention
to modify , the buffer manager records in the buffer pool (BP) dirty-pages table ,
as RecLSN ,
the current end-of-log LSN , which will be the LSN of the next log record to be
written. The
value of RecLSN indicates from what point in the log there may be updates.
Whenever pages are
written back to nonvolatile storage, the corresponding entries in the BP dirty-
page table are
removed. The contents of this table are included in the checkpoint record that is
written during
normal processing. The restart dirty-pages table is initialized from the latest
checkpoint's record
and is modified during the analysis of the other records during the analysis pass.
The minimum
RecLSN value in the table gives the starting point for the redo pass during
restart recovery.
4.5 WRITE AHEAD LOG PROTOCOL
Write-ahead logging (WAL) is a family of techniques for providing atomicity
and durability
(two of the ACID properties) in database systems.
The Write-Ahead Logging Protocol:
Must force the log record for an update before the corresponding data page
gets to disk.
Must write all log records for a exact before commit.
Guarantees Atomicity.
Guarantees Durability.
WAL allows updates of a database to be done in-place. Another way to
implement atomic
updates is with shadow paging, which is not in-place. The main advantage of
doing updates inplace is that it reduces the need to modify indexes and block
lists.
ARIES is a popular algorithm in the WAL family.
4.6 CHECKPOINTS
Checkpoint mechanism copies the state of a process into nonvolatile storage.
Restore mechanism
copies the last known checkpointed state of the process back into memory and
continues
processing. This mechanism is especially useful for application which may run
for long periods
of time before reaching a solution.
Checkpoint-Recovery gives an application or system the ability to save its state,
and tolerate
failures by enabling a failed executive to recover to an earlier safe state.
Key ideas
Saves executive state
Provides recovery mechanism in the presence of a fault
Can allow tolerance of any non-apocalyptic failure
Provides mechanism for process migration in distributed systems for fault
tolerance reasons or load balancing
During the execution of the transaction, periodically perform checkpointing.
This includes
Output the log buffers to the log.
Force – write the database buffers to the disk.
Output an entry < checkpoint > on the log.
During the recovery process, the following two steps are performed
Undo all the transactions that have not committed.
Redo all transactions that have committed after the checkpoint.
Demerits of technique:
Insufficient in context of large databases
It requires transactions to execute serially
4.7 RECOVERY FROM A SYSTEM CRASH
Sometimes when there is power failure or some hardware or software failure
occurs, it causes the
system to crash. The following actions are taken when recovering from system
crash
Scan log forward from last <checkpoint> record
Repeat history by physically redoing all updates of all transactions.
Create an undo-list during scan as follows
o Undo-list is set to L initially
o Whenever <Ti start> is found Ti is added to undo-list
o Whenever <Ti commit> or <Ti abort> is found, Ti is deleted from undo-list
This brings database to state as of crash, with committed as well as
uncommitted transactions
having been redone. Now undo-list contains transactions that are incomplete,
that is, have neither
committed nor been fully rolled back.
Scan log backwards, performing undo on log records of transactions found in
undo-list.
Transactions are rolled back as described earlier.
When <Ti start> is found for a transaction Ti in undo-list, write a <Ti abort>
log record.
Stop scan when <Ti start> records have been found for all Ti in undo-list
This undoes the effects of incomplete transactions (those with neither commit
nor abort log
records). Recovery is now complete.
4.8 REDO AND UNDO PHASES
The recovery algorithms like ARIES have two phases: Undo and Redo phases
(i) Undo (Ti): Restore the value of all data items updated by Ti to old values.
The log is scanned
backwards and the operations of transactions that were active at the time of the
crash are undone
in reverse order.
(ii) Redo (Ti): Sets the value of data items updated by transaction Ti to new
values. It actually
reapplies updates from the log to the database. Generally the Redo operation is
applied to only
committed transactions. If a transaction was aborted before the crash and its
updates were
undone, as indicated by CLRs, the actions described in CLRs are also reapplied.