Skip to content

Transactions

github-actions[bot] edited this page Dec 2, 2026 · 2 revisions

Transactions

err := store.Tx(ctx, func(tx ablage.Store) error {
	from, err := accounts.With(tx).Get(ctx, fromID)
	if err != nil {
		return err
	}
	// ...
	return transfers.With(tx).Create(ctx, &t)
})

The function's error, or a panic, rolls everything back; nil commits. Inside, use only tx and repositories bound with With(tx): the outer store would wait forever for the lock (memory) or the only connection (SQLite with one connection). Do not keep tx after the function returns.

A failed operation inside (e.g. ErrDuplicate) does not end the transaction in any store; on Postgres each write runs under a savepoint for that. You may handle the error and carry on.

A transaction is committed whole or not at all, in every store. Cosmos DB commits one only up to 100 documents (see Stores); a larger one, or a single Update or Delete matching that many rows, fails with ErrTooLarge before anything is written. Stores with such a limit implement ablage.Limiter. On Cosmos DB, writes inside a transaction are sent when it commits, and a row someone else changed in the meantime fails the commit with ErrStale.

A Tx inside a Tx joins the outer one: if the inner function fails and the outer one ignores the error, the inner writes are committed with the outer ones.

Update retries on ErrStale; inside a transaction a retry may see the same data again, so set the attempts low or check versions yourself there.

Clone this wiki locally