Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Transaction in DBMS and Examples

Last Updated on December 26, 2023 by Ankit Kochar

Transactions in Database Management Systems (DBMS) are crucial for maintaining data integrity and ensuring reliable operations within databases. A transaction is a sequence of operations (read, write, update, etc.) performed on a database that must be executed as a single unit. It follows the ACID (Atomicity, Consistency, Isolation, Durability) properties to guarantee reliability, accuracy, and consistency of the data.

In this article, we’ll delve into the fundamentals of transactions in DBMS, exploring their importance, characteristics, and examples. Understanding transactions is pivotal for developers, database administrators, and anyone working with databases to ensure robustness and reliability in handling data operations.

What is a Transaction in DBMS?

Transaction in DBMS refers to the sequence of operations we perform on the database that is treated as a single unit of work. The transaction in DBMS can consist of any operation like update, delete, and insert. DBMS has many uses one of the major use is to protect the user data in case of any system failure whether hardware failure or software failure. The transaction starts from the execution of the first operation and it ends with the execution of the last operation.
The user data is preserved by storing the data in a consistent state. When you execute the same program multiple times it will result in multiple transactions.

Example of Transaction in DBMS

Let’s understand transaction in DBMS with the help of an example. We will discuss the standard example of an ATM as you can use the ATM machine easily and will be within everyone’s reach so that you can understand the transactions clearly.
The steps shown below are the operations of the transaction.

  • Transaction is started
  • Insert your ATM card.
  • Select the language according to your preference.
  • Select the option of savings account.
  • Enter the amount you need to withdraw.
  • Enter your pin secretly.
  • Wait for the processing time.
  • Collect the cash from the machine.
  • Transaction completed.

In the above example, these all steps are done by you but in transaction in DBMS there are mainly three main operations.

  • Read
  • Write
  • Commit

Let’s understand all the above operations with the help of the example.

We have to transfer rs 100 from account A to account B. INitally account has rs 600 and B has rs 900.

R(A) -- 600     // Accessed from the RAM.
A = A-100        // Deducting rs 100 from A.
W(A)--500       // Updated in RAM.
R(B) -- 900     // Accessed from RAM.
B=B+100          // 100â‚ą is added to B's Account.
W(B) --1000      // Updated in RAM.
commit          // The data in RAM is taken back to Hard Disk.

In the above example, we have performed the transaction where we have added rs 100 from A’s account to B’s account.
The transaction is saved after the commit statement so if the system fails the transaction starts from the last commit statement. So it is advised to commit the transactions after their completion.

Uses of Transaction Management

There are many uses of transaction management and some of them are mentioned below:

  • With the help of transaction management we can access the data concurrently.
  • They are also used to satisfy the ACID properties.
  • It can be used to solve Read/Write conflicts.
  • It is used for locking protocol and concurrency control protocols.
  • We can use it to implement cascading, recoverability, and serializability.

ACID Properties of a Transaction

To ensure data integrity and consistency, transactions in DBMS are required to follow the ACID properties. ACID stands for Atomicity, Consistency, Isolation, and Durability.

  • Atomicity
    We can refer to atomicity as the indivisibility of the transaction. It means whether the transaction happens completely or does not happen at all. We will consider the transaction as a single unit of work and it either succeeds or fails as a whole. If the transaction fails in between the changes made during the transaction must e undone and the database will return to the original state.

  • Consistency
    In simple words consistency refers to the correctness of the database. It is referred to as the state of the database before the transaction and after the transaction. The database must be consistent before, during, and after the execution of the transaction.

  • Isolation
    Isolation refers to the property with the help of which multiple transactions can occur concurrently. When many transactions are occurring concurrently the changes made in one transaction does not disturb or be applicable in other transaction. They all will execute like they are the only transaction in the system.

  • Durability
    It means when the transaction has been completely executed the changes made by it should be written properly and must remain in the database even after the system failure. The changes are stored in nonvolatile memory so that they can be stored permanently.

Transaction States

Transaction in DBMS consists of various states as it is a multiple state process.
A transaction in a DBMS can be in one of the following states:

  • Active
    A transaction is said to be in an active state when its operations are ongoing. It advances to the partly committed state if all read and write operations are completed without any errors, but if any operation fails for any reason, it enters a failed state.

  • Partially Committed
    The modifications that were previously made in the main memory are now permanently made in the database after all read and write operations are finished. Following this, the state will advance to the committed state, but in the event of failure, it will go to the failed state.

  • Committed
    When the transaction completes all the operations successfully, all the changes made by the transaction in the partially committed state are stored permanently then the transaction is said to be complete. After this, the transaction can move to the terminated state.

  • Failed
    If any operation in the transaction fails due to software or hardware issue then it goes to the failed state. The occurrence of a failure makes a permanent change in the database and the changes made in the local memory are rolled back in case of failure.

  • Aborted
    During execution if the transaction fails then it goes from the failed state to the aborted state. The changes made during these transactions are rolled back as they are not permanently saved and the transaction will start from the last active state.

  • Terminated
    If the transaction comes from the committed state or gets aborted after the rollback, then the transaction comes to a consistent state from where the new transaction can get started.

Conclusion
Transactions serve as the backbone of data management in DBMS, providing a framework for reliable and consistent database operations. Maintaining ACID properties ensures that transactions execute accurately, remain isolated from each other, and persist despite system failures. Examples like online banking transactions, inventory management in e-commerce platforms, and airline reservation systems illustrate the real-world significance of transactions.

By comprehending the essence of transactions and their adherence to ACID principles, database practitioners can build resilient systems, maintain data integrity, and ensure the reliability of their applications in handling critical data operations.

Frequently Asked Questions Related to Transaction in DBMS

Here are some of the frequently asked questions about transactions in DBMS.

1. What are the different recovery management techniques?
The different recovery management techniques are checkpointing, journaling, and shadow paging.

2. Why are ACID properties important in transactions?
ACID properties—Atomicity, Consistency, Isolation, and Durability—are crucial for maintaining the reliability and integrity of transactions. Atomicity ensures that transactions are treated as a single unit; Consistency ensures that the database remains in a valid state before and after the transaction; Isolation ensures that transactions are executed independently without interference; Durability ensures that the changes made by committed transactions persist even after system failures.

3. What are some real-world examples of transactions in DBMS?
Numerous real-world applications rely on transactions in DBMS. For instance, online banking transactions involving fund transfers, e-commerce platforms managing inventory, airline reservation systems handling seat bookings—all these operations involve multiple database actions executed as transactions to ensure data accuracy and reliability.

4. How does a DBMS ensure the durability of transactions?
DBMS ensures transaction durability by using techniques like write-ahead logging (WAL) or maintaining transaction logs. Changes made by committed transactions are recorded in logs before they are written to disk. In case of system failure, these logs are used to recover committed transactions and ensure that changes are durably stored.

5. What happens if a transaction fails to complete in a DBMS?
If a transaction fails to complete (due to errors, system crashes, etc.) in a DBMS, the system employs rollback mechanisms to undo the changes made by the incomplete transaction, ensuring that the database remains consistent and in a valid state.

Leave a Reply

Your email address will not be published. Required fields are marked *