AI Glossary / Data engineering and architecture / Beginner

Database transaction

Definition

A database transaction is a unit of work that groups database operations under defined completion and concurrency rules. For transactional changes, atomicity means the included changes commit together or are rolled back together. Transactions help applications avoid partially applied updates, while the database's isolation level determines how concurrent work can interact.

What it means

Many business actions require several related records to change. Reserving stock and recording the reservation should not leave one completed while the other is missing. A transaction lets the application express that relationship to a database that supports the required behavior.

The boundary matters. A transaction in one database does not automatically include an email, an external payment service or another database. It also does not make incorrect business logic correct. The application must choose appropriate operations, constraints and concurrency controls for the intended action.

How it works

An application begins a transaction explicitly or through its database library's behavior. It performs the necessary reads and writes, then requests a commit when the work is ready. If it cannot complete the work, it requests a rollback or responds to an error that aborts the transaction.

The familiar ACID properties describe atomicity, consistency, isolation and durability. Consistency means respecting the relevant integrity rules; those rules must be correctly defined and supported by application logic. Isolation concerns concurrent execution, and durability concerns the persistence promised for committed work. Exact guarantees depend on the system and configuration.

Databases use mechanisms such as locks, versions and logging to provide their behavior. Applications may encounter waits, deadlocks or serialization failures and need an appropriate retry strategy. Keep transactions focused, and distinguish a confirmed rollback from a lost response whose final outcome is not yet known.

Three practical examples

1. An inventory service records a reservation and adjusts available stock in one database transaction, with concurrency handling that prevents conflicting reservations from violating its rules.

2. A document system creates a new document record and its required metadata together. If an essential database operation fails, it avoids retaining an incomplete database representation.

3. An AI-assisted workflow proposes a record update through a controlled application tool. The tool validates the request and applies related database changes transactionally, rather than assuming the assistant's output is itself a completed operation.

Think of it this way

Think of submitting a packet of linked forms that is accepted as a unit or returned for correction. Partial acceptance would leave an inconsistent record. The analogy has limits: a database transaction may overlap with others, and external actions do not necessarily belong to the same packet. A sent message cannot generally be unsent merely by rolling back local database changes.

Worked example

Suppose a fictional store has 10 available units of an item and no reservation for request R17. A transaction checks the applicable stock rule, decreases available units by three and records a three-unit reservation for R17. On successful commit, available stock is seven and the reservation exists.

If recording the reservation fails and the transaction rolls back, the transactional stock change must not remain: the system returns to 10 available units with no R17 reservation, assuming no other relevant committed work. Without the common transaction boundary, the stock update could persist alone.

Now consider another customer reserving stock concurrently. Atomicity alone does not establish that both applications' read-check-write sequences enforce the stock rule. Suitable isolation, locking, conditional updates or other supported mechanisms must address that interaction. Also give R17 an appropriate identity so that retrying after a lost response does not blindly create a second reservation. The example separates all-or-nothing changes from concurrency correctness and duplicate-request handling.

Illustrative case study

Fictional case: CommitHarbour's order service writes an order and immediately sends a confirmation message before committing its database transaction. A later database error rolls back the order, leaving the customer with a confirmation for an order that does not exist.

The team recognizes that the message service was outside the local transaction. It redesigns the coordination, including a durable record of the intended notification and controlled delivery handling, then tests failures at each boundary. It also checks duplicate delivery behavior.

Local transaction guarantees did not cover every surrounding action; recovery required examining the full workflow.

Common misconceptions and mistakes

Do not assume that every operation is transactional in the same way. Products can have special behavior for sequences, structural commands or external effects. Check the actual system rather than extending the word rollback beyond its documented scope.

Avoid assuming that one transaction always sees an unchanging snapshot; isolation levels differ. Do not leave a transaction open while waiting for a person to finish unrelated work, since resources or versions may remain occupied. Finally, a timeout observed by the client does not always prove that the database failed to commit.

Business use

Transactions help protect meaningful business units of work, such as reservations, allocations and related record updates. Define the required invariant first, then choose a transaction boundary and isolation behavior that support it under concurrent activity.

Test interruption, contention and duplicate requests as well as the normal path. Monitor long-running transactions and failure rates. For AI integrations, place transactional operations behind a deliberate application interface that can report confirmed success, failure or uncertainty instead of presenting a generated statement as evidence that a change occurred.

People also ask

Can a transaction contain only one statement?

Yes. Some systems automatically run individual statements in transactions, while explicit blocks group several operations.

Does rollback undo an external email?

Usually no. A local database rollback only covers operations within its transactional scope; external actions need separate coordination.

Does using a transaction prevent every concurrency error?

No. The operations, constraints and isolation behavior must support the required rule. Some conflicts require retries or explicit coordination.

Check your understanding

A reservation transaction may have committed, but the client lost its response. Is it safe to repeat the request as a completely new reservation?

Answer: Not without checking or preventing duplication. The client has an uncertain outcome, not proof of rollback. Use a request identity and supported recovery logic to establish the intended result.

Related terms

  • Commit (data management)
  • Concurrency control
  • Savepoint
  • Durability (database systems)
  • Distributed transaction
  • ACID
  • commit
  • rollback
  • isolation level
  • lock
  • durability
  • idempotency
  • transaction boundary

Learn next

Learn isolation levels and idempotent request handling next. In a disposable database, test a two-step update that succeeds and another that fails halfway. Then run competing operations and inspect whether your business rule still holds, rather than checking only that each individual transaction completes.

  • Business rule

More in Data engineering and architecture

  • Data Warehouse - A data warehouse is a data-management environment organized primarily for analysis and reporting, often integrating current and historical information from...
  • Data warehouse automation - Data warehouse automation is the use of software to generate and manage repeatable parts of a data warehouse's lifecycle.
  • Database - A database is an organized collection of data that can be stored, accessed and maintained as a coherent whole.
  • Database-as-IPC - Database-as-IPC means using a database as a channel for inter-process communication: one process writes information that another reads to discover work or...
  • Database Client - A database client is a program or software component that requests database services, such as running queries and receiving results.
  • Database connection - A database connection is the communication context through which a program interacts with a database.

Sources

PostgreSQL, Transactions, explains transaction blocks, commit, rollback and savepoints: https://www.postgresql.org/docs/current/tutorial-transactions.html

PostgreSQL, Transaction Isolation, explains concurrent visibility, anomalies and product-specific exceptions: https://www.postgresql.org/docs/current/transaction-iso.html

The primary documentation was read. The reservation and notification examples are original illustrations. They distinguish local transaction guarantees from the additional design needed for external effects and uncertain outcomes.

Previous: Database semantics Next: Database transaction schedule

  • Home
  • Daily AI Updates
  • Blog
  • AIFBL Academy
  • AI Glossary
  • Products and Tools
  • Store
  • Free CFO AI Prompt Pack
  • About Shihan Sheriff
  • Privacy Policy
  • Terms of Use
  • Refund Policy
  • Cookie Policy
  • Earnings Disclaimer