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 March 20, 2023 by Prepbytes

We deal with data all the time and often need to make some changes to the data. Transactions in DBMS are nothing but changes we made in the data. There are many scenarios when you are making a transaction and suddenly the software hangs or any other issue arises and when you restart the software the transaction is where you left it this is because of various methods that protect the transaction in DBMS. While moving further in this article we will learn about the transaction in DBMS, followed by various states and properties of a transaction in DBMS.

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.

Transaction in DBMS is a very crucial component as they help to ensure data consistency and integrity by following the ACID properties. The transaction in DBMS occurs in various states after every successful or unsuccessful operation the state of the transaction is decided and after the completion of a transaction and when the changes made by the transactions are saved in the database then we can move to a new transaction although DBMS also supports to run multiple transactions at the same time.

Frequently Asked Questions

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

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

2. Why is transaction logging important?
Transaction logging is important for recovery management to restore the database to a consistent state after a failure.

3. What is a transaction processing system (TPS)?
A transaction processing system (TPS) is a computerized system that manages the execution of transactions.

4. What is a front-end application in a TPS?
A front-end application is responsible for interacting with users and generating transaction requests.

5. What is transaction initiation in a DBMS?
Transaction initiation is when a user requests a database operation, such as an insert, update, or delete.

Leave a Reply

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