Transactions

Comprehensive study notes, diagrams, and exam preparation for Transactions.

Transactions

Definition

A transaction is a logical unit of work that must be executed entirely or not at all, so that a system remains consistent even if errors, crashes, or interruptions occur.

In database terms, a transaction is a group of SQL or system operations that follow the ACID properties:

Atomicity

  • : all steps succeed or none do

Consistency

  • : data remains valid before and after the transaction

Isolation

  • : concurrent transactions do not interfere improperly

Durability

  • : once committed, changes are permanently saved

In everyday life, the idea is similar to a purchase order. If you buy a ticket online, the system may check seat availability, accept payment, and reserve the seat. These steps are connected; if one fails, the whole booking should fail to avoid confusion or loss.


Main Content

1. ACID Properties

Atomicity and Consistency

  • : Atomicity means a transaction is indivisible. Either every operation is completed or the system undoes everything. Consistency means the transaction must move the database from one valid state to another valid state. For example, during a bank transfer, the total money across both accounts should remain logically correct after the transaction.

Isolation and Durability

  • : Isolation ensures that when multiple transactions run at the same time, each one behaves as if it is executing alone, reducing problems like dirty reads or lost updates. Durability ensures that after a transaction is committed, the results are stored permanently, even if power fails or the system crashes immediately afterward.

2. Transaction States

Active and Partially Committed

  • : A transaction begins in the active state when it starts executing. It becomes partially committed after all operations finish successfully, but before the changes are fully written to permanent storage. At this stage, the system still needs to confirm that the commit is safe.

Committed and Failed

  • : A transaction is committed once all changes are successfully saved. If an error occurs at any stage, the transaction enters the failed state. For instance, if a payment gateway rejects a card during checkout, the transaction fails and the order should not be completed.

3. Concurrency Control and Rollback

Concurrency Control

  • : In multi-user systems, many transactions may run at the same time. Concurrency control techniques, such as locks, timestamps, and scheduling rules, prevent transactions from corrupting each other’s data. For example, two users should not be able to reserve the last available seat at the exact same moment.

Rollback and Recovery

  • : If a transaction cannot complete, rollback restores the system to its previous safe state by undoing the changes made during that transaction. Recovery mechanisms use logs, checkpoints, and backup data to rebuild the correct state after failures. This is important in banks, hospitals, e-commerce websites, and any system where errors can cause serious damage.

Working / Process

1. Begin the transaction

  • The system starts a transaction when a user action or program request requires multiple related operations. In a database, this may happen with a BEGIN TRANSACTION command or automatically when an application starts a controlled update.

2. Execute all required operations

  • The system performs each step in the transaction, such as reading data, updating records, calculating amounts, or inserting new entries. During this stage, the changes are usually not made permanent yet. If any operation fails, the system stops the transaction and prepares to undo the work.

3. Commit or roll back

  • If every step is successful, the system commits the transaction, making the changes permanent. If an error, conflict, or crash occurs, the system rolls back the transaction so the database returns to its original consistent state. This final step ensures reliability and protects data integrity.

Advantages / Applications

Data integrity and accuracy

  • : Transactions prevent partial updates and keep information correct. This is crucial in banking, inventory management, airline booking, and medical records, where even a small inconsistency can cause major issues.

Fault tolerance and recovery

  • : Transactions help systems recover from errors, crashes, and power failures without corrupting data. Because incomplete work can be rolled back, the system remains stable and trustworthy.

Support for real-world operations

  • : Transactions are used in financial transfers, online shopping, ticket reservations, payroll processing, and order management. They make complex operations manageable by ensuring that multiple related changes happen safely as one unit.

Summary

  • Transactions group related actions into one reliable unit of work.
  • They protect systems from partial updates and inconsistencies.
  • They are widely used in databases, banking, and other critical applications.
  • Important terms to remember: commit, rollback, ACID, consistency, isolation, durability