Skip to main content

Transaction

Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/transaction

In short

A transaction is a group of database operations that succeed or fail as a single unit, so the data is never left in a half-finished, inconsistent state.

What is a database transaction?

A transaction bundles several database operations together so they are treated as one all-or-nothing action. If every step succeeds, the changes are saved with a COMMIT; if anything goes wrong, a ROLLBACK undoes all of them, as if the transaction never happened.

The classic example is a bank transfer: subtracting money from one account and adding it to another must both happen, or neither. Without a transaction, a crash between the two steps could make money disappear. With a transaction, the database guarantees that the two updates are applied together.

Reliable transactions follow the ACID properties. Atomicity means all or nothing; consistency means the data always moves from one valid state to another; isolation means concurrent transactions don't see each other's unfinished work; and durability means committed changes survive crashes and power loss. Databases offer different isolation levels, such as Read Committed and Serializable, which trade strictness for performance.

A database transaction is different from a business transaction like a purchase, although one is often used to record the other. Transactions also get harder in distributed systems such as microservices, where one operation spans several databases; there, teams often use patterns like sagas, a series of local transactions with compensating steps, instead of a single ACID transaction.

At a glance

A transfer of 100 from Ada to Ben as one transaction: BEGIN, take 100 from Ada, add 100 to Ben, COMMIT. If everything succeeds, both changes are saved together. If anything fails before the commit, the database rolls back and both balances stay as they were.BEGINstartAda −100UPDATE accounts…Ben +100UPDATE accounts…COMMITsave bothCommittedAda 400 → 300 · Ben 100 → 200Crash or error before COMMITROLLBACKAda 400 · Ben 100, as before
All or nothing: either both changes are saved, or neither is. No one ever sees the money gone from Ada but not yet with Ben.

Key takeaways

  • A transaction groups operations into one all-or-nothing unit.
  • COMMIT saves the changes; ROLLBACK undoes them.
  • ACID stands for atomicity, consistency, isolation, and durability.
  • Isolation levels control how concurrent transactions affect each other.

Example

A bank transfer inside a transactionsql
-- Transfer $100 from account 1 to account 2
BEGIN;

UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;

-- Save both changes together
COMMIT;

-- If something had failed, you would run ROLLBACK instead,
-- and neither update would be applied

Readers ask

What does ACID mean in databases?

ACID stands for atomicity, consistency, isolation, and durability, the four guarantees that keep database transactions reliable even when errors, crashes, or many simultaneous users occur.

What is the difference between COMMIT and ROLLBACK?

COMMIT permanently saves all the changes made in the current transaction, while ROLLBACK discards them and restores the data to how it was before the transaction started.

Do NoSQL databases support transactions?

Many do. MongoDB, for example, supports multi-document ACID transactions, but guarantees and performance vary by database, so check the documentation for your specific system.

See also

Sources

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings