Transactions in DBMS

DBMS

DBMS stands for Database Management System, which is a tool or software that is used for the creation, deletion, or manipulation of the current database.

Transaction

Any logical work or set of works that are done on the data of a database is known as a transaction. Logical work can be inserting a new value in the current database, deleting existing values, or updating the current values in the database.

For example, adding a new member to the database of a team is a transaction.

To complete a transaction, we have to follow some steps which make a transaction successful. For example, we withdraw the cash from ATM is an example of a transaction, and it can be done in the following steps:

  • Initialization if transaction
  • Inserting the ATM card into the machine
  • Choosing the language
  • Choosing the account type
  • Entering the cash amount
  • Entering the pin
  • Collecting the cash
  • Aborting the transaction

So, in the same way, we have three steps in the DBMS for a transaction which are the following:

  • Read Data
  • Write Data
  • Commit

We can understand the above three states by an example. Let suppose we have two accounts, account1, and account2, with an initial amount as 1000Rs. each. If we want to transfer Rs.500 from account1 to account2, then we will commit the transaction.

  • All the account details are in secondary memory so that they will be brought into primary memory for the transaction.
  • Now we will read the data of account1 and deduct the Rs.500 from the account1. Now, account1 contains Rs.500.
  • Now we will read the data of the account2 and add Rs.500 to it. Now, account2 will have Rs.1500.
  • In the end, we will use the commit command, which indicates that the transaction has been successful, and we can store the changes in secondary memory.
  • If, in any case, there is a failure before the commit command, then the system will be back into its previous state, and no changes will be there.

During the complete process of a transaction, there are a lot of states which are described below:

Active State:

When the transaction is going well without any error, then this is called an active state. If all the operations are good, then it goes to a partially committed state, and if it fails, then it enters into a failed state.

Partially Committed State:

All the changes in the database after the read and write operation needs to be reflected in permanent memory or database. So, a partially committed state system enters into a committed state for the permanent changes, and if there is any error, then it enters into a failed state.

Failed State:

If there is any error in hardware or software which makes the system fail, then it enters into the failed state. In the failed state, all the changes are discarded, and the system gets its previous state which was consistent.

Aborted State:

If there is any failure during the execution, then the system goes from failed to an aborted state. From an aborted state, the transaction will start its execution from a fresh start or from a newly active state.

Committed State:

If the execution of a transaction is successful, the changes are made into the main memory and stored in the database permanently, which is called the committed state.

Terminated State:

If the transaction is in the aborted state(failure) or committed state(success), then the execution stops, and it is called the terminated state.

Transactions in DBMS

Properties of Transaction

There are four properties of a transaction that should be maintained during the transaction.

  • Atomicity:

It means either a transaction will take place, or it will fail. There will not be any middle state like partial completion.

  • Consistency:

The database should be consistent before and after the transaction. Correctness and integrity constraints should be maintained during the transaction.

  • Isolation:

This property means multiple transactions can occur at the same time without affecting each other. If one transaction is occurring, then it should not bring any changes in the data for the other transaction, which is occurring concurrently.

  • Durability:

It means if there is a successful transaction, then all changes should be permanent, so if there is any system failure, we will be able to retrieve the updated data.


Next TopicTuple in DBMS




Latest Courses