Open In App

Transaction Management

Transactions are a set of operations used to perform a logical set of work. It is the bundle of all the instructions of a logical operation. A transaction usually means that the data in the database has changed. One of the major uses of DBMS is to protect the user’s data from system failures. It is done by ensuring that all the data is restored to a consistent state when the computer is restarted after a crash. The transaction is any one execution of the user program in a DBMS. One of the important properties of the transaction is that it contains a finite number of steps. Executing the same program multiple times will generate multiple transactions. 

Example: Consider the following example of transaction operations to be performed to withdraw cash from an ATM vestibule.



 Steps for ATM Transaction 

  1.  Transaction Start.
  2.  Insert your ATM card.
  3.  Select a language for your transaction.
  4.  Select the Savings Account option. 
  5.  Enter the amount you want to withdraw. 
  6.  Enter your secret pin.
  7.  Wait for some time for processing.
  8.  Collect your Cash.
  9.  Transaction Completed.

A transaction can include the following basic database access operation.

Example: Transfer of 50₹ from Account A to Account B. Initially A= 500₹, B= 800₹. This data is brought to RAM from Hard Disk. 



R(A) -- 500       // Accessed from RAM.
A = A-50 // Deducting 50₹ from A.
W(A)--450 // Updated in RAM.
R(B) -- 800 // Accessed from RAM.
B=B+50 // 50₹ is added to B's Account.
W(B) --850 // Updated in RAM.
commit // The data in RAM is taken back to Hard Disk.

Stages of Transaction

Note: The updated value of Account A = 450₹ and Account B = 850₹. 

All instructions before committing come under a partially committed state and are stored in RAM. When the commit is read the data is fully accepted and is stored on a Hard Disk. 

If the transaction is failed anywhere before committing we have to go back and start from the beginning. We can’t continue from the same state. This is known as Roll Back

Desirable Properties of Transaction (ACID Properties)

For a transaction to be performed in DBMS, it must possess several properties often called ACID properties.

Transaction States

Transactions can be implemented using SQL queries and Servers. In the diagram, you can see how transaction states work. 

Transaction States

The transaction has four properties. These are used to maintain consistency in a database, before and after the transaction.
Property of Transaction:

Atomicity

Consistency

Isolation

Durability

Implementing of Atomicity and Durability

The recovery-management component of a database system can support atomicity and durability by a variety of schemes.

E.g. the shadow-database scheme:

Shadow copy

Transaction Isolation Levels in DBMS

Some other transaction may also have used value produced by the failed transaction. So we also have to rollback those transactions.
The SQL standard defines four isolation levels:

Failure Classification

To find that where the problem has occurred, we generalize a failure into the following categories:

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 transactions or process is hurt, then this is called as transaction failure.

Reasons for a transaction failure could be –

  1. Logical errors: If a transaction cannot complete due to some code error or an internal error condition, then the logical error occurs.
  2. Syntax error: It occurs where the DBMS itself terminates an active transaction because the database system is not able to execute it. For example, The system aborts an active transaction, in case of deadlock or resource unavailability.

2. System Crash

System failure can occur due to power failure or other hardware or software failure. Example: Operating system error.

3. Disk Failure

Serializability

It is an important aspect of Transactions. In simple meaning, you can say that serializability is a way to check whether two transactions working on a database are maintaining database consistency or not.

It is of two types:

  1. Conflict Serializability
  2. View Serializability

Schedule

Schedule, as the name suggests is a process of lining the transactions and executing them one by one. When there are multiple transactions that are running in a concurrent manner and the order of operation is needed to be set so that the operations do not overlap each other, Scheduling is brought into play and the transactions are timed accordingly.

It is of two types:

  1. Serial Schedule
  2. Non-Serial Schedule

Uses of Transaction Management

Advantages of using a Transaction

Disadvantages of using a Transaction

Conclusion

In DBMSs, transaction management is crucial to preserving data integrity. To guarantee dependable operations, it upholds the ACID (Atomicity, Consistency, Isolation, Durability) qualities. A key component of reliable database systems, transactions enable the grouping of several processes into a single unit while providing data consistency and security against concurrent access.


Article Tags :