Transaction Management

Measurement
Introduced by
Jim Gray and others (IBM)
Year
1981
Type
Database reliability mechanism
Family
Data Consistency & Recovery

What does it do?

Transaction management is the mechanism by which a database system executes a group of related operations as one unit: either all of their changes are committed, or none are.

The transaction concept is associated on this page with Jim Gray's 1981 work, and the guarantees it provides are summarised as the ACID properties (Atomicity, Consistency, Isolation, Durability).

The intuition behind the method

Consider a transfer that debits one account and credits another. Between the two updates the database is briefly inconsistent: money has left one account but has not yet arrived in the other. A transaction groups both updates so that the database either reaches the state with both applied (commit) or returns to the state it had before the transaction began (rollback). Applications can then treat the business event as a single step rather than handling every possible failure in the middle. The guarantee covers the database's own state only; it does not reverse effects outside the database.

History

This page associates the transaction concept with Jim Gray, and its main reference is Gray's 1981 paper on the virtues and limitations of the concept. The ACID acronym names the four properties such systems are expected to provide. No individual is credited with coining the name ACID on this page. Later database systems added multi-version concurrency control (MVCC) and other techniques that reduce blocking between readers and writers while keeping these guarantees.

When to use it

Use transactions when several writes must succeed or fail together, for example funds transfers, order placement that reserves stock and records payment, or changes spanning several tables. They matter most when other sessions may write the same rows concurrently, since that is where interleaved operations can corrupt data. The method assumes the database can afford the locking or versioning needed for the affected rows. A workload that cannot tolerate waiting on those rows needs a different design or a weaker isolation level; a weaker level permits the anomalies described under limitations, which the application must then handle. Work that must span several databases or services needs a distributed commit protocol with its own blocking risks, so it is a separate design decision. Do not keep a transaction open while waiting for user input or an external service, because the locks it holds remain in place for that whole wait. A single-statement write is normally already atomic, so wrapping it in an explicit transaction adds overhead without adding protection.

Limitations and considerations

  • Locks are held until commit, so other transactions that need conflicting access to the same rows must wait, which reduces throughput under contention.
  • Two transactions that acquire the same locks in opposite orders can block each other; the database must then detect the deadlock and abort one of them.
  • Rollback undoes only database changes. Emails sent, payments issued to external systems or files written outside the database are not reversed by it.
  • Work spanning several databases or services needs a distributed commit protocol such as two-phase commit. That protocol can block when its coordinator fails, leaving participants holding their locks until recovery.
  • Isolation levels below SERIALIZABLE permit specific anomalies: dirty reads (seeing uncommitted changes of another transaction), non-repeatable reads (re-reading a row returns a different value) and phantoms (a repeated query returns rows inserted or removed by another committed transaction). Choosing a weaker level trades these anomalies for concurrency.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • All writes inside the transaction take effect together, so a failure partway through cannot leave a partly applied business event.
  • Committed changes are intended to persist after the commit completes. This holds when the database's write-ahead logging and commit-flush settings are configured to guarantee it, so these settings should be checked for the system in use.
  • Rollback gives the application a defined route back to the pre-transaction state, which is simpler than reversing partial changes by hand.

Applications

  • Funds transfers between accounts, where the debit and the credit must both be recorded or neither is.
  • Order processing that reserves stock and records the order together, so concurrent buyers cannot both claim the same unit.
  • Seat reservation in booking systems, where the seat is marked as taken and the booking record is created in one unit.
  • Payroll runs in which each employee's payment and its ledger entry are applied together.

How it works

How it works

1

Decide which operations belong together: those that must all take effect, or none, to leave the data in a valid state. The boundary follows the business event, not individual SQL statements.

2

Open the transaction with BEGIN or START TRANSACTION so the database knows where the atomic unit starts. Changes made after this point remain tentative until a commit.

3

Run the SQL statements (SELECT, INSERT, UPDATE, DELETE) inside the transaction. Their changes are pending and are not final until the transaction commits.

4

This is where concurrency control acts. Locks or versioning stop other transactions from overwriting or reading uncommitted changes in conflicting ways, and the strength of that protection depends on the isolation level chosen.

5

If every statement succeeded, commit to make the changes permanent; if an error occurred, roll back to undo all pending changes. The application must decide which of the two to issue based on the errors it observed. If the database aborts the transaction because of a deadlock or a serialization failure, the database reports this to the application as an error from its driver; the application should check the error type rather than treat every failure alike. Such a transaction should normally be retried as a whole from BEGIN, not by resuming the statement that failed. A retry is safe only if re-running the work does not repeat effects outside the database, so the transaction should be idempotent or its external effects controlled.

Common pitfalls

  • Opening a transaction and then waiting for user input or a network call keeps its locks held for the whole wait, blocking other sessions.
  • Catching an error and committing anyway records exactly the partial work the transaction was meant to prevent.
  • An exception that skips the rollback code leaves the transaction open with its locks still held.
  • Testing with little concurrency can hide deadlocks that appear only when many transactions overlap, so concurrent load testing is needed.
  • Retrying an aborted transaction blindly can repeat external effects, such as emails or payments, that were already sent before the abort.

Frequently asked questions

What does ACID stand for and why does it matter?

Atomicity (all-or-nothing), Consistency (the database moves only between valid states, as defined by its constraints), Isolation (concurrent transactions behave as if run separately, to the degree the chosen isolation level provides), Durability (committed changes persist). Together these let applications rely on the database to keep its data valid across failures and concurrent sessions, although business rules that the schema does not express still belong to the application.

What is the difference between COMMIT and ROLLBACK?

COMMIT makes all changes in the transaction permanent and visible to other users. ROLLBACK undoes all changes, restoring the database to its state before BEGIN. Both end the transaction.

What causes deadlocks and how can I prevent them?

A deadlock occurs when two transactions each hold a lock the other needs, so neither can proceed. Prevention reduces the chance of this: acquire locks on shared resources in a consistent order across the application, and keep transactions short so they hold locks briefly. The database still needs its own deadlock detection to identify the cycle and abort one of the transactions, because prevention cannot remove every case.

How do isolation levels affect transaction behavior?

Isolation levels (READ UNCOMMITTED through SERIALIZABLE) control how much other transactions' changes are visible. Higher isolation prevents anomalies but reduces concurrency. Choose based on your consistency requirements.

What should my application do when the database aborts a transaction?

A transaction aborted for a deadlock or serialization failure is reported to the application as an error from the database driver, and the database normally rolls it back. The application can then retry the whole transaction from BEGIN, but only if re-running it is safe: it must not repeat emails, payments or other effects outside the database that already took place.

Related methods

Sources

  1. Gray, J. (1981). The transaction concept: Virtues and limitations. VLDB Endowment, 7(6), 519-539.

    Open publication reference
  2. Papadimitriou, C. H. (1986). The Theory of Database Concurrency Control. Computer Science Press.

    Open publication reference
  3. Garcia-Molina, H., Ullman, J. D., & Widom, J. (2009). Database Systems: The Complete Book (2nd ed.). Pearson Education.

    Open publication reference