Unit-7 Class1
Unit-7 Class1
SYSTEM
Lecture notes on Transaction Management
Transaction
o The transaction is a set of logically related operation. It contains a group of tasks.
o A transaction is an action or series of actions. It is performed by a single user to perform operations for
accessing the contents of the database.
Example: Suppose an employee of bank transfers Rs 800 from X's account to Y's account. This small transaction
contains several low-level tasks:
Operations of Transaction:
Read(X): Read operation is used to read the value of X from the database and stores it in a buffer in main memory.
Write(X): Write operation is used to write the value back to the database from the buffer.
Let's take an example to debit transaction from an account which consists of following operations:
R(X);
X = X - 500;
W(X);
o The first operation reads X's value from database and stores it in a buffer.
o The second operation will decrease the value of X by 500. So buffer will contain 3500.
o The third operation will write the buffer's value to the database. So X's final value will be 3500.
But it may be possible that because of the failure of hardware, software or power, etc. that transaction may fail before
finished all the operations in the set.
For example: If in the above transaction, the debit transaction fails after executing operation 2 then X's value will
remain 4000 in the database which is not acceptable by the bank.
Transaction property: The transaction has the four properties. These are used to maintain consistency in a database,
before and after the transaction.
Property of Transaction
1. Atomicity
2. Consistency
3. Isolation
4. Durability
Atomicity:
o It states that all operations of the transaction take place at once if not, the transaction is aborted.
o There is no midway, i.e., the transaction cannot occur partially. Each transaction is treated as one unit and
either run to completion or is not executed at all.
Abort: If a transaction aborts then all the changes made are not visible.
Commit: If a transaction commits then all the changes made are visible.
Example: Let's assume that following transaction T consisting of T1 and T2. A consists of Rs 600 and B consists of
Rs 300. Transfer Rs 100 from account A to account B.
T1 T2
Read(A) Read(B)
A:= A-100 Y:= Y+100
Write(A) Write(B)
If the transaction T fails after the completion of transaction T1 but before completion of transaction T2, then the amount
will be deducted from A but not added to B. This shows the inconsistent database state. In order to ensure correctness
of database state, the transaction must be executed in entirety.
Consistency
o The integrity constraints are maintained so that the database is consistent before and after the transaction.
o The execution of a transaction will leave a database in either its prior stable state or a new stable state.
o The consistent property of database states that every transaction sees a consistent database instance.
o The transaction is used to transform the database from one consistent state to another consistent state.
For example: The total amount must be maintained before or after the transaction.
Therefore, the database is consistent. In the case when T1 is completed but T2 fails, then inconsistency will occur.
Isolation:
o It shows that the data which is used at the time of execution of a transaction cannot be used by the second
transaction until the first one is completed.
o In isolation, if the transaction T1 is being executed and using the data item X, then that data item can't be
accessed by any other transaction T2 until the transaction T1 ends.
o The concurrency control subsystem of the DBMS enforced the isolation property.
Durability:
o The durability property is used to indicate the performance of the database's consistent state. It states that the
transaction made the permanent changes.
o They cannot be lost by the erroneous operation of a faulty transaction or by the system failure. When a
transaction is completed, then the database reaches a state known as the consistent state. That consistent state
cannot be lost, even in the event of a system's failure.
o The recovery subsystem of the DBMS has the responsibility of Durability property.
States of Transaction: In a database, the transaction can be in one of the following states -
Active state:
o The active state is the first state of every transaction. In this state, the transaction is being executed.
o For example: Insertion or deletion or updating a record is done here. But all the records are still not saved to
the database.
Partially committed:
o In the partially committed state, a transaction executes its final operation, but the data is still not saved to the
database.
o In the total mark calculation example, a final display of the total marks step is executed in this state.
Committed: A transaction is said to be in a committed state if it executes all its operations successfully. In this state,
all the effects are now permanently saved on the database system.
Failed state:
o If any of the checks made by the database recovery system fails, then the transaction is said to be in the failed
state.
o In the example of total mark calculation, if the database is not able to fire a query to fetch the marks, then the
transaction will fail to execute.
Aborted:
o If any of the checks fail and the transaction has reached a failed state then the database recovery system will
make sure that the database is in its previous consistent state. If not then it will abort or roll back the transaction
to bring the database into a consistent state.
o If the transaction fails in the middle of the transaction then before executing the transaction, all the executed
transactions are rolled back to its consistent state.
o After aborting the transaction, the database recovery module will select one of the two operations:
1. Re-start the transaction
2. Kill the transaction
Concurrency Control: Concurrency Control is the management procedure that is required for controlling concurrent
execution of the operations that take place on a database.
When multiple transactions execute concurrently in an uncontrolled or unrestricted manner, then it might lead to
several problems. These problems are commonly referred to as concurrency problems in a database environment.
The five concurrency problems that can occur in the database are:
Lock-Based Protocol: In this type of protocol, any transaction cannot read or write data until it acquires an appropriate
lock on it. There are two types of lock:
1. Shared lock:
It is also known as a Read-only lock. In a shared lock, the data item can only read by the transaction.
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:
In the exclusive lock, the data item can be both reads as well as written by the transaction.
This lock is exclusive, and in this lock, multiple transactions do not modify the same data simultaneously.
It is the simplest way of locking the data while transaction. Simplistic lock-based protocols allow all the transactions
to get the lock on the data before insert or delete or update on it. It will unlock the data item after completing the
transaction.
Growing phase: In the growing phase, a new lock on the data item may be acquired by the transaction, but none can
be released.
Shrinking phase: In the shrinking phase, existing lock held by the transaction may be released, but no new locks can
be acquired.
In the below example, if lock conversion is allowed then the following phase can happen:
The following way shows how unlocking and locking work with 2-PL.
Transaction T1:
o Growing phase: from step 1-3
o Shrinking phase: from step 5-7
o Lock point: at 3
Transaction T2:
o Growing phase: from step 2-6
o Shrinking phase: from step 8-9
o Lock point: at 6
Types of Recovery Techniques in DBMS: Database recovery techniques are used in database management systems
(DBMS) to restore a database to a consistent state after a failure or error has occurred. The main goal of recovery
techniques is to ensure data integrity and consistency and prevent data loss.
There are mainly two types of recovery techniques used in DBMS
1. Rollback/Undo Recovery Technique
2. Commit/Redo Recovery Technique
1. Rollback/Undo Recovery Technique: The rollback/undo recovery technique is based on the principle of backing
out or undoing the effects of a transaction that has not been completed successfully due to a system failure or error.
This technique is accomplished by undoing the changes made by the transaction using the log records stored in the
transaction log. The transaction log contains a record of all the transactions that have been performed on the database.
The system uses the log records to undo the changes made by the failed transaction and restore the database to its
previous state.
2. Commit/Redo Recovery Technique: The commit/redo recovery technique is based on the principle of reapplying
the changes made by a transaction that has been completed successfully to the database. This technique is
accomplished by using the log records stored in the transaction log to redo the changes made by the transaction that
was in progress at the time of the failure or error. The system uses the log records to reapply the changes made by
the transaction and restore the database to its most recent consistent state.
In addition to these two techniques, there is also a third technique called checkpoint recovery.
Checkpoint Recovery: is a technique used to reduce the recovery time by periodically saving the state of the database
in a checkpoint file. In the event of a failure, the system can use the checkpoint file to restore the database to the most
recent consistent state before the failure occurred, rather than going through the entire log to recover the database.
Log-Based Recovery: The log is a sequence of records. Log of each transaction is maintained in some stable storage
so that if any failure occurs, then it can be recovered from there. If any operation is performed on the database, then it
will be recorded in the log. But the process of storing the logs should be done before the actual transaction is applied
in the database.
Let's assume there is a transaction to modify the City of a student. The following logs are written for this transaction.
When the transaction is initiated, then it writes 'start' log.
<Tn, Start> :When the transaction modifies the City from 'Noida' to 'Bangalore', then another log is written to the
file.
<Tn, City, 'Noida', 'Bangalore' > :When the transaction is finished, then it writes another log to indicate the end of
the transaction.
<Tn, Commit> :There are two approaches to modify the database:
1. Deferred Database Modification: The deferred modification technique occurs if the transaction does not modify
the database until it has committed. In this method, all the logs are created and stored in the stable storage, and the
database is updated when a transaction commits.
2. Immediate Database Modification: The Immediate modification technique occurs if database modification occurs
while the transaction is still active. In this technique, the database is modified immediately after every operation. It
follows an actual database modification.
Recovery using Log records:When the system is crashed, then the system consults the log to find which transactions
need to be undone and which need to be redone.
1. If the log contains the record <Ti, Start> and <Ti, Commit> or <Ti, Commit>, then the Transaction Ti needs
to be redone.
2. If log contains record<Tn, Start> but does not contain the record either <Ti, commit> or <Ti, abort>, then
the Transaction Ti needs to be undone.