Transactions in 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.
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:
So, in the same way, we have three steps in the DBMS for a transaction which are the following:
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.
During the complete process of a transaction, there are a lot of states which are described below:
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.
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.
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.
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.
If the transaction is in the aborted state(failure) or committed state(success), then the execution stops, and it is called the terminated state.
Properties of Transaction
There are four properties of a transaction that should be maintained during the transaction.
It means either a transaction will take place, or it will fail. There will not be any middle state like partial completion.
The database should be consistent before and after the transaction. Correctness and integrity constraints should be maintained during the transaction.
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.
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.