There is often a need to perform operations on data stored in a database. Do you know how a logical set of work is done and how transactions are managed in a database? If not, then don't worry.
In this article, we will discuss transaction management in databases, its operations, transaction states, rollback recovery, and serializability. Ultimately, we will discuss some use cases of transaction management and its disadvantages.
Transaction Management in DBMS
Transaction in DBMS refers to operations like insertion, updation, and deletion of data. This set of logical works requires one or more database operations. A transaction means that there is a change in the database.
For example, when we withdraw money from a bank account, there is an updation of money left in our account within the bank’s database. This can be referred to as a transaction where we are performing some logical set of operations to do updation or deletion in a database. These sets of operations are done as a single unit.
Transaction management in DBMS ensures that data is restored consistently when the computer restarts after a crash. After a system crash, restoring the data is essential for data recovery. The restoration uses checkpoints, transaction logs, and crash recovery methods. Therefore transaction management involves controlling and recording the transactions and performing the necessary rollback and recovery operations.
Facts about Database Transactions
Database transactions are like actions to perform with data, with four critical rules. ACID stands for atomicity, consistency, isolation, and durability to ensure everything works correctly.
A transaction usually involves various actions with data, such as adding, changing, or deleting. You can start and finish a transaction on purpose or happen automatically.
Transactions are essential because they keep data safe and organized when many people are using a database at the same time.
Some databases allow the control of transactions with special commands. These commands help to start, finish, or fix a transaction. You need to plan your transactions carefully to keep your work moving smoothly with data.
If something goes wrong during a transaction, it's like making a mistake in your work. Transactions help to fix the error or undo your work so everything stays right.
Modern databases give choices about how isolated or separate your work is from others. It's like choosing how much you share with others while you work.
Transaction States in DBMS
Transaction states in DBMS are the transaction stages from the beginning of the transaction to its completion or rollback. The transaction states are the multiple phases of the transaction during its lifetime. These states outline the transaction’s current situation and explain how it is handled.
There are a total of 6 transaction states in DBMS. The transaction states are active, partially committed state, committed, failed, aborted, and terminated state.
Active state: A transaction is called an active transaction when the operations are benign and made in that particular transaction.
Partially Committed and Committed state: In this state, the operations made in the main memory will be made permanent and then passed on to the committed state. And if, in the process, some failure is experienced, it goes into a failed state.
Aborted State: In case of transaction failure, it goes from failed state to aborted state. If we apply the rollback operation, the transaction goes to the state as it was in its beginning before applying the operations. Also, locks are released if the tractions use a lock on the data, and now other transactions can access the data.
Operations Involved in Transaction Management in DBMS
The operations of transactions in DBMS include READ, WRITE, Savepoint, Commit, and Rollback. Let's discuss these operations one by one.
READ: This operation selects relevant information and enables us to access and view the data’s current state. Therefore this operation reads the information from the database and stores the relevant data in a buffer in the main memory. Buffer refers to the memory in which data is stored temporarily in a portion of the main memory.
WRITE: The WRITE operation is used for writing the information back into the database from the buffer. It refers to the modification done to the information by insertion, updation, or deletion.
COMMIT: The COMMIT statement saves the changes made after the transaction. It is applied to successful transactions. The transaction returns to the previous star in case of system failure before applying a COMMIT. The operations before COMMIT are known to be in the partially committed state. These operations are stored in RAM (primary memory). After executing the COMMIT transaction, the updated data is accepted, and this updation is done in the secondary memory.
Rollback and Recovery in Transaction
The rollback and recovery go hand in hand in maintaining the consistency of the data. This mechanism helps ensure that the Database is consistent and not affected by system failures.
Transaction Recovery
The recovery information is usually divided into Undo (Rollback) operations and Redo (Cache Restore) operations. After any transaction fails, the Redo operations are used for recovering the content of the DMF (Data Manipulation Facility) cached data pages. The recovery process performs the redo recovery. Redo recovery is also known as forward recovery. The required changes are redone to bring the database to its consistent state. Therefore in a recovery process, the undo recovery is followed by a redo recovery.
A rollback refers to erasing the data modifications from the training of the transaction to the savepoint. In this process, the resources are made free, which were held by the transaction.
Therollback segmentis a data structure that stores the data during the previous transactions and the information needed for undoing the changes made by the particular transaction. They help maintain data consistency. The database system manages the rollback segments.
Rollback segments are used to undo the effects of uncommitted transactions that were applied by the rolling forward phase. So whenever a rollback is used, or there is an error or system failure during the transaction, the transaction returns to its initial state. In the rollback, the undo operations are used for reversing the operations made by the transactions. This is done by using thetransaction log, which determines all the modifications by the transaction.
We know that the transaction gets aborted if it fails before the COMMIT. Whenever the transaction enters the aborted state, it cannot continue from the state before its failure, and the uncommitted changes made by the transaction are rolled back. This maintains data consistency. And to continue the transaction, we need to initiate the transaction from starting and perform its execution.
A- ATOMICITY This Atomicity property states that the operation should not be performed partially or in segments. Rather, it should be executed entirely or not at all.
C- CONSISTENCY This property states that the value should be maintained by maintaining integrity while doing a transaction .
I- ISOLATION This property states that no data should be affected by another, and the changes occur concurrently.
D- DURABILITY This property refers to the data being permanent after executing the operation. This means that the data should stay the same even if there is a case of system failure.
Serializability is a crucial aspect of transaction management in DBMS. This is the way of checking whether consistency is begin maintained when two transactions are working on a database. It ensures that the transactions are being made concurrently and produce the expected sequential results. Serializability can be achieved using locking-based Concurrency Control or Multi-version Concurrency Contol (MVCC).
The locking-based concurrency control is a mechanism in which a transaction cannot read or write unless and until it acquires an appropriate lock. Lock refers to the variable that indicates those operations which can be executed on the particular item.
MCVV is a database optimization technique used for creating duplicate copies of records to read and update data simultaneously safely. MCVV reduces the number of database deadlocks and improves read access performance.
Conflict serializability is used in transaction management to ensure that after executing concurrent transactions. While View Serializability is used for checking the consistency of the given schedule.
Schedules in transaction management in DBMS
We know that at a time, one operation can be performed in a database, so when multiple transactions run concurrently, there is a requirement for a sequence for the operations to be performed. This sequence of operations in a database is known as schedule, and the process is called scheduling.
Mismanagement of the execution of operations may lead to issues often known as concurrency problems. Therefore scheduling is a crucial requirement.
There are two types of scheduling Serial and Non-serial scheduling.
Serial Scheduling
In serial scheduling, the transactions are scheduled in a serial manner which means one after another. A transaction will only execute after one transaction has completed its execution.
Non-serial scheduling In this type of scheduling, multiple transactions’ execution takes place simultaneously. A transaction does not wait for the other transaction to complete its execution instead proceeds.
Some of the uses of transaction management are mentioned below.
Transactions are used for managing concurrency as the DBMS schedules the access of data concurrently, which means that a user can access various data from the database without experiencing any interruption. An example of a concurrency control mechanism used in transaction management is locking.
It is used in Concurrency control protocols. Concurrency control is required to maintain consistent data. Concurrency means the execution of multiple transactions at a time by increasing time efficiency. Inconsistency may arise when multiple transactions try to access the same data.
It is used in the locking of data. Locks are used in transactions to control concurrent access, this data consistency. Recommended article, Transactions, and concurrency control
Transaction management resolves the read/write conflicts.
Transaction management ensures a transaction's ACID properties, allowing developers to handle concurrency and failures.
Transaction management is used in the implementation of recoverability, serializability, and cascading.
Disadvantages of Using a Transaction
Below are some of the disadvantages of transactions in a database.
A transaction can be pretty complex to understand and implement.
An incomplete transaction may result in an inconsistent state.
Managing a transaction can prove to be an expensive task considering its maintenance and hardware and software requirement.
Changing the information within the transaction can prove to be a difficult task. Also read -Aggregation in DBMS
Frequently Asked Questions
What are the uses of transaction management in DBMS?
Transactions are used for managing concurrency as the DBMS schedules the access of data concurrently, which means that a user can access various data from the database without experiencing any interruption. It is used in Concurrency control protocols for locking data and for resolving read/write conflicts.
What are the transaction states in DBMS?
Transaction states in DBMS are the transaction stages from the beginning of the transaction to its completion or rollback. The transaction states are the multiple phases of the transaction during its lifetime. These states outline the transaction’s current situation and explain how it is handled. There are a total of 6 transaction states in dbms. The transaction states are active, partially committed state, committed, failed, aborted, and terminated state.
What is transaction management in DBMS?
Transaction management in DBMS ensures that data is restored consistently when the computer restarts after a crash. After a system crash, restoring the data is essential for data recovery. The restoration uses checkpoints, transaction logs, and crash recovery methods. Therefore transaction management involves controlling and recording the transactions and performing the necessary rollback and recovery operations.
In this article, we have discussed about transaction management in databases, its operations, transaction states, rollback recovery, and serializability. Ultimately, we have discussed some use cases of transaction management and its disadvantages. You can read more such articles on our platform, Coding Ninjas Studio.
You will find straightforward explanations of almost every topic on this platform. So take your coding journey to the next level using Coding Ninjas. You can also consider our DBMS Course to give your career an edge over others. Happy coding!