transaction in DBMS
DBMS DBMS Tutorials

Transaction in DBMS

Transaction in DBMS

  • Transaction in DBMS is a logical unit of operations to be performed on Data stored in database.
  • In this tutorial we will learn about basics of transaction, advantages and disadvantages of transaction.

What is a Transaction in DBMS ?

  • Transaction in DBMS is a series of instructions followed by a single user or a set of users or application program which will read, write or update the contents of a Database.
  • A single transaction in DBMS is a logical unit of work which may be an entire program or a part of a program. It may be a single command or may involve a large number of operations.
  • A straightforward application of transaction would be while buying online tickets. When a customer buys online tickets, while the customer is filling up the form, the transaction processing system is holding the ticket so that no one else can buy the same ticket.

Let’s consider we have a set of instructions, and these instructions together are called a Program.

We need to transfer money from person A’s account to person B’s account. We need to execute a set of instructions to perform that task.

  • First, we read account A, then debit account A by 100 Rupees, then write the existing amount to A, then increment 100 rupees to B, then update account B. We can write that simply as transfer an amount of 100 from Account A to Account B.
  • If any logical instruction from this set of instructions fails, then the program will not be executed, and the transaction would not occur. When a transaction is committed, the database reaches a new Consistent State.
  • To make a transaction take place, the set of instructions much follow some properties.
  • These properties are known as acid properties atomicity consistency isolation and durability of a transaction in DBMS, which are discussed in brief here

Advantages of Transaction in DBMS

Some advantages of transaction in DBMS are as follow-

  • Transaction in DBMS are really fast and usually take only a few seconds. However, if a lot of processes are in the queue, it may take longer.
  • Transaction Processing System makes online booking fair as the processes are executed in the order in which they have been queued.
  • Batch or Real-Time Processing is available.
  • Increase in productivity and customer satisfaction.

Disadvantages of Transaction in DBMS

  • High setup costs.
  • Lack of standard formats.
  • Transaction Processing Systems need to use direct access files; magnetic tapes can not be used.

Examples of Transaction in DBMS

An example of a simple transaction is given below

Suppose a person A transfers Rs 500 from his account to another person B’s account then this complete process can be seems as a transaction consisting the following sequence of logical operations

A’s Account

OpenAccount(A)

AOld_Balance = A.balance
ANewBalance = OldBalance – 500
A.balance = ANewBalance
CloseAccount(A)

B’s Account

OpenAccount(B)
BOld_Balance = B.balance
BNewBalance = BOldBalance + 500
B.balance = NewBBalance
Close_Account(B)

Application of Transaction Processing System

Some Real-world application od transaction in DBMS are as follow

  • Automatic Teller Machines (ATM).
  • Credit card authorizations.
  • Online bill payments.
  • Self-checkout systems.
  • Trading stocks over the internet.
  • Other forms of electronic commerce.

Transaction Failure in Between the Operation

  • Now we understood that what is transaction, we should also understand what may be problems associated with it.
  • Main problem with a transaction that may occur is that a transaction may be fail before finishing all its operations.
  • Transaction failure may occur due to many reasons such as hardware failure , power failure, system crash etc.
  • Sometime this is problem may leave database in an inconsistent state.
  • Assume that in above example if transaction fail after debit the amount from account A but before credit to account B in this case amount is not credited to account B.

To solve this problem, we have the following two operations

Commit a Transaction :  If all the operations in a transaction are completed successfully then commit those changes to the database permanently.

Rollback a Transaction : If any of the operation fails then rollback all the changes done by previous operations.

Conclusion and Summary

We have explained the transaction introduction, advantages of transaction and disadvantages of transaction here in this tutorial.

Transaction Failure reasons and solution is also discussed.

Next Tutorial – Transaction State Diagram

Leave a Reply

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