Lecture on Transactions

Jul 1, 2024

Lecture on Transactions

Definition of Transaction

  • A set of operations used to perform a logical unit of work.
  • Commonly associated with monetary transactions (give and take money, withdrawing, debiting, or crediting).
  • Transactions can also involve any change or reading of a database.

Example of a Transaction: ATM Withdrawal

  • Task: Withdraw money
  • Operations:
    1. Insert card
    2. Read information (balance, authenticity)
    3. Choose language
    4. Select account type (savings/current)
    5. Enter amount
    6. Enter PIN
    7. Dispense money
    8. Transaction completion message
  • These steps form a transaction.

Example of a Transaction: Online Fund Transfer

  • Operations:
    1. Authenticate (username and password)
    2. Select/Add beneficiary
    3. Enter amount
    4. Receive and enter OTP
    5. Enter transaction password
    6. Transaction completion message

Transactions in Databases

  • Transactions usually represent changes in a database.
  • Banking systems use databases to store account holder details, money, etc.
  • When transactions occur, data is fetched from the database and changes are made.
  • Major operations in a database transaction:
    • Read: Access the database to fetch data.
    • Write: Make changes to the data.

Detailed Walkthrough of a Database Transaction

  • Scenario: Transferring money from Account A to Account B
  • Steps:
    1. Read account A balance from hard disk to RAM (e.g., A = $1000)
    2. Perform operation: A = A - 500 (new A balance = $500 in RAM)
    3. Read account B balance from hard disk to RAM (e.g., B = $2000)
    4. Perform operation: B = B + 500 (new B balance = $2500 in RAM)
    5. Commit: Save changes from RAM to hard disk (permanent changes).
  • Other operations: Commit and Rollback
    • Commit: Save all changes permanently.
    • Rollback: Revert changes (not covered in detail).

Key Points

  • **CPU and Memory: **
    • CPU performs operations in RAM, not directly on the hard disk.
    • This ensures speed as CPU is fast, and hard disk is slow.
    • Data must be in RAM or cache for the CPU to work on it.

Summary

  • Read: Fetch data from the database.
  • Write: Make changes to the data.
  • Commit: Save changes permanently to the database.
  • Transactions: A set of operations to complete tasks and change or access a database.
  • Transaction states and properties will be discussed further.