Transaction
- A transaction can be defined as a group of tasks.
- A single task is the minimum processing unit which cannot be divided further.
- A transaction is an action or series of actions that are being performed by a single user or application program, which reads or updates the contents of the database.
- A transaction is a logical unit of work of database processing that includes one or more database access operations.
- The main operations in a transaction are-
- Read Operation
- Read operation reads the data from the database and then stores it in the buffer in main memory.
- For example- Read(A) instruction will read the value of A from the database and will store it in the buffer in the main memory.
- Write Operation
- Write operation writes the updated data value back to the database from the buffer.
- For example- Write(A) will write the updated value of A from the buffer to the database.
- A transaction can include the following operations:
- Retrieve: To retrieve data stored ina database.
- Insert: To store new data in the database.
- Delete: To delete existing data from the database.
- Update: To modify existing data in the database.
- Commit: To save the work done permanently.
- Rollback: To undo the work done.
- Example:
- Suppose a bank employee transfers Rs 500 from A's account to B's account.
- The steps are...
- A’s Account
- Open_Account(A)
- Old_Balance = A.balance
- New_Balance = Old_Balance - 500
- A.balance = New_Balance
- Close_Account(A)
- B’s Account
- Open_Account(B)
- Old_Balance = B.balance
- New_Balance = Old_Balance + 500
- B.balance = New_Balance
- Close_Account(B)
- States of Transactions:
- Active −
- In this state, the transaction is being executed.
- This is the initial state of every transaction.
- Failed −
- A transaction is said to be in a failed state if any of the checks made by the database recovery system fails.
- A failed transaction can no longer proceed further.
- Kill the transaction
- Re-start the transaction
- Committed −
- If a transaction executes all its operations successfully, it is said to be committed.
- All its effects are now permanently established on the database system.
- Aborted −
- If any of the checks fail and the transaction has reached a failed state, then the recovery manager rolls back all its write operations on the database to bring the database back to its original state where it was prior to the execution of the transaction.
- Transactions in this state are called aborted.
- The database recovery module can select one of the two operations after a transaction aborts.
- Partially Committed −
- When a transaction executes its final operation, it is said to be in a partially committed state.
Tags:
DBMS