Transaction & ACID Properties

In this tutorial, we will delve into Database Management System (DBMS) transactions and the ACID properties in detail, including examples to illustrate each concept.

1. What is a Transaction in DBMS?

A transaction in a DBMS is a unit of work that is treated as a single, indivisible entity. It can consist of one or more database operations (e.g., insert, update, delete), and these operations are either all executed successfully or none at all. Transactions are used to maintain the integrity and consistency of the database.

Let's consider an example of a simple banking transaction to illustrate the concept of a transaction in a DBMS. In this example, we'll perform a fund transfer from one bank account to another.

Transaction Description: A customer named Alice wants to transfer $500 from her checking account (Account A) to her savings account (Account B).

Here's how the transaction would look:

  1. Begin Transaction: The transaction starts.

  2. Debit from Account A: A deduction of $500 is made from Account A (checking account).

    SQL Query: UPDATE Accounts SET Balance = Balance - 500 WHERE AccountNumber = 'A';

    If this operation fails (e.g., due to insufficient funds), the entire transaction will be rolled back.

  3. Credit to Account B: A deposit of $500 is made into Account B (savings account).

    SQL Query: UPDATE Accounts SET Balance = Balance + 500 WHERE AccountNumber = 'B';

    If this operation fails for any reason, the entire transaction will be rolled back to maintain atomicity.

  4. Commit Transaction: If both the debit and credit operations were successful without errors, the transaction is committed.

    SQL Query: COMMIT;

    This step makes the changes permanent, ensuring durability.

If any part of the transaction fails (e.g., due to insufficient funds, database connection issues, etc.), the system will automatically roll back the entire transaction, leaving both accounts in their original state. This ensures that the database remains consistent and that the customer's funds are not lost in the event of an error.

Note : All the above 4 steps are part of a single transaction.

2. ACID Properties and Why we need them ?

To ensure integrity of the data, we require that the DB system maintain the following properties of the transaction

2.1. Atomicity

Atomicity means that a transaction is treated as a single, indivisible unit. It is an all-or-nothing concept, meaning that either all the operations within a transaction are successfully completed, or none of them are. If any part of the transaction fails, the entire transaction is rolled back, and the database returns to its original state.

Example: Consider a bank transfer where money is debited from one account and credited to another. If the debit succeeds but the credit fails, the entire transaction should be rolled back to prevent inconsistencies.

2.2. Consistency

Consistency ensures that a transaction takes the database from one consistent state to another. The database must satisfy a set of integrity constraints before and after the transaction. If a transaction violates these constraints, it is rolled back.

Example: If a database has a constraint that ensures that every order must have a valid customer, a transaction that tries to insert an order without a valid customer should fail.

2.3. Isolation

Isolation ensures that concurrent transactions do not interfere with each other. Transactions should execute as if they are the only transactions in the system. Isolation levels (e.g., Read Uncommitted, Read Committed, Repeatable Read, Serializable) define the degree to which transactions are isolated from each other.

Example: Two users simultaneously withdrawing money from the same account should not lead to inconsistencies or incorrect results.

2.4. Durability

Durability guarantees that once a transaction is committed, its changes to the database will persist, even in the face of system failures (e.g., power outage, hardware failure). The changes made by a committed transaction are permanent.

Example: If a transaction updates a customer's address, that address change should persist even if the database server crashes.

3. Transaction States

Transactions in a Database Management System (DBMS) typically go through different states during their lifecycle. Understanding these transaction states is crucial for managing and ensuring the integrity of data within a database. Here are the common transaction states:

  1. Active: In this initial state, the transaction is actively executing its operations. It has started but has not yet reached a point where it can be considered complete or ready for a commit.

  2. Partially Committed: Once all the operations within a transaction have been executed successfully and it's about to be finalized, it enters the partially committed state. In this state, the system ensures that the transaction can be committed without violating any integrity constraints. If all is well, it proceeds to the next state.

  3. Committed: In this state, the transaction has been successfully completed, and all its changes have been made permanent in the database. Once a transaction is committed, its changes are durable, meaning they will persist even in the event of a system failure.

  4. Rolled Back: If any part of a transaction encounters an error or if the transaction is explicitly rolled back by the user or the system, it enters the rolled-back state. In this state, any changes made by the transaction are undone, and the database is returned to its state before the transaction began.

  5. Failed: A transaction can enter the failed state when it encounters a severe error or a system crash during execution. In this state, it's neither committed nor rolled back. Recovery mechanisms are typically employed to decide how to handle failed transactions, either by rolling them back or completing them, depending on the circumstances.

  6. Aborted: Similar to the failed state, an aborted transaction is one that is terminated due to errors or external factors. Unlike the failed state, an aborted transaction is explicitly marked as such, and it's typically rolled back to maintain consistency.

  7. Terminated: This state represents the end of a transaction's lifecycle. A terminated transaction is neither active nor partially committed, but it may have been successfully committed or explicitly rolled back.

It's important to note that not all DBMS implementations use these exact terms or states, and some systems may have additional states or variations. However, the core concept of transitioning through states like active, partially committed, committed, rolled back, and possibly failed or aborted is common to most transaction management systems.

Managing these states effectively is crucial for ensuring the reliability, integrity, and consistency of a database, especially in systems with concurrent transactions or in scenarios where transactions may be interrupted due to errors or system failures.

4. Review Examples

Let's illustrate these concepts with some examples:

4.1. Atomicity Example

Suppose you're transferring money from one bank account to another. This is an example of a transaction:

  1. Debit $100 from Account A.

  2. Credit $100 to Account B.

If the debit is successful, but the credit fails (e.g., due to insufficient funds in Account B), the entire transaction should be rolled back to maintain atomicity.

4.2. Consistency Example

Consider an e-commerce database with an integrity constraint: every order must have a valid customer. If a transaction tries to insert an order without specifying a valid customer, it should be rolled back to maintain consistency.

4.3. Isolation Example

Imagine two users concurrently updating the quantity of a product in an inventory system. Isolation ensures that their transactions do not interfere with each other. For instance, if both users want to reduce the quantity by 5 units, isolation guarantees that the final quantity reflects the reduction from each transaction, and they do not overwrite each other's changes.

4.4. Durability Example

When a user updates their profile information in a social media platform and clicks "Save," the changes must be durable. Even if the platform experiences a system failure immediately after the update, the user's profile changes should still be intact when the system recovers.

5. Conclusion

Understanding transactions and the ACID properties is crucial for building reliable and robust database systems. By ensuring that transactions are atomic, maintain consistency, provide isolation, and guarantee durability, you can create applications that can handle complex operations while preserving data integrity.

Last updated

Was this helpful?