Unit IV FIS
Unit IV FIS
There are several techniques for implementing concurrency control, such as locking,
timestamp ordering, and optimistic concurrency control. Locking involves acquiring
locks on data items to prevent other transactions from accessing or modifying them
concurrently. Timestamp ordering involves assigning a unique timestamp to each
transaction and ordering transactions based on their timestamps to ensure
serializability. Optimistic concurrency control assumes that conflicts are rare and
allows transactions to execute concurrently without acquiring locks but checks for
conflicts at the end of each transaction and rolls back conflicting transactions if
necessary.
Effective concurrency control is critical for ensuring the correctness and performance
of database systems and other concurrent applications.
Introduction to Transactions
In database management, a transaction is a logical unit of work that consists of one
or more database operations. Transactions are used to ensure the consistency and
reliability of a database, by ensuring that all the operations that make up a
transaction are executed as a single unit.
The primary goal of a transaction is to maintain the integrity of data in the database.
This means that the database should remain in a consistent state both before and
after the transaction is executed. In other words, a transaction should be atomic,
consistent, isolated, and durable (ACID).
Atomicity refers to the property of a transaction that ensures all operations in the
transaction are treated as a single, indivisible unit of work. This means that if any part
of the transaction fails, the entire transaction is rolled back, and the database is left in
its original state.
Consistency refers to the property that ensures that the database remains in a valid
state both before and after the transaction. This means that a transaction should
leave the database in a consistent state and should not violate any constraints, such
as referential integrity, unique constraints, or domain constraints.
Isolation refers to the property that ensures that the operations of one transaction
are isolated from the operations of other concurrent transactions. This means that
the results of a transaction should not be affected by the concurrent execution of
other transactions.
Durability refers to the property that ensures that the effects of a transaction are
permanent and are not lost due to system failures, crashes, or power outages.
Transaction Systems
Transaction systems are software systems that provide support for executing
transactions in a database. A transaction system provides a programming interface
that enables applications to initiate transactions and commit or roll back changes to
the database.
Transaction systems are essential for managing data in a reliable and consistent
manner. They provide a mechanism for managing concurrent access to data,
ensuring data consistency, and ensuring that transactions are durable and
recoverable in the event of a system failure. In modern transaction systems, the ACID
properties of transactions are maintained through the use of various concurrency
control mechanisms, such as locking, multi-version concurrency control, and
optimistic concurrency control.
ACID Properties
ACID stands for Atomicity, Consistency, Isolation, and Durability. These are a set of
properties that are fundamental to ensuring that transactions are reliable, consistent,
and accurate in a database management system.
The ACID properties are essential for ensuring the reliability and consistency of
transactions in a database management system. ACID-compliant systems ensure that
transactions are executed in a predictable, reliable, and consistent manner, which is
critical for applications that require high levels of data accuracy and consistency.
However, achieving high levels of ACID compliance can sometimes come at the cost
of performance, as transaction processing may be slower due to the need for
concurrency control mechanisms and other transaction management features.
Media recovery, on the other hand, refers to the process of restoring individual data
files or database objects that have been damaged or lost due to hardware failures,
human errors, or other causes. Media recovery is typically performed using backup
and restore operations, which involve restoring data from backup copies onto a
separate storage device or medium.
Both system and media recovery are critical for ensuring the reliability and durability
of a database. Without these recovery mechanisms, a database management system
could lose important data due to failures or errors, leading to data inconsistencies
and loss of business-critical information. In addition, recovery mechanisms can help
minimize downtime and ensure that the database is back up and running as quickly
as possible in the event of a failure.
Overall, system and media recovery mechanisms are an essential part of any
database management system and should be carefully planned and implemented to
ensure that the system can recover quickly and effectively from failures and errors.
Locking Protocol
Locking protocol is a concurrency control mechanism used in database management
systems to ensure that transactions do not interfere with each other when accessing
and modifying data. Locking protocol ensures that only one transaction can access or
modify a particular data item at a time, preventing conflicts and ensuring data
consistency.
The locking protocol works by allowing transactions to acquire locks on data items
before accessing or modifying them. A lock is a mechanism that prevents other
transactions from accessing or modifying the same data item simultaneously. Locks
can be of two types: shared locks and exclusive locks.
A shared lock allows multiple transactions to read the same data item simultaneously.
Shared locks are used when a transaction needs to read a data item but does not
need to modify it. Multiple transactions can acquire shared locks on the same data
item at the same time, but no transaction can acquire an exclusive lock on the same
data item until all shared locks have been released.
An exclusive lock, on the other hand, allows only one transaction to access or modify
a data item at a time. Exclusive locks are used when a transaction needs to modify a
data item. An exclusive lock prevents other transactions from acquiring either a
shared or an exclusive lock on the same data item until the lock is released.
Locks are acquired and released by the transactions themselves. When a transaction
acquires a lock, it holds the lock until it is either released explicitly or the transaction
completes. If a transaction attempts to acquire a lock on a data item that is already
locked by another transaction, it must wait until the lock is released before
continuing.
Overall, SQL provides a range of concurrency control mechanisms that can be used
to manage concurrent access to data and ensure data consistency in database
management systems. The choice of mechanism depends on the specific
requirements of the application and the characteristics of the workload.
The log is a file that records all changes made to the database, including updates,
inserts, and deletes. The log also records transactions and their status, such as
whether they were committed or aborted. The log is usually stored on non-volatile
storage, such as a hard disk, to ensure that it is not lost in the event of a system
failure.
During normal operation, transactions modify the database and the log is updated to
reflect these changes. When a transaction commits, the log entry for that transaction
is written to disk, ensuring that the changes made by the transaction are durable. If a
failure occurs, the system can use the log to recover from the failure by restoring the
database to a consistent state.
There are two types of log-based recovery: forward recovery and backward recovery.
1. Forward recovery: Forward recovery is used when the failure occurs after a
transaction has committed. In this case, the system can simply replay the log
starting from the point where the failed transaction committed, applying each
change to the database in the order in which it was made.
2. Backward recovery: Backward recovery is used when the failure occurs before
a transaction has committed. In this case, the system must undo the changes
made by the failed transaction by rolling back the transaction and restoring
the database to its state before the transaction began. The system can do this
by replaying the log in reverse order, undoing each change made by the failed
transaction until the database is restored to its previous state.
The two-phase commit protocol consists of two phases: the prepare phase and the
commit phase.
1. Prepare Phase: In the prepare phase, the transaction coordinator (TC) sends a
prepare request to all the participants in the transaction. The participants then
respond to the prepare request, indicating whether they are able to commit
the transaction or not. If all participants are able to commit the transaction,
they respond with a "yes" vote. If any participant cannot commit the
transaction, they respond with a "no" vote.
2. Commit Phase: If all participants respond with a "yes" vote in the prepare
phase, the transaction coordinator sends a commit request to all participants.
The participants then commit the transaction and send a message to the
transaction coordinator indicating that the transaction has been committed. If
any participant responds with a "no" vote in the prepare phase or if any
participant fails to respond, the transaction coordinator sends an abort
request to all participants, causing them to abort the transaction.
The two-phase commit protocol ensures that all participants in a transaction commit
or abort the transaction together, thereby maintaining data consistency across all
systems. However, the two-phase commit protocol has some limitations, including
the possibility of blocking and the need for a centralized coordinator. To overcome
these limitations, other distributed transaction protocols, such as three-phase
commit (3PC) and the Paxos protocol, have been developed.
1. Identify the cause of the failure: Before beginning the recovery process, it is
important to identify the cause of the failure. This will help to determine the
appropriate recovery strategy.
2. Restore the database from backup: The first step in the recovery process is to
restore the database from a backup. This can usually be done using the SQL
Server Management Studio (SSMS) or a command-line tool such as SQLCMD.
It is important to ensure that the backup being restored is the most recent
version that is consistent with the transaction logs.
3. Apply transaction logs: Once the database has been restored from backup, the
next step is to apply transaction logs to bring the database up to date. This
can be done using the RESTORE LOG command in SQL.
4. Test the database: After the recovery process is complete, it is important to
test the database to ensure that it is functioning correctly. This can involve
running queries and tests to verify that the data is consistent and accurate.
SQL also provides several tools for managing transaction logs and ensuring data
consistency in the event of a failure. These tools include:
1. Full and differential backups: SQL supports both full and differential backups,
which can be used to ensure that the database can be restored to a consistent
state in the event of a failure.
2. Checksums and page verification: SQL supports the use of checksums and
page verification to ensure that data remains consistent and accurate. These
tools can be used to detect and repair errors that may occur during the
recovery process.
3. Recovery models: SQL provides different recovery models, including simple,
bulk-logged, and full recovery models, that can be used to manage
transaction logs and ensure data consistency.
In summary, SQL provides a powerful set of tools for recovering a database after a
failure. These tools include backup and restore functionality, transaction log
management, and tools for ensuring data consistency. By following best practices for
SQL recovery, businesses can ensure that critical data is protected and available in
the event of a failure.