Changes that must succeed together.
WHAT A TRANSACTION IS
A group of changes applied entirely or not at all.
WHEN TO USE ONE
Several related changes that must be consistent Money movements Anything where partial completion leaves the data wrong
WHAT HAPPENS WITHOUT ONE
A failure halfway leaves the database inconsistent.
WHAT THAT LOOKS LIKE
An order recorded without its items Stock reduced without a sale A payment recorded twice
HOW TO USE ONE
Wrap the changes, so an exception reverses everything.
WHAT TO KEEP OUTSIDE A TRANSACTION
Anything slow External calls
WHY
A transaction holds locks, and holding them during a network call is harmful.
WHAT TO DO ABOUT EXTERNAL CALLS
Perform them before or after, and handle the consequences of partial completion explicitly.
WHAT ELSE PROTECTS INTEGRITY
Foreign key constraints Unique constraints Not-null constraints
WHY DATABASE CONSTRAINTS MATTER
They hold even when code has a bug.
WHAT TO TEST
Behaviour when something fails mid-operation.