Transaction

A group of statements that commit together or not at all. In Go, db.BeginTx returns a *sql.Tx; every statement must go through it, a deferred Rollback cleans up early returns, and Commit makes the changes visible and durable.

Minimal example

go
tx, err := db.BeginTx(ctx, nil)
if err != nil {
	return err
}
defer func() { _ = tx.Rollback() }()
// tx.ExecContext(...)
return tx.Commit()

Learn this concept

Lesson 3.7 · Transactions in Go and MySQL

Used in exercises

Exercise 3.10 · POST /v1/orders: idempotent checkout Exercise 3.12 · POST /v1/orders/{id}/events: order lifecycle Exercise 3.15 · Outbox relay to order-events Exercise 3.17 · Notifier worker with retries and DLQ Exercise 3.23 · Concurrent outbox relay