diff options
author | Matt T. Proud <matt.proud@gmail.com> | 2021-02-25 01:03:35 +0100 |
---|---|---|
committer | Jean de Klerk <deklerk@google.com> | 2021-03-11 16:37:38 +0000 |
commit | 0fc370c5d26353a865a29a01f093942c949d530a (patch) | |
tree | 80c940f284e55c84cf49bba24fc9d273bd86ef64 /src/database | |
parent | b8e9ec856c2b2d717ab14e85f43e00b532c4370a (diff) | |
download | go-0fc370c5d26353a865a29a01f093942c949d530a.tar.gz go-0fc370c5d26353a865a29a01f093942c949d530a.zip |
docs: clarify when APIs use context.Background.
The Go standard library retrofitted context support onto existing APIs
using context.Background and later offered variants that directly
supported user-defined context value specification. This commit makes
that behavior clear in documentation and suggests context-aware
alternatives if the user is looking for one.
An example motivation is supporting code for use in systems that expect
APIs to be cancelable for lifecycle correctness or load
shedding/management reasons, as alluded to in
https://blog.golang.org/context-and-structs.
Updates #44143
Change-Id: I2d7f954ddf9b48264d5ebc8d0007058ff9bddf14
Reviewed-on: https://go-review.googlesource.com/c/go/+/296152
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Jean de Klerk <deklerk@google.com>
Trust: Jean de Klerk <deklerk@google.com>
Run-TryBot: Jean de Klerk <deklerk@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Diffstat (limited to 'src/database')
-rw-r--r-- | src/database/sql/sql.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/database/sql/sql.go b/src/database/sql/sql.go index 37bcb0d091..12cc524c63 100644 --- a/src/database/sql/sql.go +++ b/src/database/sql/sql.go @@ -813,6 +813,9 @@ func (db *DB) PingContext(ctx context.Context) error { // Ping verifies a connection to the database is still alive, // establishing a connection if necessary. +// +// Ping uses context.Background internally; to specify the context, use +// PingContext. func (db *DB) Ping() error { return db.PingContext(context.Background()) } @@ -1481,6 +1484,9 @@ func (db *DB) PrepareContext(ctx context.Context, query string) (*Stmt, error) { // returned statement. // The caller must call the statement's Close method // when the statement is no longer needed. +// +// Prepare uses context.Background internally; to specify the context, use +// PrepareContext. func (db *DB) Prepare(query string) (*Stmt, error) { return db.PrepareContext(context.Background(), query) } @@ -1551,6 +1557,9 @@ func (db *DB) ExecContext(ctx context.Context, query string, args ...interface{} // Exec executes a query without returning any rows. // The args are for any placeholder parameters in the query. +// +// Exec uses context.Background internally; to specify the context, use +// ExecContext. func (db *DB) Exec(query string, args ...interface{}) (Result, error) { return db.ExecContext(context.Background(), query, args...) } @@ -1621,6 +1630,9 @@ func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{ // Query executes a query that returns rows, typically a SELECT. // The args are for any placeholder parameters in the query. +// +// Query uses context.Background internally; to specify the context, use +// QueryContext. func (db *DB) Query(query string, args ...interface{}) (*Rows, error) { return db.QueryContext(context.Background(), query, args...) } @@ -1719,6 +1731,9 @@ func (db *DB) QueryRowContext(ctx context.Context, query string, args ...interfa // If the query selects no rows, the *Row's Scan will return ErrNoRows. // Otherwise, the *Row's Scan scans the first selected row and discards // the rest. +// +// QueryRow uses context.Background internally; to specify the context, use +// QueryRowContext. func (db *DB) QueryRow(query string, args ...interface{}) *Row { return db.QueryRowContext(context.Background(), query, args...) } @@ -1750,6 +1765,9 @@ func (db *DB) BeginTx(ctx context.Context, opts *TxOptions) (*Tx, error) { // Begin starts a transaction. The default isolation level is dependent on // the driver. +// +// Begin uses context.Background internally; to specify the context, use +// BeginTx. func (db *DB) Begin() (*Tx, error) { return db.BeginTx(context.Background(), nil) } @@ -2255,6 +2273,9 @@ func (tx *Tx) PrepareContext(ctx context.Context, query string) (*Stmt, error) { // be used once the transaction has been committed or rolled back. // // To use an existing prepared statement on this transaction, see Tx.Stmt. +// +// Prepare uses context.Background internally; to specify the context, use +// PrepareContext. func (tx *Tx) Prepare(query string) (*Stmt, error) { return tx.PrepareContext(context.Background(), query) } @@ -2358,6 +2379,9 @@ func (tx *Tx) StmtContext(ctx context.Context, stmt *Stmt) *Stmt { // // The returned statement operates within the transaction and will be closed // when the transaction has been committed or rolled back. +// +// Stmt uses context.Background internally; to specify the context, use +// StmtContext. func (tx *Tx) Stmt(stmt *Stmt) *Stmt { return tx.StmtContext(context.Background(), stmt) } @@ -2374,6 +2398,9 @@ func (tx *Tx) ExecContext(ctx context.Context, query string, args ...interface{} // Exec executes a query that doesn't return rows. // For example: an INSERT and UPDATE. +// +// Exec uses context.Background internally; to specify the context, use +// ExecContext. func (tx *Tx) Exec(query string, args ...interface{}) (Result, error) { return tx.ExecContext(context.Background(), query, args...) } @@ -2389,6 +2416,9 @@ func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{ } // Query executes a query that returns rows, typically a SELECT. +// +// Query uses context.Background internally; to specify the context, use +// QueryContext. func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) { return tx.QueryContext(context.Background(), query, args...) } @@ -2410,6 +2440,9 @@ func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...interfa // If the query selects no rows, the *Row's Scan will return ErrNoRows. // Otherwise, the *Row's Scan scans the first selected row and discards // the rest. +// +// QueryRow uses context.Background internally; to specify the context, use +// QueryRowContext. func (tx *Tx) QueryRow(query string, args ...interface{}) *Row { return tx.QueryRowContext(context.Background(), query, args...) } @@ -2516,6 +2549,9 @@ func (s *Stmt) ExecContext(ctx context.Context, args ...interface{}) (Result, er // Exec executes a prepared statement with the given arguments and // returns a Result summarizing the effect of the statement. +// +// Exec uses context.Background internally; to specify the context, use +// ExecContext. func (s *Stmt) Exec(args ...interface{}) (Result, error) { return s.ExecContext(context.Background(), args...) } @@ -2687,6 +2723,9 @@ func (s *Stmt) QueryContext(ctx context.Context, args ...interface{}) (*Rows, er // Query executes a prepared query statement with the given arguments // and returns the query results as a *Rows. +// +// Query uses context.Background internally; to specify the context, use +// QueryContext. func (s *Stmt) Query(args ...interface{}) (*Rows, error) { return s.QueryContext(context.Background(), args...) } @@ -2726,6 +2765,9 @@ func (s *Stmt) QueryRowContext(ctx context.Context, args ...interface{}) *Row { // // var name string // err := nameByUseridStmt.QueryRow(id).Scan(&name) +// +// QueryRow uses context.Background internally; to specify the context, use +// QueryRowContext. func (s *Stmt) QueryRow(args ...interface{}) *Row { return s.QueryRowContext(context.Background(), args...) } |