]> Cypherpunks repositories - gostls13.git/commitdiff
database/sql: more docs explaining that DB is a pool
authorBrad Fitzpatrick <bradfitz@golang.org>
Mon, 19 May 2014 16:54:47 +0000 (09:54 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Mon, 19 May 2014 16:54:47 +0000 (09:54 -0700)
This is the main point of confusion and the emphasis of
a recent Gophercon talk.

Fixes #5886. (mostly fixed in previous commits)

LGTM=r
R=r
CC=golang-codereviews
https://golang.org/cl/100560043

src/pkg/database/sql/sql.go

index ef54dcdf91de74f70ea0fc3148e6ac5b3199f234..765b80c60a20fe5004d53202a8f20245d7bfd1ab 100644 (file)
@@ -181,7 +181,8 @@ type Scanner interface {
 // defers this error until a Scan.
 var ErrNoRows = errors.New("sql: no rows in result set")
 
-// DB is a database handle. It's safe for concurrent use by multiple
+// DB is a database handle representing a pool of zero or more
+// underlying connections. It's safe for concurrent use by multiple
 // goroutines.
 //
 // The sql package creates and frees connections automatically; it
@@ -420,6 +421,11 @@ var connectionRequestQueueSize = 1000000
 // Open may just validate its arguments without creating a connection
 // to the database. To verify that the data source name is valid, call
 // Ping.
+//
+// The returned DB is safe for concurrent use by multiple goroutines
+// and maintains its own pool of idle connections. Thus, the Open
+// function should be called just once. It is rarely necessary to
+// close a DB.
 func Open(driverName, dataSourceName string) (*DB, error) {
        driveri, ok := drivers[driverName]
        if !ok {
@@ -452,6 +458,9 @@ func (db *DB) Ping() error {
 }
 
 // Close closes the database, releasing any open resources.
+//
+// It is rare to Close a DB, as the DB handle is meant to be
+// long-lived and shared between many goroutines.
 func (db *DB) Close() error {
        db.mu.Lock()
        if db.closed { // Make DB.Close idempotent