Transaction

What is transaction ?

transaction

In a database management system (DBMS), a transaction is a sequence of one or more operations (such as read, write, update, or delete) executed as a single logical unit of work.

In databases,a transaction is a unit of program execution that accesses and possibly updates various data items.

The transaction consists of all operations executed between the begin transaction and end transaction.

Begain Transaction

Begin Transaction marks the start of a new transaction. This indicates to the DBMS that all subsequent operations should be treated as a single, atomic unit of work.

End Transaction

End Transaction can mean either committing the transaction or rolling it back, depending on the outcome of the operations within the transaction block.

ok

Properties a transaction should possess

acid

Atomicity:

Atomicity is one of the core properties of database transactions, encapsulated within the ACID (Atomicity, Consistency, Isolation, Durability) principles that govern transaction management in database systems.

Atomicity ensures that a series of database operations within a transaction are treated as a single, indivisible unit of work. This means that either all operations in the transaction are executed successfully, or none of them are.

Consistency:

Consistency is one of the core properties of database transactions, represented by the “C” in the ACID (Atomicity, Consistency, Isolation, Durability) principles.

In Database Management System (DBMS), consistency ensures that a transaction brings the database from one valid state to another valid state, maintaining the database’s defined rules and constraints.

Isolation:

Isolation is one of the key properties of database transactions, encapsulated within the ACID (Atomicity, Consistency, Isolation, Durability) principles.

In Database Management System (DBMS), isolation ensures that the operations of one transaction are hidden from other transactions until the transaction is committed, thereby preventing concurrent transactions from interfering with each other.

Durability:

Durability property of DBMS is a important property if database that ensures that once a transaction is committed, its changes are permanent and will survive any system failures, such as crashes or power outages.

It guarantees that once data is written to the database, it will persist even in the event of hardware failures or software crashes.

Lets imagine and understand what is actually happining

Consider a transaction Ti that transfers $50 from account A to account B.

The initial value of accounts:

Account A is = $1000 Account B is = $2000

Steps involved are:

  1. Transaction Ti reads the current balance of account A, which is $1000.
  2. Transaction Ti subtracts $50 from the balance of account A.
  3. After this operation, the balance of account A becomes $950.
  4. Transaction Ti writes the new balance of account A, which is $950, back to the database.
  5. Transaction Ti reads the current balance of account B, which is $2000.
  6. Transaction Ti adds $50 to the balance of account B.
  7. After this operation, the balance of account B becomes $2050.
  8. Transaction Ti writes the new balance of account B, which is $2050, back to the database.

Pactical Implementation of transaction in Posatgresql

Steps Included:

  1. Lets create a database called test_transaction

    alt text

  2. Lets create a new table call accounts

    alt text

  3. Inserting values inside the table accounts

    alt text

  4. OK! Now lets begain the transaction

    alt text

  5. Start Transaction

    alt text

    oh

  6. From the accounts A and B, we will update the balance by either deducting or adding to the previous value.

    Initial alt text

    After updating alt text

  7. Commiting the changes made in both the accounts and after commiting the changes we cannot perform ROLL BACK operation or undo the changes.

    alt text

Storage Structure in DB

Storage media categorized based on speed, capacity, and resilience:

  1. Volatile storage
  2. Non-volatile storage
  3. Stable storage

1. Volatile storage

2. Non-volatile storage

3. Stable Storage

For a transaction to be durable:

For a transaction to be atomic:

Simple transaction state model

To understand the simple transaction model lets perform some transactions

  1. Active State
  1. Partially Committed state
  1. Failed to Aborted
  1. Active to Aborted
  1. Partially Committed to Failed

Transaction Atomicity and Durability

When a transaction fails and can’t continue, it must be undone and marked as aborted. Then, the system has two choices:

Transaction Isolation and Concurrency

Reasons for Concurrency:

Problem:

Concurrent transactions might interfere with each other, risking data consistency.

Solution:

Ensuring Consistency:

Serializability

Key Concepts

Ensuring Consistency

Isolation Levels

Implementation Methods

Examples