Skip to content

Commit d90074c

Browse files
pushraxnebulabox
authored andcommitted
database/sql: document Stmt lifetime
When prepared on a DB, prepared statement code in database/sql handles everything to keep the prepared statement alive as it moves across the connection pool. Understanding this is an important part of using this API correctly, but it was only documented indirectly via `(*Tx) Prepare*`. Change-Id: Ic8757e0150d59e675d9f0252f6c15aef2cc2e831 GitHub-Last-Rev: 55dba87 GitHub-Pull-Request: golang#29890 Reviewed-on: https://go-review.googlesource.com/c/159077 Reviewed-by: Brad Fitzpatrick <[email protected]> Reviewed-by: Daniel Theophanes <[email protected]>
1 parent cdbe263 commit d90074c

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/database/sql/sql.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,6 +2256,13 @@ var (
22562256

22572257
// Stmt is a prepared statement.
22582258
// A Stmt is safe for concurrent use by multiple goroutines.
2259+
//
2260+
// If a Stmt is prepared on a Tx or Conn, it will be bound to a single
2261+
// underlying connection forever. If the Tx or Conn closes, the Stmt will
2262+
// become unusable and all operations will return an error.
2263+
// If a Stmt is prepared on a DB, it will remain usable for the lifetime of the
2264+
// DB. When the Stmt needs to execute on a new underlying connection, it will
2265+
// prepare itself on the new connection automatically.
22592266
type Stmt struct {
22602267
// Immutable:
22612268
db *DB // where we came from

0 commit comments

Comments
 (0)