diff --git a/docs/howto/transactions.md b/docs/howto/transactions.md index bc93ebd661..9ed61b6a4d 100644 --- a/docs/howto/transactions.md +++ b/docs/howto/transactions.md @@ -57,6 +57,7 @@ func (q *Queries) WithTx(tx *sql.Tx) *Queries { You'd use it like this: ```go +// Using `github/lib/pq` as the driver. func bumpCounter(ctx context.Context, db *sql.DB, queries *tutorial.Queries, id int32) error { tx, err := db.Begin() if err != nil { @@ -76,4 +77,25 @@ func bumpCounter(ctx context.Context, db *sql.DB, queries *tutorial.Queries, id } return tx.Commit() } + +// Using `github.com/jackc/pgx/v5` as the driver. +func bumpCounter(ctx context.Context, db *pgx.Conn, queries *tutorial.Queries, id int32) error { + tx, err := db.Begin(ctx) + if err != nil { + return err + } + defer tx.Rollback(ctx) + qtx := queries.WithTx(tx) + r, err := qtx.GetRecord(ctx, id) + if err != nil { + return err + } + if err := qtx.UpdateRecord(ctx, tutorial.UpdateRecordParams{ + ID: r.ID, + Counter: r.Counter + 1, + }); err != nil { + return err + } + return tx.Commit(ctx) +} ``` \ No newline at end of file