Advanced 12 min

Transactions and consistency

A transaction gives related changes one commit boundary.

How it works

Keep both inventory changes within the same unit of work. An error before commit lets the program discard the whole update. For simultaneous orders, also think about whether another transaction might see an older count and make its own decision too early.

Walk through an example

Moving one item between inventories reduces the source count and increases the destination count. Commit both changes together so a failure cannot leave just one adjustment saved.

What to watch for

Concurrent transactions can still interact in surprising ways. Check the database’s isolation behavior and protect the rule you rely on.

Check your understanding

What should happen to the source change?

See the example and explanation
Worked example
Given The source count changes. The destination update then fails before commit.
Answer It should roll back.

Both changes belong to the same transaction, so aborting it cancels the uncommitted source update.

Open practice problem
Next lessonConcurrency and race conditions