DBMS Unit-4
DBMS Unit-4
Page 1 of 15
Unit IV
To improve the throughput of the system, another kind of schedule are used which has
some more strict rules which help the database to remain consistent even when transactions
execute simultaneously.
ii) Non-Serial Schedule
To reduce the waiting time of transactions in the waiting queue and improve the system
efficiency, we use nonserial schedules which allow multiple transactions to start before a
transaction is completely executed. This may sometimes result in inconsistency and errors
in database operation.
So, these errors are handled with specific algorithms to maintain the consistency of the
database and improve CPU throughput as well.
Non-serial schedules are also sometimes referred to as parallel schedules, as transactions
execute in parallel in these kinds of schedules.
Serializable
Serializability in DBMS is the property of a nonserial schedule that determines whether it
would maintain the database consistency or not.
The nonserial schedule which ensures that the database would be consistent after the
transactions are executed in the order determined by that schedule is said to be Serializable
Schedules.
The serial schedules always maintain database consistency as a transaction starts only
when the execution of the other transaction has been completed under it.
Thus, serial schedules are always serializable.
A transaction is a series of operations, so various states occur in its completion journey.
They are discussed as follows:
i) Active
It is the first stage of any transaction when it has begun to execute. The execution of the
transaction takes place in this state.
Operations such as insertion, deletion, or updation are performed during this state.
During this state, the data records are under manipulation and they are not saved to the
database, rather they remain somewhere in a buffer in the main memory.
ii) Partially Committed
This state of transaction is achieved when it has completed most of the operations and is
executing its final operation.
It can be a signal to the commit operation, as after the final operation of the transaction
completes its execution, the data has to be saved to the database through the commit
operation.
If some kind of error occurs during this state, the transaction goes into a failed state, else it
goes into the Committed state.
iii) Commited
This state of transaction is achieved when all the transaction-related operations have been executed
successfully along with the Commit operation, i.e. data is saved into the database after the required
manipulations in this state. This marks the successful completion of a transaction.
iv) Failed
If any of the transaction-related operations cause an error during the active or partially
committed state, further execution of the transaction is stopped and it is brought into a
failed state. Here, the database recovery system makes sure that the database is in a
consistent state.
v) Aborted
If the error is not resolved in the failed state, then the transaction is aborted and a rollback
operation is performed to bring database to the the last saved consistent state. When the transaction
is aborted, the database recovery module either restarts the transaction or kills it.
Page 2 of 15
Unit IV
The illustration below shows the various states that a transaction may encounter in its completion
journey.
In DBMS, a transaction passes through various states such as active, partially committed, failed,
and aborted.
Active State – When the instructions of the transaction are running then the transaction is in active
state. If all the ‘read and write’ operations are performed without any error then it goes to the
“partially committed state”; if any instruction fails, it goes to the “failed state”.
2. Partially Committed – After completion of all the read and write operation the changes are
made in main memory or local buffer. If the changes are made permanent on the DataBase then
the state will change to “committed state” and in case of failure it will go to the “failed state”.
3. Failed State – When any instruction of the transaction fails, it goes to the “failed state” or if
failure occurs in making a permanent change of data on Database.
4. Aborted State – After having any type of failure the transaction goes from “failed state” to
“aborted state” and since in previous states, the changes are only made to local buffer or main
memory and hence these changes are deleted or rolled-back.
5.Committed State – It is the state when the changes are made permanent on the Data Base and
the transaction is complete and therefore terminated in the “terminated state”.
6.Terminated State – If there isn’t any roll-back or the transaction comes from the “committed
state”, then the system is consistent and ready for new transaction and the old transaction is
terminated.
Page 3 of 15
Unit IV
Schedule
A series of operation from one transaction to another transaction is known as schedule. It is used to
preserve the order of the operation in each of the individual transaction.
1. Serial Schedule
The serial schedule is a type of schedule where one transaction is executed completely before
starting another transaction. In the serial schedule, when the first transaction completes its cycle,
then the next transaction is executed.
Page 4 of 15
Unit IV
For example: Suppose there are two transactions T1 and T2 which have some operations. If it has
no interleaving of operations, then there are the following two possible outcomes:
1. Execute all the operations of T1 which was followed by all the operations of T2.
2. Execute all the operations of T1 which was followed by all the operations of T2.
In the given (a) figure, Schedule A shows the serial schedule where T1 followed by T2.
In the given (b) figure, Schedule B shows the serial schedule where T2 followed by T1.
2. Non-serial Schedule
If interleaving of operations is allowed, then there will be non-serial schedule.
It contains many possible orders in which the system can execute the individual operations
of the transactions.
In the given figure (c) and (d), Schedule C and Schedule D are the non-serial schedules. It
has interleaving of operations.
3. Serializable schedule
The serializability of schedules is used to find non-serial schedules that allow the
transaction to execute concurrently without interfering with one another.
It identifies which schedules are correct when executions of the transaction have
interleaving of their operations.
A non-serial schedule will be serializable if its result is equal to the result of its transactions
executed serially.
Page 5 of 15
Unit IV
Here,
Schedule A and Schedule B are serial schedule.
Schedule C and Schedule D are Non-serial schedule.
Serializability in DBMS
If a non-serial schedule can be transformed into its corresponding serial schedule, it is said to be
serializable. Simply said, a non-serial schedule is referred to as a serializable schedule if it yields
the same results as a serial timetable.
Types of Serializability
There are two ways to check whether any non-serial schedule is serializable.
1. Conflict serializability
Conflict serializability refers to a subset of serializability that focuses on maintaining the
consistency of a database while ensuring that identical data items are executed in an order. In a
Page 6 of 15
Unit IV
DBMS each transaction has a value and all the transactions, in the database rely on this uniqueness.
This uniqueness ensures that no two operations with the conflict value can occur simultaneously.
For example lets consider an order table and a customer table as two instances. Each order is
associated with one customer even though a single client may place orders. However there are
restrictions for achieving conflict serializability in the database. Here are a few of them.
1. Different transactions should be used for the two procedures.
2. The identical data item should be present in both transactions.
3. Between the two operations, there should be at least one write operation.
Example
Three transactions—t1, t2, and t3—are active on a schedule “S” at once. Let’s create a graph of
precedence.
Transaction – 1 (t1) Transaction – 2 (t2) Transaction – 3 (t3)
R(a)
R(b)
R(b)
W(b)
W(a)
W(a)
R(a)
W(a)
It is a conflict serializable schedule as well as a serial schedule because the graph (a DAG) has no
loops. We can also determine the order of transactions because it is a serial schedule.
Page 7 of 15
Unit IV
1. The first prerequisite is that the same kind of transaction appears on every schedule. This
requirement means that the same kind of group of transactions cannot appear on both
schedules S1 and S2. The schedules are not equal to one another if one schedule commits a
transaction but it does not match the transaction of the other schedule.
2. The second requirement is that different read or write operations should not be used in
either schedule. On the other hand, we say that two schedules are not similar if schedule S1
has two write operations whereas schedule S2 only has one. The number of the write
operation must be the same in both schedules, however there is no issue if the number of
the read operation is different.
3. The second to last requirement is that there should not be a conflict between either
timetable. execution order for a single data item. Assume, for instance, that schedule S1’s
transaction is T1, and schedule S2’s transaction is T2. The data item A is written by both
the transaction T1 and the transaction T2. The schedules are not equal in this instance.
However, we referred to the schedule as equivalent to one another if it had the same
number of all write operations in the data item.
Serializability testing
We can utilize the Serialization Graph or Precedence Graph to examine a schedule’s serializability.
A schedule’s full transactions are organized into a Directed Graph, what a serialization graph is.
Recoverability in DBMS
Recoverability is a property of database systems that ensures that, in the event of a failure or error,
the system can recover the database to a consistent state. Recoverability guarantees that all
committed transactions are durable and that their effects are permanently stored in the database,
while the effects of uncommitted transactions are undone to maintain data consistency.
The recoverability property is enforced through the use of transaction logs, which record all
changes made to the database during transaction processing. When a failure occurs, the system
uses the log to recover the database to a consistent state, which involves either undoing the effects
of uncommitted transactions or redoing the effects of committed transactions.
Page 8 of 15
Unit IV
Page 9 of 15
Unit IV
Page 10 of 15
Unit IV
using Log records part. Note that user need to only examine the part of the log starting with the
last checkpoint log record to find the set of transactions T, and to find out whether a commit or
abort record occurs in the log for each transaction in T. For example, consider the set of
transactions {T0, T1, . . ., T100}. Suppose that the most recent checkpoint took place during the
execution of transaction T67 and T69, while T68 and all transactions with subscripts lower than 67
completed before the checkpoint. Thus, only transactions T67, T69, . . ., T100 need to be
considered during the recovery scheme. Each of them needs to be redone if it has completed (that
is, either committed or aborted); otherwise, it was incomplete, and needs to be undone.
Log-based recovery is a technique used in database management systems (DBMS) to recover a
database to a consistent state in the event of a failure or crash. It involves the use of transaction
logs, which are records of all the transactions performed on the database.
In log-based recovery, the DBMS uses the transaction log to reconstruct the database to a
consistent state. The transaction log contains records of all the changes made to the database,
including updates, inserts, and deletes. It also records information about each transaction, such as
its start and end times.
When a failure occurs, the DBMS uses the transaction log to determine which transactions were
incomplete at the time of the failure. It then performs a series of operations to undo the incomplete
transactions and redo the completed ones. This process is called the redo/undo recovery algorithm.
The redo operation involves reapplying the changes made by completed transactions that were not
yet saved to the database at the time of the failure. This ensures that all changes are applied to the
database.
The undo operation involves undoing the changes made by incomplete transactions that were
saved to the database at the time of the failure. This restores the database to a consistent state by
reversing the effects of the incomplete transactions.
Once the redo and undo operations are completed, the DBMS can bring the database back online
and resume normal operations.
Log-based recovery is an essential feature of modern DBMSs and provides a reliable mechanism
for recovering from failures and ensuring the consistency of the database.
Advantages of Log based Recovery
Durability: In the event of a breakdown, the log file offers a dependable and long-lasting
method of recovering data. It guarantees that in the event of a system crash, no committed
transaction is lost.
Faster Recovery: Since log-based recovery recovers databases by replaying committed
transactions from the log file, it is typically faster than alternative recovery methods.
Incremental Backup: Backups can be made in increments using log-based recovery. Just
the changes made since the last backup are kept in the log file, rather than creating a
complete backup of the database each time.
Lowers the Risk of Data Corruption: By making sure that all transactions are correctly
committed or canceled before they are written to the database , log-based recovery lowers
the risk of data corruption.
Disadvantages of Log based Recovery
Additional overhead: Maintaining the log file incurs an additional overhead on the
database system, which can reduce the performance of the system.
Complexity: Log-based recovery is a complex process that requires careful management
and administration. If not managed properly, it can lead to data inconsistencies or loss.
Storage space: The log file can consume a significant amount of storage space, especially
in a database with a large number of transactions.
Time-Consuming: The process of replaying the transactions from the log file can be time-
consuming, especially if there are a large number of transactions to recover.
Page 11 of 15
Unit IV
Deadlock in DBMS
In database management systems (DBMS) a deadlock occurs when two or more transactions are
unable to the proceed because each transaction is waiting for the other to the release locks on
resources. This situation creates a cycle of the dependencies where no transaction can continue
leading to the standstill in the system. The Deadlocks can severely impact the performance and
reliability of a DBMS making it crucial to the understand and manage them effectively.
What is Deadlock?
The Deadlock is a condition in a multi-user database environment where transactions are unable to
the complete because they are each waiting for the resources held by other transactions. This
results in a cycle of the dependencies where no transaction can proceed.
Basically, Deadlocks occur when two or more transactions wait indefinitely for resources
held by each other. Also, mastering how to detect and resolve deadlocks is vital for database
efficiency.
Characteristics of Deadlock
Mutual Exclusion: Only one transaction can hold a particular resource at a time.
Hold and Wait: The Transactions holding resources may request additional resources held
by others.
No Preemption: The Resources cannot be forcibly taken from the transaction holding them.
Circular Wait: A cycle of transactions exists where each transaction is waiting for the
resource held by the next transaction in the cycle.
In a database management system (DBMS), a deadlock occurs when two or more transactions are
waiting for each other to release resources, such as locks on database objects, that they need to
complete their operations. As a result, none of the transactions can proceed, leading to a situation
where they are stuck or “deadlocked.”
Deadlocks can happen in multi-user environments when two or more transactions are running
concurrently and try to access the same data in a different order. When this happens, one
transaction may hold a lock on a resource that another transaction needs, while the second
transaction may hold a lock on a resource that the first transaction needs. Both transactions are
then blocked, waiting for the other to release the resource they need.
DBMSs often use various techniques to detect and resolve deadlocks automatically. These
techniques include timeout mechanisms, where a transaction is forced to release its locks after a
certain period of time, and deadlock detection algorithms, which periodically scan the transaction
log for deadlock cycles and then choose a transaction to abort to resolve the deadlock.
It is also possible to prevent deadlocks by careful design of transactions, such as always acquiring
locks in the same order or releasing locks as soon as possible. Proper design of the database
schema and application can also help to minimize the likelihood of deadlocks.
In a database, a deadlock is an unwanted situation in which two or more transactions are waiting
indefinitely for one another to give up locks. Deadlock is said to be one of the most feared
complications in DBMS as it brings the whole system to a Halt.
Example – let us understand the concept of deadlock suppose, Transaction T1 holds a lock on
some rows in the Students table and needs to update some rows in the Grades table.
Simultaneously, Transaction T2 holds locks on those very rows (Which T1 needs to update) in the
Grades table but needs to update the rows in the Student table held by Transaction T1.
Now, the main problem arises. Transaction T1 will wait for transaction T2 to give up the lock, and
similarly, transaction T2 will wait for transaction T1 to give up the lock. As a consequence, All
activity comes to a halt and remains at a standstill forever unless the DBMS detects the deadlock
and aborts one of the transactions.
Page 12 of 15
Unit IV
Page 13 of 15
Unit IV
Checks if TS (T1) < TS (T2) – if T1 is the older transaction and T2 has held some resource, then it
allows T1 to wait until resource is available for execution. That means if a younger transaction has
locked some resource and an older transaction is waiting for it, then an older transaction is allowed
to wait for it till it is available. If T1 is an older transaction and has held some resource with it and
if T2 is waiting for it, then T2 is killed and restarted later with random delay but with the same
timestamp. i.e. if the older transaction has held some resource and the younger transaction waits
for the resource, then the younger transaction is killed and restarted with a very minute delay with
the same timestamp.
This scheme allows the older transaction to wait but kills the younger one.
Wound Wait Scheme: In this scheme, if an older transaction requests for a resource held by a
younger transaction, then an older transaction forces a younger transaction to kill the
transaction and release the resource. The younger transaction is restarted with a minute delay
but with the same timestamp. If the younger transaction is requesting a resource that is held by
an older one, then the younger transaction is asked to wait till the older one releases it.
The following table lists the differences between Wait – Die and Wound -Wait scheme prevention
schemes:
Wait – Die Wound -Wait
It is based on a non-preemptive technique. It is based on a preemptive technique.
In this, older transactions must wait for the younger In this, older transactions never wait for
one to release its data items. younger transactions.
The number of aborts and rollbacks is higher in these In this, the number of aborts and rollback is
techniques. lesser.
Applications
1. Delayed Transactions: Deadlocks can cause transactions to be delayed, as the resources
they need are being held by other transactions. This can lead to slower response times and
longer wait times for users.
2. Lost Transactions: In some cases, deadlocks can cause transactions to be lost or aborted,
which can result in data inconsistencies or other issues.
3. Reduced Concurrency: Deadlocks can reduce the level of concurrency in the system, as
transactions are blocked waiting for resources to become available. This can lead to slower
transaction processing and reduced overall throughput.
4. Increased Resource Usage: Deadlocks can result in increased resource usage, as
transactions that are blocked waiting for resources to become available continue to
consume system resources. This can lead to performance degradation and increased
resource contention.
5. Reduced User Satisfaction: Deadlocks can lead to a perception of poor system
performance and can reduce user satisfaction with the application. This can have a negative
impact on user adoption and retention.
Features of Deadlock in a DBMS
1. Mutual Exclusion: Each resource can be held by only one transaction at a time, and other
transactions must wait for it to be released.
2. Hold and Wait: Transactions can request resources while holding on to resources already
allocated to them.
3. No Preemption: Resources cannot be taken away from a transaction forcibly, and the
transaction must release them voluntarily.
4. Circular Wait: Transactions are waiting for resources in a circular chain, where each
transaction is waiting for a resource held by the next transaction in the chain.
5. Indefinite Blocking: Transactions are blocked indefinitely, waiting for resources to
become available, and no transaction can proceed.
Page 14 of 15
Unit IV
Page 15 of 15