0% found this document useful (0 votes)
6 views6 pages

Unit4 Notes

Download as doc, pdf, or txt
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 6

UNIT-4

Transactions and Concurrency Management


Q1) What is a transaction Explain its properties
Ans) Def:
“Transaction is a logical unit of a task that contains one or more SQL statements. A transaction is an
automic unit.” It is a UNIT OF WORK define to work on a database.
--> it is seen DBMS as a series or list of actions
-->the actions that can be executed by a transaction include reads and writes of database objects.
A transaction is a very small unit of a program and it may contain several lowlevel tasks. A transaction in a
database system must maintain Atomicity, Consistency, Isolation, and Durability − commonly known as
ACID properties − in order to ensure accuracy, completeness, and data integrity.
Atomicity − This property states that a transaction must be treated as an atomic unit, that is, either all of its
operations are executed or none. There must be no state in a database where a transaction is left partially
completed. States should be defined either before the execution of the transaction or after the
execution/abortion/failure of the transaction.
Consistency − The database must remain in a consistent state after any transaction. No transaction should
have any adverse effect on the data residing in the database. If the database was in a consistent state before
the execution of a transaction, it must remain consistent after the execution of the transaction as well.
Durability − The database should be durable enough to hold all its latest updates even if the system fails or
restarts. If a transaction updates a chunk of data in a database and commits, then the database will hold the
modified data. If a transaction commits but the system fails before the data could be written on to the disk,
then that data will be updated once the system springs back into action.
Isolation − In a database system where more than one transaction are being executed simultaneously and in
parallel, the property of isolation states that all the transactions will be carried out and executed as if it is the
only transaction in the system. No transaction will affect the existence of any other transaction.
Q2)Explain Transaction model
Ans)States of Transactions
A transaction in a database can be in one of the following states −

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 −

Re-start the transaction


Kill the transaction
Committed − If a transaction executes all its operations successfully, it is said to be committed. All its
effects are now permanently established on the database system.
Q3)Write about concurrency control and its techniques
Ans)In a multiprogramming environment where multiple transactions can be executed simultaneously, it is
highly important to control the concurrency of transactions. We have concurrency control protocols to
ensure atomicity, isolation, and serializability of concurrent transactions. Concurrency control protocols can
be broadly divided into two categories −
Lock based protocols
Time stamp based protocols
Lock-based Protocols
Database systems equipped with lock-based protocols use a mechanism by which any transaction cannot
read or write data until it acquires an appropriate lock on it. Locks are of two kinds −
Binary Locks − A lock on a data item can be in two states; it is either locked or unlocked.
Shared/exclusive − This type of locking mechanism differentiates the locks based on their uses. If a lock is
acquired on a data item to perform a write operation, it is an exclusive lock. Allowing more than one
transaction to write on the same data item would lead the database into an inconsistent state. Read locks are
shared because no data value is being changed.
Timestamp-based Protocols
The most commonly used concurrency protocol is the timestamp based protocol. This protocol uses either
system time or logical counter as a timestamp.
Lock-based protocols manage the order between the conflicting pairs among transactions at the time of
execution, whereas timestamp-based protocols start working as soon as a transaction is created.
Every transaction has a timestamp associated with it, and the ordering is determined by the age of the
transaction. A transaction created at 0002 clock time would be older than all other transactions that come
after it. For example, any transaction 'y' entering the system at 0004 is two seconds younger and the priority
would be given to the older one.
Q4)What is Two-Phase Locking (2PL)?

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.

This protocol can be divided into two phases,


1. In Growing Phase, a transaction obtains locks, but may not release any lock.
2. In Shrinking Phase, a transaction may release locks, but may not obtain any lock.

Two-Phase Locking does not ensure freedom from deadlocks.

Types of Two – Phase Locking Protocol

Following are the types of two – phase locking protocol:

1. Strict Two – Phase Locking Protocol


2. Rigorous Two – Phase Locking Protocol
3. Conservative Two – Phase Locking Protocol

1. Strict Two-Phase Locking Protocol


Strict Two-Phase Locking Protocol avoids cascaded rollbacks.
This protocol not only requires two-phase locking but also all exclusive-locks should be held until the
transaction commits or aborts.
It is not deadlock free.
It ensures that if data is being modified by one transaction, then other transaction cannot read it until first
transaction commits.
Most of the database systems implement rigorous two – phase locking protocol.
2. Rigorous Two-Phase Locking
Rigorous Two – Phase Locking Protocol avoids cascading rollbacks.
This protocol requires that all the share and exclusive locks to be held until the transaction commits.
3. Conservative Two-Phase Locking Protocol
Conservative Two – Phase Locking Protocol is also called as Static Two – Phase Locking Protocol.
This protocol is almost free from deadlocks as all required items are listed in advanced.
It requires locking of all data items to access before the transaction starts.

Q4)What is a Dead Lock Explain

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.

A deadlock may occur if all the following conditions holds true.

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.

Once a deadlock is detected it is being corrected by following methods:

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.

Q6) Write about optimistic concurrency control

Ans) Optimistic Methods of Concurrency Control :


The optimistic method of concurrency control is based on the assumption that conflicts of database
operations are rare and that it is better to let transactions run to completion and only check for conflicts
before they commit.
An optimistic concurrency control method is also known as validation or certification methods. No checking
is done while the transaction is executing. The optimistic method does not require locking or timestamping
techniques. Instead, a transaction is executed without restrictions until it is committed. In optimistic
methods, each transaction moves through the following phases:
1. Read phase.
2. Validation or certification phase.
3. Write phase.

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.

Q5) Explain Database Recovery Techniques


It is imperative to have a backup of the database in case the original is corrupted or lost because of any
reason. Using this backup, the database can be recovered as it was before the failure.
Database backup basically means that a duplicate of the database information and data is created and stored
in backup server just to be on the safe side. Transaction logs are also stored in the backup along with the
database data because without them, the data would be useless.
Reasons of Failure in a Database
There can be multiple reasons of failure in a database because of which a database backup and recovery plan
is required. Some of these reasons are:

 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.

You might also like