DBMS Unit-4
DBMS Unit-4
Now that we understand what transaction is, we should understand what are the
problems associated with it.
The main problem that can happen during a transaction is that the transaction can fail
before finishing the all the operations in the set. This can happen due to power failure,
system crash etc.
This is a serious problem that can leave database in an inconsistent state. Assume that
transaction fail after third operation (see the example above) then the amount would
be deducted from your account but your friend will not receive it.
Commit: If all the operations in a transaction are completed successfully then commit those
changes to the database permanently.
Rollback: If any of the operation fails then rollback all the changes done by previous
operations.
STATES OF TRANSACTION
Transactions can be implemented using SQL queries and Server. In the below-given
diagram, you can see how transaction states works.
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
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.
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:
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.
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.
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 The recovery subsystem of the DBMS has the responsibility of Durability property.
Shadow copy:
In the shadow-copy scheme, a transaction that wants to update the database first creates a
complete copy of the database.
All updates are done on the new database copy, leaving the original copy, the shadow copy,
untouched. If at any point the transaction has to be aborted, the system merely deletes the
new copy. The old copy of the database has not been affected.
This scheme is based on making copies of the database, called shadow copies, assumes that
only one transaction is active at a time.
The scheme also assumes that the database is simply a file on disk. A pointer called db-
pointer is maintained on disk; it points to the current copy of the database.
First, the operating system is asked to make sure that all pages of the new copy of the
database have been written out to disk. (Unix systems use the flush command for this
purpose.)
After the operating system has written all the pages to disk, the database system updates the
pointer db-pointer to point to the new copy of the database;
the new copy then becomes the current copy of the database. The old copy of the database is
then deleted.
Figure below depicts the scheme, showing the database state before and after the update.
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.
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.
o In the given (a) figure, Schedule A shows the serial schedule where T1 followed by
T2.
o In the given (b) figure, Schedule B shows the serial schedule where T2 followed by
T1.
2. NON-SERIAL SCHEDULE
o If interleaving of operations is allowed, then there will be non-serial schedule.
o It contains many possible orders in which the system can execute the individual
operations of the transactions.
o 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
o The serializability of schedules is used to find non-serial schedules that allow the
transaction to execute concurrently without interfering with one another.
o It identifies which schedules are correct when executions of the transaction have
interleaving of their operations.
o A non-serial schedule will be serializable if its result is equal to the result of its
transactions executed serially.
SERIALIZABILITY IN DBMS
1. Conflict Serializability
2. View Serializability
Conflict Serializability
If a given non-serial schedule can be converted into a serial schedule by swapping its non-
conflicting operations, then it is called as a conflict serializable schedule.
Conflicting Operations
Two operations are called as conflicting operations if all the following conditions hold true
for them-
Example-
In this schedule,
Follow the following steps to check whether a given non-serial schedule is conflict
serializable or not-
Follow the following steps to check whether a given non-serial schedule is conflict
serializable or not-
Step-01:
Step-02:
Start creating a precedence graph by drawing one node for each transaction.
Step-03:
If there is no cycle found, then the schedule is conflict serializable otherwise not.
VIEW SERIALIZABILITY?
View Serializability is a process to find out that a given schedule is view serializable or not.
To check whether a given schedule is view serializable, we need to check whether the given
schedule is View Equivalent to its serial schedule. Lets take an example to understand what I
mean by that.
View Serializability
o A schedule will view serializable if it is view equivalent to a serial schedule.
o If a schedule is conflict serializable, then it will be view serializable.
o The view serializable which does not conflict serializable contains blind writes.
View Equivalent
Two schedules S1 and S2 are said to be view equivalent if they satisfy the following
conditions:
1. Initial Read:
An initial read of both schedules must be the same. Suppose two schedule S1 and S2. In
schedule S1, if a transaction T1 is reading the data item A, then in S2, transaction T1 should
also read A.
2. Updated Read
3. Final Write
A final write must be the same between both the schedules. In schedule S1, if a transaction
T1 updates A at last then in S2, final writes operations should also be done by T1.
Recoverability of Schedule
Sometimes a transaction may not execute completely due to a software issue, system crash or
hardware failure. In that case, the failed transaction has to be rollback. But
some other transaction may also have used value produced by the failed transaction. So we
also have to rollback those transactions.
1. Read Uncommitted – Read Uncommitted is the lowest isolation level. In this level,
one transaction may read not yet committed changes made by other transaction, thereby
allowing dirty reads. In this level, transactions are not isolated from each other.
2. Read Committed – This isolation level guarantees that any data read is committed at
the moment it is read. Thus it does not allows dirty read. The transaction holds a read or
write lock on the current row, and thus prevent other transactions from reading,
updating or deleting it.
3. Repeatable Read – This is the most restrictive isolation level. The transaction holds
read locks on all rows it references and writes locks on all rows it inserts, updates, or
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 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.
In the transaction process, a system usually allows executing more than one transaction
simultaneously. This process is called a concurrent execution.
In a database transaction, the two main operations are READ and WRITE operations. So,
there is a need to manage these two operations in the concurrent execution of the transactions
as if these operations are not performed in an interleaved manner, and the data may become
inconsistent. So, the following problems occur with the Concurrent Execution of the
operations:
t1 Read(A)
t2 A=A-50
t3 Read(A)
t4 A=A+50
t5 Write(A)
t6 Write(A)
Here,
At t1 time, T1 transaction reads the value of A i.e., 100.
At t2 time, T1 transaction deducts the value of A by 50.
At t3 time, T2 transactions read the value of A i.e., 100.
At t4 time, T2 transaction adds the value of A by 150.
At t5 time, T1 transaction writes the value of A data item on the basis of value seen at time t2
i.e., 50.
This type of problem occurs when one transaction T1 updates a data item of the database, and
then that transaction fails due to some reason, but its updates are accessed by some other
transaction.
Example: Let’s take the value of A is 100
t1 Read(A)
t2 A=A+20
t3 Write(A)
t4 Read(A)
t5 A=A+30
t6 Write(A)
t7 Write(B)
Here,
At t1 time, T1 transaction reads the value of A i.e., 100.
t1 Read(A)
t2 Read(A)
t3 A=A+30
t4 Write(A)
t5 Read(A)
Here,
At t1 time, T1 transaction reads the value of A i.e., 100.
CONCURRENCY CONTROL
Concurrency Control is the working concept that is required for controlling and managing the
concurrent execution of database operations and thus avoiding the inconsistencies in the
database. Thus, for maintaining the concurrency of the database, we have the concurrency
control protocols.
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:
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.
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.
o The two-phase locking protocol divides the execution phase of the transaction into
three parts.
o In the first part, when the execution of the transaction starts, it seeks permission for
the lock it requires.
o In the second part, the transaction acquires all the locks. The third phase is started as
soon as the transaction releases its first lock.
o In the third phase, the transaction cannot demand any new locks. It only releases the
acquired locks.
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:
o The first phase of Strict-2PL is similar to 2PL. In the first phase, after acquiring all the
locks, the transaction continues to execute normally.
o The only difference between 2PL and strict 2PL is that Strict-2PL does not release a
lock after using it.
o Strict-2PL waits until the whole transaction to commit, and then it releases all the
locks at a time.
o The priority of the older transaction is higher that's why it executes first. To determine
the timestamp of the transaction, this protocol uses system time or logical counter.
o The lock-based protocol is used to manage the order between conflicting pairs among
transactions at the execution time. But Timestamp based protocols start working as
soon as a transaction is created.
1. Check the following condition whenever a transaction Ti issues a Read (X) operation:
Where,
Validation phase is also known as optimistic concurrency control technique. In the validation
based protocol, the transaction is executed in the following three phases:
Validation (Ti): It contains the time when Ti finishes its read phase and starts its validation
phase.
o This protocol is used to determine the time stamp for the transaction for serialization
using the time stamp of the validation phase, as it is the actual phase which
determines if the transaction will commit or rollback.
o While executing the transaction, it ensures a greater degree of concurrency and also
less number of conflicts.
Thomas Write Rule provides the guarantee of serializability order for the protocol. It
improves the Basic Timestamp Ordering Algorithm.
o If TS(T) < W_TS(X) then don't execute the W_item(X) operation of the transaction
and continue processing.
o If neither condition 1 nor condition 2 occurs, then allowed to execute the WRITE
operation by transaction Ti and set W_TS(X) to TS(T).
MULTIPLE GRANULARITY
Multiple Granularity:
o It can be defined as hierarchically breaking up the database into blocks which can be
locked.
o The Multiple Granularity protocol enhances concurrency and reduces lock overhead.
o It maintains the track of what to lock and how to lock.
o It makes easy to decide either to lock a data item or to unlock a data item. This type of
hierarchy can be graphically represented as a tree.
o The area consists of children nodes which are known as files. No file can be present in
more than one area.
o Finally, each file contains child nodes known as records. The file has exactly those
records that are its child nodes. No records represent in more than one file.
o Hence, the levels of the tree starting from the top level are as follows:
o Database
Recovery and Atomicity – Log – Based Recovery – Recovery with Concurrent Transactions
– Check Points - Buffer Management – Failure with loss of nonvolatile storage-Advance
Recovery systems- ARIES Algorithm, Remote Backup systems. File organization – various
kinds of indexes - B+ Trees- Query Processing – Relational Query Optimization.
When a system crashes, it may have several transactions being executed and various
files opened for them to modify the data items.
But according to ACID properties of DBMS, atomicity of transactions as a whole
must be maintained, that is, either all the operations are executed or none.
Database recovery means recovering the data when it get deleted, hacked or
damaged accidentally.
Atomicity is must whether is transaction is over or not it should reflect in the database
permanently or it should not effect the database at all.
It should check the states of all the transactions, which were being executed.
A transaction may be in the middle of some operation; the DBMS must ensure
the atomicity of the transaction in this case.
There are two types of techniques, which can help a DBMS in recovering as well as
maintaining the atomicity of a transaction −
Maintaining the logs of each transaction, and writing them onto some stable storage
before actually modifying the database.
Maintaining shadow paging, where the changes are done on a volatile memory, and
later, the actual database is updated.
o 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.
o If any operation is performed on the database, then it will be recorded in the log.
o But the process of storing the logs should be done before the actual transaction is
applied in the database.
o The deferred modification technique occurs if the transaction does not modify the
database until it has committed.
o In this method, all the logs are created and stored in the stable storage, and the
database is updated when a transaction commits.
Concurrency control means that multiple transactions can be executed at the same time and
then the interleaved logs occur. But there may be changes in transaction results so maintain
the order of execution of those transactions.
During recovery, it would be very difficult for the recovery system to backtrack all the logs
and then start recovering.
Recovery with concurrent transactions can be done in the following four ways.
In this scheme, the recovery scheme depends greatly on the concurrency control scheme
that is used. So, to rollback a failed transaction, we must undo the updates performed by the
transaction.
Transaction rollback :
In this scheme, we rollback a failed transaction by using the log.
The system scans the log backward a failed transaction, for every log record found in
the log the system restores the data item.
Checkpoints :
Checkpoints is a process of saving a snapshot of the applications state so that it can
restart from that point in case of failure.
Checkpoint is a point of time at which a record is written onto the database form the
buffers.
Checkpoint shortens the recovery process.
When it reaches the checkpoint, then the transaction will be updated into the database,
and till that point, the entire log file will be removed from the file. Then the log file is
updated with the new step of transaction till the next checkpoint and so on.
The checkpoint is used to declare the point before which the DBMS was in the
consistent state, and all the transactions were committed.
Restart recovery: