Group
  • Database

    Wrap multi-step writes in transactions

    Finds write flows that touch multiple records and makes them atomic so partial failures can't corrupt state.

    • Improve quality
    • Debug & fix
    • Advanced
    • General

Free Prompt

Find every write flow in my app that changes more than one record and wrap it in a transaction. Look for the money paths first: checkout (create order, create line items, decrement inventory, record payment), account changes (transfer ownership, merge records), signup (create user, create profile, create default settings), and anything with a loop of inserts. For each flow: wrap the whole sequence in a database transaction so either every write commits or none do. Check my database's transaction support and use its real API (multi-document transactions in MongoDB, BEGIN/COMMIT in SQL, or my ORM's transaction wrapper). Make sure errors inside the flow trigger a rollback and surface a clean failure to the caller, not a swallowed partial state. Keep transactions short: no email sending, API calls, or slow work inside them, since held locks block other writes. Do not wrap single-record writes pointlessly. Do not change the business logic or the order of operations, just the atomicity. Deliver: each flow identified, the transaction added, and a failure-injection test for the money paths: force the second write to fail and confirm the first write rolls back instead of leaving an order with no line items or inventory decremented for an order that doesn't exist.

What This Does / How This Helps

Finds the multi-step writes in your app and makes each one atomic, so a failure halfway through can't leave half-finished state behind. The classic damage: the charge succeeds, the order insert fails, and the customer paid for nothing. Or inventory decrements for an order that never got created. These flows look fine until the first production hiccup, and then you're reconciling money by hand. Transactions make the sequence all-or-nothing. The failure-injection tests are the proof: deliberately break step two and confirm step one vanishes with it.

Want to skip doing this by hand?

Fortivibe audits your app for all of the areas these prompts cover (and more).

See What We Check

Related Prompts