liveBook · Manning

Database transaction

As explained in The Coder Cafe

A transaction is a sequence of database operations treated as one logical unit of work with defined consistency and atomicity behavior.

A database transaction is an indivisible unit of work that groups multiple operations into one logical step. It protects data when operations must succeed together, such as deducting money from one account and adding it to another. section 6.5 section 6.1.1

Transactions are commonly understood through four ACID properties and a short operational lifecycle:

  • Atomicity makes the work all-or-nothing: if one operation fails, the transaction rolls back rather than leaving partial changes. section 6.1.1
  • Consistency requires each accepted transaction to preserve database integrity rules, such as foreign-key constraints and valid data types. section 6.1.1
  • Isolation limits the effect of concurrent transactions; isolation levels determine how strictly this property is enforced. section 6.5
  • Durability makes committed changes permanent, while rollback undoes changes that were not committed. section 6.5
  • The lifecycle is begin, perform reads or writes, then commit or roll back. A savepoint can provide an intermediate rollback target during complex work. section 6.5

Bank transfer

Treat subtracting funds from one account and adding them to another as one transaction. If either operation fails, roll back both changes; otherwise, commit them together. section 6.1.1

Transactions protect structural data integrity but do not automatically enforce every business invariant. For example, keeping the sum of customer balances positive may require application-level logic. Atomicity can also span a database write and a Kafka message, making the guarantee a property of the wider system. section 6.1.1 section 6.5

How The Coder Cafe covers it

How deep it goes
Explains ACID properties, lifecycle steps, isolation levels, rollback, and savepoints, while deferring some invariants to application logic.
Examples
Uses bank transfers, database writes, Kafka messages, commit, rollback, and PostgreSQL savepoints as concrete illustrations.
What it assumes
Assumes database operations and integrity rules while defining transaction behavior from the ACID properties upward.
What you can do after
Operate transactions by beginning work, performing operations, and choosing commit, rollback, or a savepoint-based rollback.
How it connects
Connects transactions with ACID, isolation levels, data integrity, concurrency control, savepoints, and distributed systems.

Also explained in

Compare how 21 books explain database transaction →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Coder Cafe. The section links point to the parts of the book it draws on.