Open In App

Transaction Control in DBMS

Improve
Improve
Like Article
Like
Save
Share
Report

The transaction is a single logical unit that accesses and modifies the contents of the database. Transactions access data using read and write operations. Transaction is a single operation of processing that can have many operations. Transaction is needed when more than one user wants to access same database. Transaction has ACID properties.

 

Transaction state

 

ACID Properties of Transaction:

ACID stands for Atomicity, Consistency, Isolation and Durability

  1. Atomicity: All the operations in a transaction are considered to be atomic and as one unit. If system fails or any read/write conflicts occur during transaction the system needs to revert back to its previous state. Atomicity is maintained by the Transaction Management Component.
  2. Consistency: Every transaction should lead to database connection from one valid state to other valid state. If system fails because of invalid data while doing an operation revert back the system to its previous state. Consistency is maintained by the Application manager.
  3. Isolation: If multiple transactions are executing on single database, each transaction should be isolated from other transaction. If multiple transactions are performed on single database, operation from any transaction should not interfere with operation in other transaction. Isolation is maintained by the concurrency control manager. 
  4. Durability: Durability means the changes made during the transactions should exist after completion of transaction. Changes must be permanent and must not be lost due to any database failure. It is maintained by the recovery manager.
ACID

 

Example:

A has an account with an amount of Rs 150. B has an account with an amount of Rs 50. A is transferring amount Rs 100 to B’s account.

  1. Atomicity: Operations required for transfer are: Deduct amount Rs100 from A’s account. Add amount Rs 100 to B’s account. All operations should be done. If system fails to add amount in B’s account after deducting from A’s account, revert the operation on A’s account.
  2. Consistency: The sum amount in A’s account and B’s account should be same before and after the transaction completes. In the example the sum of both account before and after transaction is Rs 200, which preserves the consistency.
  3. Isolation: If there is any other transaction (let between A and C) is going on, it should not affect the transaction between A and B i.e., both the transactions should be isolated.
  4. Durability: It may happen system gets crashed after the completion of all operations then, after restarting it should preserve all the changes. The amount in A’s and B’s account should be same before and after the system restart.

Schedule:

Schedule is process of grouping transactions into one and executing them in a predefined order. It is a sequence of execution of operation from various transactions. Schedule is required in database because when multiple transactions execute in parallel, they may affect the result of each other. So, to resolve this the order of the transactions are changed by creating a schedule.

Types of Schedules:

  1. Serial Schedule: A schedule in which the transactions are defined to execute one after another is called serial schedule.
  2. Non- Serial Schedule: A schedule in which the transactions are defined to execute in any order is called non-serial schedule.

Recoverable and Non-Recoverable Schedule:

  • Schedules in which transactions commit only after all transactions whose changes they commit are called recoverable schedule.
  • If some transaction Tj is reading value updated or written by some other transaction Ti then, the commit of Tj must occur after the commit of Ti.
Schedule S1 Schedule S2
T1 T2 T1 T2

R(X)

X=X+10

W(X)

 

R(X)

X=X+10

W(X)

COMMIT

 

Rollback

COMMIT

R(X)

X=X-5

W(X)

COMMIT

 

R(X)

X=X-5

W(X)

COMMIT

Non-Recoverable Schedule Recoverable Schedule

Cascading Abort and Cascadeless Schedule:

  • If in a schedule, failure of one transaction causes rollback or abort to other dependent transaction such a schedule is called Cascading abort. It leads to wastage of CPU time.
  • If in a schedule, a transaction is not allowed to read a data item until the last transaction that has written it is committed or aborted, then such a schedule is called Cascadeless schedule.
Schedule S1 Schedule S2
T1 T2 T3 T1 T2 T3

(Cascading Rollback)

R(X)

W(X)

   

R(X)

W(X)

COMMIT

   
 

(Cascading Rollback)

R(X)

W(X)

   

R(X)

W(X)

COMMIT

 

If transaction fails⇢

COMMIT

 

(Cascading Rollback)

R(X)

W(X)

COMMIT

   

R(X)

W(X)

COMMIT

Cascading Abort Cascadeless Schedule

Strict Schedule:

  • If in a schedule, a transaction is neither allowed to read nor write a data item until the last transaction that has written it is committed or aborted, then such schedule is called strict schedule.
  • Strict schedule implements more restrictions than cascadeless schedule.
Schedule S
T1 T2

R(X)

W(X)

COMMIT

 
 

W(X)

R(X)

COMMIT

Strict Schedule

Transaction Control:

Following is the commands used for transaction control:

COMMIT: The COMMIT command is used to save changes made by the transaction in the database.

Syntax: 

COMMIT;

ROLLBACK: The ROLLBACK command is used to undo saved changes made by the transaction in the database.

Syntax:

ROLLBACK;

SAVEPOINT: SAVEPOINT is a point in the transaction where transaction can be rolled back without entire transaction rollback.

Syntax:

SAVEPOINT savepoint_name;

Concurrency Control:

Two basic concurrency control protocols:

  1. Lock Based Protocol: In this protocol the data item to be accessed is locked by the first transaction. After performing operations transaction unlocks the data item, so that it can be accessed by other transactions. When there is a lock in a data item other transactions can read it.
  2. Time Stamp Based Protocol: Time Stamp Based Protocol uses time stamp to serialize the execution of concurrent transactions. This protocol ensures that every conflicting read and write operations are executed in timestamp order. The protocol uses the system time as a timestamp.


Last Updated : 02 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads