Connections
ant sits on database/sql. The pool and connections are the standard library's, and db.DB() returns the *sql.DB.
Opening
ant.Open connects and fills in your context. ant doesn't pick a driver, so import one.
import _ "github.com/jackc/pgx/v5/stdlib"
func open(ctx context.Context) (*AppDB, error) {
return ant.Open[AppDB](ctx, postgres.Dialect, dsn)
}
Call Close once, on the context from Open. It closes the pool.
Pool settings
db.DB().SetMaxOpenConns(25)
db.DB().SetConnMaxLifetime(5 * time.Minute)
An existing pool
ant.Wrap puts a context on a pool you already have.
pool, err := sql.Open("pgx", dsn)
if err != nil {
return err
}
db, err := ant.Wrap[AppDB](postgres.Dialect, pool)
Close closes that pool too.
Join a transaction started elsewhere
ant.UseTx binds a context to a *sql.Tx that another library began, such as a job queue or a middleware. Your code commits or rolls it back.
tx, err := db.DB().BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
bound, err := ant.UseTx(db, tx)
if err != nil {
return err
}
err = bound.Users.Insert(ctx, alice)
if err != nil {
return err
}
return tx.Commit()
ant can't see how that transaction ends, so:
- What ant saves in it is treated as saved at once, as outside a transaction. If you roll the
*sql.Txback, the tracker still thinks the rows are stored. Reload or clear what it saved. - Observers don't run, since they wait for a commit ant can't see.
- A savepoint ant opens in it, through
bound.Tx, is ant's own, and its rollback is taken back in the tracker as usual. - On SQL Server, end the transaction before its context ends. go-mssqldb rolls back with the context the transaction began with, and an ended context leaves a goroutine reading the connection after it returns to the pool. ant avoids this in its own transactions, but one from
UseTxkeeps the driver's behavior.
Share a transaction between context types
ant.CurrentTx lets two context types share one transaction, so they commit or roll back together.
type OtherDB struct {
ant.Context
Posts ant.Table[Post]
}
err := db.Tx(ctx, func(tx *AppDB) error {
joined, err := ant.UseTx(other, ant.CurrentTx(tx))
if err != nil {
return err
}
return joined.Posts.Insert(ctx, post)
})
Keep work on one connection
ant.UseConn keeps everything on one connection from the pool, for a session setting or an advisory lock. Transactions stay on that connection too. You close the connection.
conn, err := db.DB().Conn(ctx)
if err != nil {
return err
}
defer conn.Close()
bound, err := ant.UseConn(db, conn)
if err != nil {
return err
}
_, err = ant.Raw[any](bound, "SET application_name = 'worker'").Exec(ctx)
An advisory lock belongs to a connection, so a job that must run on one instance at a time locks and works through the same bound context:
conn, err := db.DB().Conn(ctx)
if err != nil {
return err
}
defer conn.Close()
bound, err := ant.UseConn(db, conn)
if err != nil {
return err
}
_, err = ant.Raw[any](bound, "SELECT pg_advisory_lock(42)").Exec(ctx)
if err != nil {
return err
}
defer ant.Raw[any](bound, "SELECT pg_advisory_unlock(42)").Exec(ctx)
err = bound.Tx(ctx, func(tx *AppDB) error {
return tx.Posts.Insert(ctx, post)
})
Use bound from one goroutine at a time, as you would a *sql.Conn. Two running together fail in the driver, and two savepoint stacks on one connection would undo each other's work. For parallel work, take a connection per goroutine.
Read replicas
ant.WithReplicas sends reads outside transactions to replicas in turn. Writes, locks, and transactions use the primary. A failing replica is an error for that read, not a retry elsewhere.
replica, err := sql.Open("pgx", replicaDSN)
if err != nil {
return err
}
db, err := ant.Open[AppDB](ctx, postgres.Dialect, dsn,
ant.WithReplicas(replica),
ant.WithStickyWrites(2*time.Second),
)
WithStickyWrites sends a context's reads to the primary for a while after each of its writes, so replication lag can't hide them. ant.OnPrimary forces one read:
users, err := db.Users.All(ant.OnPrimary(ctx))
For separate databases, see several databases.
See it tested: tests/features/transactions