In plain words
A transaction groups several database changes so they succeed or fail together: if any step fails, the database undoes the others, so nothing is left half done.
Where you see it
Upgrading a Harbor team writes three things: the old subscription marked ended, a new subscription row, and an invoice. Without a transaction, a crash after the first write leaves a team with no active plan and no invoice; inside one, the crash undoes all three.
What goes wrong
- Calling Stripe inside the transaction
A rollback undoes database writes, not a card charge. If the charge succeeds and the commit then fails, the customer has paid for nothing, so keep outside calls out of the transaction and record their results in a way you can reconcile.
- Using the outer client inside the callback
A query sent through db instead of tx runs outside the transaction and isn't rolled back. Every query inside must use the transaction's own handle.
await db.insert(invoices).values(row); // inside the callback, outside the transaction - Expecting a transaction to stop every race
Two requests can both read a seat count of 4, both add a member and both commit, leaving 6 members on a 5-seat plan. Lock the row with select ... for update, or let a constraint refuse the second write.
What to tell your agent
Quick check
Sign in to earn +10 XP for itHarbor's upgrade code charges the card through Stripe, then saves the new subscription in a transaction. The save fails. What does the rollback undo?