DBMS Unit-Iii
DBMS Unit-Iii
States of Transactions
The various states of a transaction concept in DBMS are listed below:
A transaction enters into an active state when the execution process begins
Active State
state read or write operations can be performed.
Partially
A transaction goes into the partially committed state after the end of a trans
Committed
Committed When the transaction is committed to state, it has already completed its exe
State successfully. Moreover, all of its changes are recorded to the database perm
A transaction considers failed when any one of the checks fails or if the tran
Failed State
aborted while it is in the active state.
Let’s study a state transition diagram that highlights how a transaction moves
between these various states.
ACID Properties
The expansion of the term ACID defines for:
1) Atomicity: The term atomicity defines that the data remains atomic. It
means if any operation is performed on the data, either it should be
performed or executed completely or should not be executed at all. It further
means that the operation should not break in between or execute partially.
In the case of executing operations on the transaction, the operation should
be completely executed and not partially.
Example: If Remo has account A having $30 in his account from which he
wishes to send $10 to Sheero's account, which is B. In account B, a sum of $
100 is already present. When $10 will be transferred to account B, the sum
will become $110. Now, there will be two operations that will take place. One
is the amount of $10 that Remo wants to transfer will be debited from his
account A, and the same amount will get credited to account B, i.e., into
Sheero's account. Now, what happens - the first operation of debit executes
successfully, but the credit operation, however, fails. Thus, in Remo's
account A, the value becomes $20, and to that of Sheero's account, it
remains $100 as it was previously present.
In the above diagram, it can be seen that after crediting $10, the amount is
still $100 in account B. So, it is not an atomic transaction.
The below image shows that both debit and credit operations are done
successfully. Thus the transaction is atomic.
Thus, when the amount loses atomicity, then in the bank systems, this
becomes a huge issue, and so the atomicity is the main focus in the bank
systems.
2) Consistency: The word consistency means that the value should
remain preserved always. In DBMS, the integrity of the data should be
maintained, which means if a change in the database is made, it should
remain preserved always. In the case of transactions, the integrity of the
data is very essential so that the database remains consistent before and
after the transaction. The data should always be correct.
Example:
In the above figure, there are three accounts, A, B, and C, where A is making
a transaction T one by one to both B & C. There are two operations that take
place, i.e., Debit and Credit. Account A firstly debits $50 to account B, and
the amount in account A is read $300 by B before the transaction. After the
successful transaction T, the available amount in B becomes $150. Now, A
debits $20 to account C, and that time, the value read by C is $250 (that is
correct as a debit of $50 has been successfully done to B). The debit and
credit operation from account A to C has been done successfully. We can see
that the transaction is done successfully, and the value is also read correctly.
Thus, the data is consistent. In case the value read by B and C is $300, which
means that data is inconsistent because when the debit operation executes,
it will not be consistent.
Therefore, the ACID property of DBMS plays a vital role in maintaining the
consistency and availability of data in the database.
Example
Let’s take two transactions T1 and T2,
If both transactions are performed without interfering each other then it is called as
serial schedule, it can be represented as follows −
T1 T2
READ1(A)
WRITE1(A)
READ1(B)
C1
READ2(B)
WRITE2(B)
READ2(B)
C2
Non serial schedule − When a transaction is overlapped between the transaction T1
and T2.
Example
Consider the following example −
T1 T2
READ1(A)
WRITE1(A)
T1 T2
READ2(B)
WRITE2(B)
READ1(B)
WRITE1(B)
READ1(B)
Types of serializability
There are two types of serializability −
View serializability
A schedule is view-serializability if it is viewed equivalent to a serial schedule.
The rules it follows are as follows −
T1 is reading the initial value of A, then T2 also reads the initial value of A.
T1 is the reading value written by T2, then T2 also reads the value written by T1.
T1 is writing the final value, and then T2 also has the write operation as the final
value.
Conflict serializability
It orders any conflicting operations in the same way as some serial execution. A pair of
operations is said to conflict if they operate on the same data item and one of them is a
write operation.
That means
Readi(x) readj(x) - non conflict read-read operation
Readi(x) writej(x) - conflict read-write operation.
Writei(x) readj(x) - conflict write-read operation.
Writei(x) writej(x) - conflict write-write operation.
In case of transaction the term ACID has been used significantly to state some of
important properties that a transaction must follow. We all know ACID stands for
Atomicity, Consistency, Isolation and Durability and these properties collectively called
as ACID Properties.
Isolation
It determines the visibility of transactions of other systems. A lower level allows every
user to access the same data. Therefore, it involves high risk of data privacy and
security of the system. However, a higher isolation level reduces the type of
concurrency over the data but requires more resources and is slower than lower
isolation levels.
The isolation protocols help safeguards the data from unwanted transactions. They
maintain the integrity of every data by defining how and when the changes made by
one operation are visible to others.
Levels of isolation
Isolation
It determines the visibility of transactions of other systems. A lower level allows every
user to access the same data. Therefore, it involves high risk of data privacy and
security of the system. However, a higher isolation level reduces the type of
concurrency over the data but requires more resources and is slower than lower
isolation levels.
The isolation protocols help safeguards the data from unwanted transactions. They
maintain the integrity of every data by defining how and when the changes made by
one operation are visible to others.
Example
Consider an example of isolation.
What is the isolation level of transaction E?
session begins
SET GLOBAL TRANSACTION
ISOLATION LEVEL SERIALIZABLE;
session ends
session begins
SET SESSION TRANSACTION
ISOLATION LEVEL REPEATABLE READ;
transaction A
transaction B
SET TRANSACTION
ISOLATION LEVEL READ UNCOMMITTED;
transaction C
SET TRANSACTION
ISOLATION LEVEL READ COMMITTED;
transaction D
transaction E
session ends
Check which option −
A- Serializable
B- Repeatable read
C- Read uncommitted
Step 1 − In the above program, the first session starts and ends without doing
any transaction.
Step 2 − The second session begins at session-level with isolation level
"Repeatable Read". Transaction A & B gets executed with these settings.
Step 3 − Once again a new transaction begins with isolation level "Read
uncommitted". This setting is used only for "Transaction C" since "Set
transaction" alone is mentioned. If the "SET transaction" is used without global or
session keywords, then these particular settings will work only for a single
transaction.
Step 4 − Once again "Set Transaction" with isolation level Read committed works
only for Transaction D. (Refer step 3 for reason)
Step 5 − "Transaction E" gets continued at the "Repeatable Read" since the
transaction started at step 2 has not ended still. Transaction isolation level set at
Step 3 and Step 4 vanishes once a single transaction is executed. So,
automatically "Transaction E" will refer to the prior transaction settings.
Concurrency control concept comes under the Transaction in database management
system (DBMS). It is a procedure in DBMS which helps us for the management of two
simultaneous processes to execute without conflicts between each other, these
conflicts occur in multi user systems.
Concurrency can simply be said to be executing multiple transactions at a time. It is
required to increase time efficiency. If many transactions try to access the same data,
then inconsistency arises. Concurrency control required to maintain consistency data.
For example, if we take ATM machines and do not use concurrency, multiple persons
cannot draw money at a time in different places. This is where we need concurrency.
Advantages
The advantages of concurrency control are as follows −
Waiting time will be decreased.
Response time will decrease.
Resource utilization will increase.
System performance & Efficiency is increased.
1. Shared lock:
o It is also known as a Read-only lock. In a shared lock, the data item can only
read by the transaction.
o It can be shared between the transactions because when the transaction
holds a lock, then it can't update the data on the data item.
2. Exclusive lock:
o In the exclusive lock, the data item can be both reads as well as written by
the transaction.
o This lock is exclusive, and in this lock, multiple transactions do not modify the
same data simultaneously.
Growing phase: In the growing phase, a new lock on the data item may be
acquired by the transaction, but none can be released.
In the below example, if lock conversion is allowed then the following phase
can happen:
Example:
The following way shows how unlocking and locking work with 2-PL.
Transaction T1:
Transaction T2:
Deadlock prevention.
Deadlock avoidance.
Deadlock detection and removal.
All of the three approaches can be incorporated in both a centralized and a distributed
database system.
Deadlock Prevention
The deadlock prevention approach does not allow any transaction to acquire locks that
will lead to deadlocks. The convention is that when more than one transactions request
for locking the same data item, only one of them is granted the lock.
One of the most popular deadlock prevention methods is pre-acquisition of all the locks.
In this method, a transaction acquires all the locks before starting to execute and retains
the locks for the entire duration of transaction. If another transaction needs any of the
already acquired locks, it has to wait until all the locks it needs are available. Using this
approach, the system is prevented from being deadlocked since none of the waiting
transactions are holding any lock.
Deadlock Avoidance
The deadlock avoidance approach handles deadlocks before they occur. It analyzes the
transactions and the locks to determine whether or not waiting leads to a deadlock.
The method can be briefly stated as follows. Transactions start executing and request
data items that they need to lock. The lock manager checks whether the lock is
available. If it is available, the lock manager allocates the data item and the transaction
acquires the lock. However, if the item is locked by some other transaction in
incompatible mode, the lock manager runs an algorithm to test whether keeping the
transaction in waiting state will cause a deadlock or not. Accordingly, the algorithm
decides whether the transaction can wait or one of the transactions should be aborted.
There are two algorithms for this purpose, namely wait-die and wound-wait. Let us
assume that there are two transactions, T1 and T2, where T1 tries to lock a data item
which is already locked by T2. The algorithms are as follows −
Wait-Die − If T1 is older than T2, T1 is allowed to wait. Otherwise, if T1 is
younger than T2, T1 is aborted and later restarted.
Wound-Wait − If T1 is older than T2, T2 is aborted and later restarted.
Otherwise, if T1 is younger than T2, T1 is allowed to wait.
Deadlock Detection and Removal
The deadlock detection and removal approach runs a deadlock detection algorithm
periodically and removes deadlock in case there is one. It does not check for deadlock
when a transaction places a request for a lock. When a transaction requests a lock, the
lock manager checks whether it is available. If it is available, the transaction is allowed
to lock the data item; otherwise the transaction is allowed to wait.
Since there are no precautions while granting lock requests, some of the transactions
may be deadlocked. To detect deadlocks, the lock manager periodically checks if the
wait-forgraph has cycles. If the system is deadlocked, the lock manager chooses a
victim transaction from each cycle. The victim is aborted and rolled back; and then
restarted later. Some of the methods used for victim selection are −
The entire DBMS is a very complex structure with multiple transactions being
performed and carried out every second. The toughness and strength of a
system depend not only on the complex and secured architecture of a
system but also in the way how data are managed and maintained in the
worst cases. If the underlying architecture fails or crashes, then there must
be some techniques and procedures by which the lost data during a
transaction gets recovered.
Whatever the grounds of the failure are, there are two principal things that
you have to consider:
Recovery Facilities
Every DBMS should offer the following facilities to help out with the recovery
mechanism:
Failure Classification
To find that where the problem has occurred, we generalize a failure into the
following categories:
1. Transaction failure
2. System crash
3. Disk failure
1. Transaction failure
The transaction failure occurs when it fails to execute or when it
reaches a point from where it can't go any further. If a few transaction
or process is hurt, then this is called as transaction failure.
2. System Crash
o System failure can occur due to power failure or other hardware or
software failure. Example: Operating system error.
3. Disk Failure
o It occurs where hard-disk drives or storage drives used to fail
frequently. It was a common problem in the early days of technology
evolution.
o Disk failure occurs due to the formation of bad sectors, disk head
crash, and unreachability to the disk or any other failure, which destroy
all or part of disk storage.