Unit4 Notes
Unit4 Notes
Unit4 Notes
Active − In this state, the transaction is being executed. This is the initial state of every transaction.
Partially Committed − When a transaction executes its final operation, it is said to be in a partially
committed state.
Failed − A transaction is said to be in a failed state if any of the checks made by the database recovery
system fails. A failed transaction can no longer proceed further.
Aborted − If any of the checks fails and the transaction has reached a failed state, then the recovery
manager rolls back all its write operations on the database to bring the database back to its original state
where it was prior to the execution of the transaction. Transactions in this state are called aborted. The
database recovery module can select one of the two operations after a transaction aborts −
Two-Phase Locking (2PL) is a concurrency control method which divides the execution phase of a
transaction into three parts.
It ensures conflict serializable schedules.
If read and write operations introduce the first unlock operation in the transaction, then it is said to be Two-
Phase Locking Protocol.
Ans)A deadlock is a condition wherein two or more tasks are waiting for each other in order to be finished
but none of the task is willing to give up the resources that other task needs. In this situation no task ever
gets finished and is in waiting state forever.
Mutual exclusion condition: There must be at least one resource that cannot be used by more than one
process at a time.
Hold and wait condition: A process that is holding a resource can request for additional resources that are
being held by other processes in the system.
No preemption condition: A resource cannot be forcibly taken from a process. Only the process can release
a resource that is being held by it.
Circular wait condition: A condition where one process is waiting for a resource that is being held by
second process and second process is waiting for third process ….so on and the last process is waiting for
the first process. Thus making a circular chain of waiting.
There are 3 techniques to control the deadlocks. They are
Deadlock detection
Resource scheduler is one that keeps the track of resources allocated to and requested by processes. Thus, if
there is a deadlock it is known to the resource scheduler. This is how a deadlock is detected.
Terminating processes involved in deadlock: Terminating all the processes involved in deadlock or
terminating process one by one until deadlock is resolved can be the solutions but both of these approaches
are not good. Terminating all processes cost high and partial work done by processes gets lost. Terminating
one by one takes lot of time because each time a process is terminated, it needs to check whether the
deadlock is resolved or not. Thus, the best approach is considering process age and priority while
terminating them during a deadlock condition.
Resource Preemption: Another approach can be the preemption of resources and allocation of them to the
other processes until the deadlock is resolved.
Deadlock prevention
We have learnt that if all the four Coffman conditions hold true then a deadlock occurs so preventing one or
more of them could prevent the deadlock.
Removing mutual exclusion: All resources must be sharable that means at a time more than one processes
can get a hold of the resources. That approach is practically impossible.
Removing hold and wait condition: This can be removed if the process acquires all the resources that are
needed before starting out. Another way to remove this to enforce a rule of requesting resource when there
are none in held by the process.
Preemption of resources: Preemption of resources from a process can result in rollback and thus this needs
to be avoided in order to maintain the consistency and stability of the system.
Avoid circular wait condition: This can be avoided if the resources are maintained in a hierarchy and
process can hold the resources in increasing order of precedence. This avoid circular wait. Another way of
doing this to force one resource per process rule – A process can request for a resource once it releases the
resource currently being held by it. This avoids the circular wait.
Deadlock Avoidance
Deadlock can be avoided if resources are allocated in such a way that it avoids the deadlock occurrence.
There are two algorithms for deadlock avoidance.
Wait/Die
Wound/Wait
Here is the table representation of resource allocation for each algorithm. Both of these algorithms take
process age into consideration while determining the best possible way of resource allocation for deadlock
avoidance.
a. Read phase :
In a Read phase, the updates are prepared using private (or local) copies (or versions) of the granule. In this
phase, the transaction reads values of committed data from the database, executes the needed computations,
and makes the updates to a private copy of the database values. All update operations of the transaction are
recorded in a temporary update file, which is not accessed by the remaining transactions.
It is conventional to allocate a timestamp to each transaction at the end of its Read to determine the set of
transactions that must be examined by the validation procedure. These set of transactions are those who have
finished their Read phases since the start of the transaction being verified
b. Validation or certification phase :
In a validation (or certification) phase, the transaction is validated to assure that the changes made will not
affect the integrity and consistency of the database.
If the validation test is positive, the transaction goes to the write phase. If the validation test is negative, the
transaction is restarted, and the changes are discarded. Thus, in this phase the list of granules is checked for
conflicts. If conflicts are detected in this phase, the transaction is aborted and restarted. The validation
algorithm must check that the transaction has :
Seen all modifications of transactions committed after it starts.
Not read granules updated by a transaction committed after its start.
c. Write phase :
In a Write phase, the changes are permanently applied to the database and the updated granules are made
public. Otherwise, the updates are discarded and the transaction is restarted. This phase is only for the Read-
Write transactions and not for Read-only transactions.
User Error - Normally, user error is the biggest reason of data destruction or corruption in a
database. To rectify the error, the database needs to be restored to the point in time before the error
occured.
Hardware Failure - This can also lead to loss of data in a database. The database is stored on
multiple hard drives across various locations. These hard drives may sometimes malfunction leading
to database corruption. So, it is important to periodically change them.
Catastrophic Event - A catastrophic event can be a natural calamity like a flood or earthquake or
deliberate sabotage such as hacking of the database. Either way, the database data may be corrupted
and backup may be required.
Methods of Backup
The different methods of backup in a database are:
Full Backup - This method takes a lot of time as the full copy of the database is made including the
data and the transaction records.
Transaction Log - Only the transaction logs are saved as the backup in this method. To keep the
backup file as small as possible, the previous transaction log details are deleted once a new backup
record is made.
Differential Backup - This is similar to full backup in that it stores both the data and the transaction
records. However only that information is saved in the backup that has changed since the last full
backup. Because of this, differential backup leads to smaller files.
Database Recovery
There are two methods that are primarily used for database recovery. These are:
Log based recovery - In log based recovery, logs of all database transactions are stored in a secure
area so that in case of a system failure, the database can recover the data. All log information, such as
the time of the transaction, its data etc. should be stored before the transaction is executed.
Shadow paging - In shadow paging, after the transaction is completed its data is automatically
stored for safekeeping. So, if the system crashes in the middle of a transaction, changes made by it
will not be reflected in the database.