{"success":true,"name":"wrap-multi-step-database-writes-in-transactions","description":"One operation writes to several tables or rows that must stay consistent.","category":"ai-brimind","path":"ai-brimind/wrap-multi-step-database-writes-in-transactions/SKILL.md","file":"SKILL.md","content":"---\nname: wrap-multi-step-database-writes-in-transactions\ndescription: \"One operation writes to several tables or rows that must stay consistent.\"\n---\n\n# Wrap Multi-Step Database Writes in Transactions\n\n## When to use\nProject uses a database, and a single logical action (order + items, transfer + balances, user + profile) performs multiple writes that must all succeed together.\n\n## Example request\nOrder row saved, then the items insert fails → an order with no items exists forever — partial write. Money subtracted from one account, crash before adding to the other → money vanished — non-atomic transfer. Two requests update the same counter simultaneously → one update overwrites the other — lost update without transaction isolation.\n\n## Instructions\nAct like a developer who assumes the process can die between any two lines. When one logical action requires multiple writes: (1) name the atomic unit out loud — 'order + its items + stock decrement succeed together or not at all'; (2) wrap exactly those writes in a transaction (BEGIN/COMMIT with ROLLBACK on error, or the ORM's transaction API) so the database guarantees all-or-nothing; (3) keep transactions short — do slow work (network calls, file processing) BEFORE or AFTER the transaction, never inside it, or you'll hold locks and block other requests; (4) on error, make sure the rollback actually runs (try/catch or the library's callback form) and the error is surfaced, not swallowed. Check whether the framework already provides transaction helpers before managing connections manually. Only proceed once you can point at the transaction boundaries and answer: 'if the process crashes right here, what state is the data in?' — and the answer for every line is 'a consistent one'.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1085\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}