Transaction Management in DBMS
Transaction Management in DBMS
For example, you are transferring money from your bank account to your friend’s
account, the set of operations would be like this:
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.
To solve this problem, we have the following two operations
Rollback: If any of the operation fails then rollback all the changes done by
previous operations.
Facts about Database Transactions
A transaction is a program unit whose execution may or may not change the
contents of a database.
The transaction is executed as a single unit.
If the database operations do not update the database but only retrieve data, this
type of transaction is called a read-only transaction.
A successful transaction can change the database from one CONSISTENT
STATE to another
DBMS transactions must be atomic, consistent, isolated and durable.
States of Transactions
Atomicity
By this, we mean that either the entire transaction takes place at once or doesn’t happen
at all. There is no midway i.e. transactions do not occur partially. Each transaction is
considered as one unit and either runs to completion or is not executed at all. It involves
the following two operations.
—Abort: If a transaction aborts, changes made to database are not visible.
—Commit: If a transaction commits, changes made are visible.
Atomicity is also known as the ‘All or nothing rule’.
Consider the following transaction T consisting of T1 and T2: Transfer of 100 from
account X to account Y.
If the transaction fails after completion of T1 but before completion of T2.( say,
after write(X) but before write(Y)), then amount has been deducted from X but not
added to Y. This results in an inconsistent database state. Therefore, the transaction must
be executed in entirety in order to ensure correctness of database state.
Consistency
This means that integrity constraints must be maintained so that the database is consistent
before and after the transaction. It refers to the correctness of a database.
Isolation
This property ensures that multiple transactions can occur concurrently without leading to
the inconsistency of database state. Transactions occur independently without
interference. Changes occurring in a particular transaction will not be visible to any other
transaction until that particular change in that transaction is written to memory or has
been committed. This property ensures that the execution of transactions concurrently
will result in a state that is equivalent to a state achieved these were executed serially in
some order.
Durability:
This property ensures that once the transaction has completed execution, the
updates and modifications to the database are stored in and written to disk and they
persist even if a system failure occurs. These updates now become permanent and
are stored in non-volatile memory. The effects of the transaction, thus, are never
lost.
The ACID properties, in totality, provide a mechanism to ensure correctness and
consistency of a database in a way such that each transaction is a group of
operations that acts a single unit, produces consistent results, acts in isolation from
other operations and updates that it makes are durably stored.
What is a Schedule?
A schedule is a series of operations from one or more transactions. A schedule can
be of two types:
Serial Schedule: When one transaction completely executes before starting
another transaction, the schedule is called serial schedule. A serial schedule is
always consistent. e.g.; If a schedule S has debit transaction T1 and credit
transaction T2, possible serial schedules are T1 followed by T2 (T1->T2) or
T2 followed by T1 ((T2->T1). A serial schedule has low throughput and less
resource utilization.
Concurrent Schedule: When operations of a transaction are interleaved with
operations of other transactions of a schedule, the schedule is called
Concurrent schedule. But concurrency can lead to inconsistency in the
database.