1. Transaction:
⦁ In a database management system (DBMS), a transaction is a unit of work that involves one or more database operations, such as inserting, updating, or deleting records.
⦁ A transaction is a logical and consistent set of actions that must be completed as a whole or not at all, to maintain the integrity and consistency of the database.
⦁ In other words, a transaction is a sequence of database operations that must be treated as a single, indivisible unit of work.
⦁ If any part of the transaction fails, then the entire transaction must be rolled back, so that the database is returned to its original state before the transaction began.
2. States of Transaction:
****
In a database, the transaction can be in one of the following states –
1. Active State
⦁ The active state is the first state of every transaction. In this state, the transaction is being executed. ⦁ For example: Insertion or deletion or updating a record is done here. But all the records are still not saved to the database. ⦁ If all the ‘read and write’ operations are performed without any error then it goes to the “partially committed state”; if any instruction fails, it goes to the “failed state”.
2. Partially committed
⦁ In the partially committed state, a transaction executes its final operation, but the data is still not saved to the database ⦁ After completion of all the read and write operation the changes are made in main memory or local buffer. ⦁ If the changes are made permanent on the DataBase then the state will change to “committed state” and in case of failure it will go to the “failed state”.
3. Failed State
⦁ When any instruction of the transaction fails, it goes to the “failed state” or if failure occurs in making a permanent change of data on Data Base. ⦁ In the example of total mark calculation, if the database is not able to fire a query to fetch the marks, then the transaction will fail to execute.
4. Aborted State
⦁ After having any type of failure the transaction goes from “failed state” to “aborted state” and since in previous states, ⦁ The changes are only made to local buffer or main memory and hence these changes are deleted or rolled-back.
5. Committed State
It is the state when the changes are made permanent on the Data Base and the transaction is complete and therefore terminated in the “terminated state”.
6. Terminated State
If there isn’t any roll-back or the transaction comes from the “committed state”, then the system is consistent and ready for new transaction and the old transaction is terminated. An example of a transaction state in a DBMS
Suppose a user wants to transfer $100 from their checking account to their savings account. The transaction might involve the following steps:
⦁ Begin Transaction: The transaction starts, and a lock is acquired on the checking and savings accounts to prevent other transactions from modifying them.
⦁ Debit the Checking Account: The amount is debited from the checking account, and the updated balance is recorded in the transaction log.
⦁ Credit the Savings Account: The amount is credited to the savings account, and the updated balance is recorded in the transaction log.
⦁ Commit Transaction: The transaction is committed, and the locks on the checking and savings accounts are released, allowing other transactions to access them.
If all of these steps are completed successfully, the transaction is said to be in a committed state. However, if any step fails (e.g., due to insufficient funds in the checking account), the transaction will be rolled back to its previous state (i.e., the debit to the checking account will be undone), and the transaction will be in an aborted state.