exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
// Package sql provides a generic interface around SQL (or SQL-like)
|
|
|
|
|
// databases.
|
2013-03-25 17:38:51 -07:00
|
|
|
//
|
|
|
|
|
// The sql package must be used in conjunction with a database driver.
|
|
|
|
|
// See http://golang.org/s/sqldrivers for a list of drivers.
|
2013-10-24 10:13:23 -07:00
|
|
|
//
|
|
|
|
|
// For more usage examples, see the wiki page at
|
|
|
|
|
// http://golang.org/s/sqlwiki.
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
package sql
|
|
|
|
|
|
|
|
|
|
import (
|
2012-01-19 16:04:26 -08:00
|
|
|
"database/sql/driver"
|
2011-11-01 22:04:37 -04:00
|
|
|
"errors"
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
"fmt"
|
2011-11-01 22:04:37 -04:00
|
|
|
"io"
|
2013-02-20 15:35:27 -08:00
|
|
|
"runtime"
|
2014-10-15 13:10:14 -04:00
|
|
|
"sort"
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
"sync"
|
2015-01-23 20:02:37 +09:00
|
|
|
"sync/atomic"
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var drivers = make(map[string]driver.Driver)
|
|
|
|
|
|
|
|
|
|
// Register makes a database driver available by the provided name.
|
|
|
|
|
// If Register is called twice with the same name or if driver is nil,
|
|
|
|
|
// it panics.
|
|
|
|
|
func Register(name string, driver driver.Driver) {
|
|
|
|
|
if driver == nil {
|
2011-12-15 10:14:57 -08:00
|
|
|
panic("sql: Register driver is nil")
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
if _, dup := drivers[name]; dup {
|
2011-12-15 10:14:57 -08:00
|
|
|
panic("sql: Register called twice for driver " + name)
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
drivers[name] = driver
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-31 09:49:42 -07:00
|
|
|
func unregisterAllDrivers() {
|
|
|
|
|
// For tests.
|
|
|
|
|
drivers = make(map[string]driver.Driver)
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-15 13:10:14 -04:00
|
|
|
// Drivers returns a sorted list of the names of the registered drivers.
|
|
|
|
|
func Drivers() []string {
|
|
|
|
|
var list []string
|
|
|
|
|
for name := range drivers {
|
|
|
|
|
list = append(list, name)
|
|
|
|
|
}
|
|
|
|
|
sort.Strings(list)
|
|
|
|
|
return list
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-17 10:44:35 -08:00
|
|
|
// RawBytes is a byte slice that holds a reference to memory owned by
|
|
|
|
|
// the database itself. After a Scan into a RawBytes, the slice is only
|
|
|
|
|
// valid until the next call to Next, Scan, or Close.
|
|
|
|
|
type RawBytes []byte
|
|
|
|
|
|
2012-01-19 09:27:45 -08:00
|
|
|
// NullString represents a string that may be null.
|
2012-02-10 10:20:49 +11:00
|
|
|
// NullString implements the Scanner interface so
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
// it can be used as a scan destination:
|
|
|
|
|
//
|
2012-01-19 09:27:45 -08:00
|
|
|
// var s NullString
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
// err := db.QueryRow("SELECT name FROM foo WHERE id=?", id).Scan(&s)
|
|
|
|
|
// ...
|
|
|
|
|
// if s.Valid {
|
|
|
|
|
// // use s.String
|
|
|
|
|
// } else {
|
|
|
|
|
// // NULL value
|
|
|
|
|
// }
|
|
|
|
|
//
|
2012-01-19 09:27:45 -08:00
|
|
|
type NullString struct {
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
String string
|
|
|
|
|
Valid bool // Valid is true if String is not NULL
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-10 10:20:49 +11:00
|
|
|
// Scan implements the Scanner interface.
|
|
|
|
|
func (ns *NullString) Scan(value interface{}) error {
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
if value == nil {
|
2012-01-19 09:27:45 -08:00
|
|
|
ns.String, ns.Valid = "", false
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
return nil
|
|
|
|
|
}
|
2012-01-19 09:27:45 -08:00
|
|
|
ns.Valid = true
|
|
|
|
|
return convertAssign(&ns.String, value)
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-20 14:25:28 +11:00
|
|
|
// Value implements the driver Valuer interface.
|
|
|
|
|
func (ns NullString) Value() (driver.Value, error) {
|
2012-01-19 09:27:45 -08:00
|
|
|
if !ns.Valid {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
return ns.String, nil
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
2012-01-25 17:47:32 -08:00
|
|
|
// NullInt64 represents an int64 that may be null.
|
2012-02-10 10:20:49 +11:00
|
|
|
// NullInt64 implements the Scanner interface so
|
2012-01-25 17:47:32 -08:00
|
|
|
// it can be used as a scan destination, similar to NullString.
|
|
|
|
|
type NullInt64 struct {
|
|
|
|
|
Int64 int64
|
|
|
|
|
Valid bool // Valid is true if Int64 is not NULL
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-10 10:20:49 +11:00
|
|
|
// Scan implements the Scanner interface.
|
|
|
|
|
func (n *NullInt64) Scan(value interface{}) error {
|
2012-01-25 17:47:32 -08:00
|
|
|
if value == nil {
|
|
|
|
|
n.Int64, n.Valid = 0, false
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
n.Valid = true
|
|
|
|
|
return convertAssign(&n.Int64, value)
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-20 14:25:28 +11:00
|
|
|
// Value implements the driver Valuer interface.
|
|
|
|
|
func (n NullInt64) Value() (driver.Value, error) {
|
2012-01-25 17:47:32 -08:00
|
|
|
if !n.Valid {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
return n.Int64, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NullFloat64 represents a float64 that may be null.
|
2012-02-10 10:20:49 +11:00
|
|
|
// NullFloat64 implements the Scanner interface so
|
2012-01-25 17:47:32 -08:00
|
|
|
// it can be used as a scan destination, similar to NullString.
|
|
|
|
|
type NullFloat64 struct {
|
|
|
|
|
Float64 float64
|
|
|
|
|
Valid bool // Valid is true if Float64 is not NULL
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-10 10:20:49 +11:00
|
|
|
// Scan implements the Scanner interface.
|
|
|
|
|
func (n *NullFloat64) Scan(value interface{}) error {
|
2012-01-25 17:47:32 -08:00
|
|
|
if value == nil {
|
|
|
|
|
n.Float64, n.Valid = 0, false
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
n.Valid = true
|
|
|
|
|
return convertAssign(&n.Float64, value)
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-20 14:25:28 +11:00
|
|
|
// Value implements the driver Valuer interface.
|
|
|
|
|
func (n NullFloat64) Value() (driver.Value, error) {
|
2012-01-25 17:47:32 -08:00
|
|
|
if !n.Valid {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
return n.Float64, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NullBool represents a bool that may be null.
|
2012-02-10 10:20:49 +11:00
|
|
|
// NullBool implements the Scanner interface so
|
2012-01-25 17:47:32 -08:00
|
|
|
// it can be used as a scan destination, similar to NullString.
|
|
|
|
|
type NullBool struct {
|
|
|
|
|
Bool bool
|
|
|
|
|
Valid bool // Valid is true if Bool is not NULL
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-10 10:20:49 +11:00
|
|
|
// Scan implements the Scanner interface.
|
|
|
|
|
func (n *NullBool) Scan(value interface{}) error {
|
2012-01-25 17:47:32 -08:00
|
|
|
if value == nil {
|
|
|
|
|
n.Bool, n.Valid = false, false
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
n.Valid = true
|
|
|
|
|
return convertAssign(&n.Bool, value)
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-20 14:25:28 +11:00
|
|
|
// Value implements the driver Valuer interface.
|
|
|
|
|
func (n NullBool) Value() (driver.Value, error) {
|
2012-01-25 17:47:32 -08:00
|
|
|
if !n.Valid {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
return n.Bool, nil
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-10 10:20:49 +11:00
|
|
|
// Scanner is an interface used by Scan.
|
|
|
|
|
type Scanner interface {
|
|
|
|
|
// Scan assigns a value from a database driver.
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
//
|
2012-02-10 10:20:49 +11:00
|
|
|
// The src value will be of one of the following restricted
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
// set of types:
|
|
|
|
|
//
|
|
|
|
|
// int64
|
|
|
|
|
// float64
|
|
|
|
|
// bool
|
|
|
|
|
// []byte
|
2012-02-10 10:20:49 +11:00
|
|
|
// string
|
|
|
|
|
// time.Time
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
// nil - for NULL values
|
|
|
|
|
//
|
|
|
|
|
// An error should be returned if the value can not be stored
|
|
|
|
|
// without loss of information.
|
2012-02-10 10:20:49 +11:00
|
|
|
Scan(src interface{}) error
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ErrNoRows is returned by Scan when QueryRow doesn't return a
|
|
|
|
|
// row. In such a case, QueryRow returns a placeholder *Row value that
|
|
|
|
|
// defers this error until a Scan.
|
2011-12-15 10:14:57 -08:00
|
|
|
var ErrNoRows = errors.New("sql: no rows in result set")
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
|
2014-05-19 09:54:47 -07:00
|
|
|
// DB is a database handle representing a pool of zero or more
|
|
|
|
|
// underlying connections. It's safe for concurrent use by multiple
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
// goroutines.
|
2012-03-06 17:44:47 -08:00
|
|
|
//
|
2013-03-18 15:54:22 -07:00
|
|
|
// The sql package creates and frees connections automatically; it
|
|
|
|
|
// also maintains a free pool of idle connections. If the database has
|
|
|
|
|
// a concept of per-connection state, such state can only be reliably
|
|
|
|
|
// observed within a transaction. Once DB.Begin is called, the
|
|
|
|
|
// returned Tx is bound to a single connection. Once Commit or
|
|
|
|
|
// Rollback is called on the transaction, that transaction's
|
|
|
|
|
// connection is returned to DB's idle connection pool. The pool size
|
|
|
|
|
// can be controlled with SetMaxIdleConns.
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
type DB struct {
|
|
|
|
|
driver driver.Driver
|
|
|
|
|
dsn string
|
2015-01-23 20:02:37 +09:00
|
|
|
// numClosed is an atomic counter which represents a total number of
|
|
|
|
|
// closed connections. Stmt.openStmt checks it before cleaning closed
|
|
|
|
|
// connections in Stmt.css.
|
|
|
|
|
numClosed uint64
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
|
2013-08-30 09:27:33 -07:00
|
|
|
mu sync.Mutex // protects following fields
|
2014-08-28 08:49:56 -07:00
|
|
|
freeConn []*driverConn
|
2014-08-28 11:07:29 -07:00
|
|
|
connRequests []chan connRequest
|
2013-08-30 09:27:33 -07:00
|
|
|
numOpen int
|
|
|
|
|
pendingOpens int
|
2013-10-29 16:03:13 -07:00
|
|
|
// Used to signal the need for new connections
|
2013-08-30 09:27:33 -07:00
|
|
|
// a goroutine running connectionOpener() reads on this chan and
|
|
|
|
|
// maybeOpenNewConnections sends on the chan (one send per needed connection)
|
|
|
|
|
// It is closed during db.Close(). The close tells the connectionOpener
|
|
|
|
|
// goroutine to exit.
|
|
|
|
|
openerCh chan struct{}
|
2013-04-03 11:13:40 -07:00
|
|
|
closed bool
|
|
|
|
|
dep map[finalCloser]depSet
|
|
|
|
|
lastPut map[*driverConn]string // stacktrace of last conn's put; debug only
|
|
|
|
|
maxIdle int // zero means defaultMaxIdleConns; negative means 0
|
2013-08-30 09:27:33 -07:00
|
|
|
maxOpen int // <= 0 means unlimited
|
2013-03-14 15:01:45 -07:00
|
|
|
}
|
|
|
|
|
|
2015-03-27 19:45:12 +01:00
|
|
|
// connReuseStrategy determines how (*DB).conn returns database connections.
|
|
|
|
|
type connReuseStrategy uint8
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
// alwaysNewConn forces a new connection to the database.
|
|
|
|
|
alwaysNewConn connReuseStrategy = iota
|
|
|
|
|
// cachedOrNewConn returns a cached connection, if available, else waits
|
|
|
|
|
// for one to become available (if MaxOpenConns has been reached) or
|
|
|
|
|
// creates a new database connection.
|
|
|
|
|
cachedOrNewConn
|
|
|
|
|
)
|
|
|
|
|
|
2013-03-14 15:01:45 -07:00
|
|
|
// driverConn wraps a driver.Conn with a mutex, to
|
|
|
|
|
// be held during all calls into the Conn. (including any calls onto
|
|
|
|
|
// interfaces returned via that Conn, such as calls on Tx, Stmt,
|
|
|
|
|
// Result, Rows)
|
|
|
|
|
type driverConn struct {
|
2013-03-25 16:50:27 -07:00
|
|
|
db *DB
|
|
|
|
|
|
database/sql: fix driver Conn refcounting with prepared statements
The refcounting of driver Conns was completedly busted and
would leak (be held open forever) with any reasonable
load. This was a significant regression from Go 1.0.
The core of this patch is removing one line:
s.db.addDep(dc, s)
A database conn (dc) is a resource that be re-created any time
(but cached for speed) should not be held open forever with a
dependency refcount just because the Stmt (s) is alive (which
typically last for long periods of time, like forever).
The meat of the patch is new tests. In fixing the real issue,
a lot of tests then failed due to the fakedb_test.go's paranoia
about closing a fakeConn while it has open fakeStmts on it. I
could've ignored that, but that's been a problem in the past for
other bugs.
Instead, I now track per-Conn open statements and close them
when the the conn closes. The proper way to do this would've
been making *driverStmt a finalCloser and using the dep mechanism,
but it was much more invasive. Added a TODO instead.
I'd like to give a way for drivers to opt-out of caring about
driver.Stmt closes before a driver.Conn close, but that's a TODO
for the future, and that TODO is added in this CL.
I know this is very late for Go 1.1, but database/sql is
currently nearly useless without this.
I'd like to believe all these database/sql bugs in the past
release cycle are the result of increased usage, number of
drivers, and good feedback from increasingly-capable Go
developers, and not the result of me sucking. It's also hard
with all the real drivers being out-of-tree, so I'm having to
add more and more hooks to fakedb_test.go to simulate things
which real drivers end up doing.
Fixes #5323
R=golang-dev, snaury, gwenn.kahz, google, r
CC=golang-dev
https://golang.org/cl/8836045
2013-04-25 14:45:56 -07:00
|
|
|
sync.Mutex // guards following
|
|
|
|
|
ci driver.Conn
|
|
|
|
|
closed bool
|
|
|
|
|
finalClosed bool // ci.Close has been called
|
|
|
|
|
openStmt map[driver.Stmt]bool
|
2013-04-03 11:13:40 -07:00
|
|
|
|
|
|
|
|
// guarded by db.mu
|
database/sql: fix driver Conn refcounting with prepared statements
The refcounting of driver Conns was completedly busted and
would leak (be held open forever) with any reasonable
load. This was a significant regression from Go 1.0.
The core of this patch is removing one line:
s.db.addDep(dc, s)
A database conn (dc) is a resource that be re-created any time
(but cached for speed) should not be held open forever with a
dependency refcount just because the Stmt (s) is alive (which
typically last for long periods of time, like forever).
The meat of the patch is new tests. In fixing the real issue,
a lot of tests then failed due to the fakedb_test.go's paranoia
about closing a fakeConn while it has open fakeStmts on it. I
could've ignored that, but that's been a problem in the past for
other bugs.
Instead, I now track per-Conn open statements and close them
when the the conn closes. The proper way to do this would've
been making *driverStmt a finalCloser and using the dep mechanism,
but it was much more invasive. Added a TODO instead.
I'd like to give a way for drivers to opt-out of caring about
driver.Stmt closes before a driver.Conn close, but that's a TODO
for the future, and that TODO is added in this CL.
I know this is very late for Go 1.1, but database/sql is
currently nearly useless without this.
I'd like to believe all these database/sql bugs in the past
release cycle are the result of increased usage, number of
drivers, and good feedback from increasingly-capable Go
developers, and not the result of me sucking. It's also hard
with all the real drivers being out-of-tree, so I'm having to
add more and more hooks to fakedb_test.go to simulate things
which real drivers end up doing.
Fixes #5323
R=golang-dev, snaury, gwenn.kahz, google, r
CC=golang-dev
https://golang.org/cl/8836045
2013-04-25 14:45:56 -07:00
|
|
|
inUse bool
|
|
|
|
|
onPut []func() // code (with db.mu held) run when conn is next returned
|
2015-01-23 20:02:37 +09:00
|
|
|
dbmuClosed bool // same as closed, but guarded by db.mu, for removeClosedStmtLocked
|
database/sql: fix driver Conn refcounting with prepared statements
The refcounting of driver Conns was completedly busted and
would leak (be held open forever) with any reasonable
load. This was a significant regression from Go 1.0.
The core of this patch is removing one line:
s.db.addDep(dc, s)
A database conn (dc) is a resource that be re-created any time
(but cached for speed) should not be held open forever with a
dependency refcount just because the Stmt (s) is alive (which
typically last for long periods of time, like forever).
The meat of the patch is new tests. In fixing the real issue,
a lot of tests then failed due to the fakedb_test.go's paranoia
about closing a fakeConn while it has open fakeStmts on it. I
could've ignored that, but that's been a problem in the past for
other bugs.
Instead, I now track per-Conn open statements and close them
when the the conn closes. The proper way to do this would've
been making *driverStmt a finalCloser and using the dep mechanism,
but it was much more invasive. Added a TODO instead.
I'd like to give a way for drivers to opt-out of caring about
driver.Stmt closes before a driver.Conn close, but that's a TODO
for the future, and that TODO is added in this CL.
I know this is very late for Go 1.1, but database/sql is
currently nearly useless without this.
I'd like to believe all these database/sql bugs in the past
release cycle are the result of increased usage, number of
drivers, and good feedback from increasingly-capable Go
developers, and not the result of me sucking. It's also hard
with all the real drivers being out-of-tree, so I'm having to
add more and more hooks to fakedb_test.go to simulate things
which real drivers end up doing.
Fixes #5323
R=golang-dev, snaury, gwenn.kahz, google, r
CC=golang-dev
https://golang.org/cl/8836045
2013-04-25 14:45:56 -07:00
|
|
|
}
|
|
|
|
|
|
2013-05-14 16:35:31 -07:00
|
|
|
func (dc *driverConn) releaseConn(err error) {
|
|
|
|
|
dc.db.putConn(dc, err)
|
|
|
|
|
}
|
|
|
|
|
|
database/sql: fix driver Conn refcounting with prepared statements
The refcounting of driver Conns was completedly busted and
would leak (be held open forever) with any reasonable
load. This was a significant regression from Go 1.0.
The core of this patch is removing one line:
s.db.addDep(dc, s)
A database conn (dc) is a resource that be re-created any time
(but cached for speed) should not be held open forever with a
dependency refcount just because the Stmt (s) is alive (which
typically last for long periods of time, like forever).
The meat of the patch is new tests. In fixing the real issue,
a lot of tests then failed due to the fakedb_test.go's paranoia
about closing a fakeConn while it has open fakeStmts on it. I
could've ignored that, but that's been a problem in the past for
other bugs.
Instead, I now track per-Conn open statements and close them
when the the conn closes. The proper way to do this would've
been making *driverStmt a finalCloser and using the dep mechanism,
but it was much more invasive. Added a TODO instead.
I'd like to give a way for drivers to opt-out of caring about
driver.Stmt closes before a driver.Conn close, but that's a TODO
for the future, and that TODO is added in this CL.
I know this is very late for Go 1.1, but database/sql is
currently nearly useless without this.
I'd like to believe all these database/sql bugs in the past
release cycle are the result of increased usage, number of
drivers, and good feedback from increasingly-capable Go
developers, and not the result of me sucking. It's also hard
with all the real drivers being out-of-tree, so I'm having to
add more and more hooks to fakedb_test.go to simulate things
which real drivers end up doing.
Fixes #5323
R=golang-dev, snaury, gwenn.kahz, google, r
CC=golang-dev
https://golang.org/cl/8836045
2013-04-25 14:45:56 -07:00
|
|
|
func (dc *driverConn) removeOpenStmt(si driver.Stmt) {
|
|
|
|
|
dc.Lock()
|
|
|
|
|
defer dc.Unlock()
|
|
|
|
|
delete(dc.openStmt, si)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (dc *driverConn) prepareLocked(query string) (driver.Stmt, error) {
|
|
|
|
|
si, err := dc.ci.Prepare(query)
|
|
|
|
|
if err == nil {
|
|
|
|
|
// Track each driverConn's open statements, so we can close them
|
|
|
|
|
// before closing the conn.
|
|
|
|
|
//
|
|
|
|
|
// TODO(bradfitz): let drivers opt out of caring about
|
|
|
|
|
// stmt closes if the conn is about to close anyway? For now
|
|
|
|
|
// do the safe thing, in case stmts need to be closed.
|
|
|
|
|
//
|
2013-12-17 11:57:30 -08:00
|
|
|
// TODO(bradfitz): after Go 1.2, closing driver.Stmts
|
database/sql: fix driver Conn refcounting with prepared statements
The refcounting of driver Conns was completedly busted and
would leak (be held open forever) with any reasonable
load. This was a significant regression from Go 1.0.
The core of this patch is removing one line:
s.db.addDep(dc, s)
A database conn (dc) is a resource that be re-created any time
(but cached for speed) should not be held open forever with a
dependency refcount just because the Stmt (s) is alive (which
typically last for long periods of time, like forever).
The meat of the patch is new tests. In fixing the real issue,
a lot of tests then failed due to the fakedb_test.go's paranoia
about closing a fakeConn while it has open fakeStmts on it. I
could've ignored that, but that's been a problem in the past for
other bugs.
Instead, I now track per-Conn open statements and close them
when the the conn closes. The proper way to do this would've
been making *driverStmt a finalCloser and using the dep mechanism,
but it was much more invasive. Added a TODO instead.
I'd like to give a way for drivers to opt-out of caring about
driver.Stmt closes before a driver.Conn close, but that's a TODO
for the future, and that TODO is added in this CL.
I know this is very late for Go 1.1, but database/sql is
currently nearly useless without this.
I'd like to believe all these database/sql bugs in the past
release cycle are the result of increased usage, number of
drivers, and good feedback from increasingly-capable Go
developers, and not the result of me sucking. It's also hard
with all the real drivers being out-of-tree, so I'm having to
add more and more hooks to fakedb_test.go to simulate things
which real drivers end up doing.
Fixes #5323
R=golang-dev, snaury, gwenn.kahz, google, r
CC=golang-dev
https://golang.org/cl/8836045
2013-04-25 14:45:56 -07:00
|
|
|
// should be moved to driverStmt, using unique
|
|
|
|
|
// *driverStmts everywhere (including from
|
|
|
|
|
// *Stmt.connStmt, instead of returning a
|
|
|
|
|
// driver.Stmt), using driverStmt as a pointer
|
|
|
|
|
// everywhere, and making it a finalCloser.
|
|
|
|
|
if dc.openStmt == nil {
|
|
|
|
|
dc.openStmt = make(map[driver.Stmt]bool)
|
|
|
|
|
}
|
|
|
|
|
dc.openStmt[si] = true
|
|
|
|
|
}
|
|
|
|
|
return si, err
|
2013-03-25 16:50:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// the dc.db's Mutex is held.
|
2013-08-30 09:27:33 -07:00
|
|
|
func (dc *driverConn) closeDBLocked() func() error {
|
2013-03-25 16:50:27 -07:00
|
|
|
dc.Lock()
|
2013-08-30 09:27:33 -07:00
|
|
|
defer dc.Unlock()
|
2013-03-25 16:50:27 -07:00
|
|
|
if dc.closed {
|
2013-08-30 09:27:33 -07:00
|
|
|
return func() error { return errors.New("sql: duplicate driverConn close") }
|
2013-03-25 16:50:27 -07:00
|
|
|
}
|
|
|
|
|
dc.closed = true
|
2013-08-30 09:27:33 -07:00
|
|
|
return dc.db.removeDepLocked(dc, dc)
|
2013-03-25 16:50:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (dc *driverConn) Close() error {
|
|
|
|
|
dc.Lock()
|
|
|
|
|
if dc.closed {
|
|
|
|
|
dc.Unlock()
|
|
|
|
|
return errors.New("sql: duplicate driverConn close")
|
|
|
|
|
}
|
|
|
|
|
dc.closed = true
|
|
|
|
|
dc.Unlock() // not defer; removeDep finalClose calls may need to lock
|
database/sql: fix driver Conn refcounting with prepared statements
The refcounting of driver Conns was completedly busted and
would leak (be held open forever) with any reasonable
load. This was a significant regression from Go 1.0.
The core of this patch is removing one line:
s.db.addDep(dc, s)
A database conn (dc) is a resource that be re-created any time
(but cached for speed) should not be held open forever with a
dependency refcount just because the Stmt (s) is alive (which
typically last for long periods of time, like forever).
The meat of the patch is new tests. In fixing the real issue,
a lot of tests then failed due to the fakedb_test.go's paranoia
about closing a fakeConn while it has open fakeStmts on it. I
could've ignored that, but that's been a problem in the past for
other bugs.
Instead, I now track per-Conn open statements and close them
when the the conn closes. The proper way to do this would've
been making *driverStmt a finalCloser and using the dep mechanism,
but it was much more invasive. Added a TODO instead.
I'd like to give a way for drivers to opt-out of caring about
driver.Stmt closes before a driver.Conn close, but that's a TODO
for the future, and that TODO is added in this CL.
I know this is very late for Go 1.1, but database/sql is
currently nearly useless without this.
I'd like to believe all these database/sql bugs in the past
release cycle are the result of increased usage, number of
drivers, and good feedback from increasingly-capable Go
developers, and not the result of me sucking. It's also hard
with all the real drivers being out-of-tree, so I'm having to
add more and more hooks to fakedb_test.go to simulate things
which real drivers end up doing.
Fixes #5323
R=golang-dev, snaury, gwenn.kahz, google, r
CC=golang-dev
https://golang.org/cl/8836045
2013-04-25 14:45:56 -07:00
|
|
|
|
|
|
|
|
// And now updates that require holding dc.mu.Lock.
|
|
|
|
|
dc.db.mu.Lock()
|
|
|
|
|
dc.dbmuClosed = true
|
|
|
|
|
fn := dc.db.removeDepLocked(dc, dc)
|
|
|
|
|
dc.db.mu.Unlock()
|
|
|
|
|
return fn()
|
2013-03-25 16:50:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (dc *driverConn) finalClose() error {
|
|
|
|
|
dc.Lock()
|
database/sql: fix driver Conn refcounting with prepared statements
The refcounting of driver Conns was completedly busted and
would leak (be held open forever) with any reasonable
load. This was a significant regression from Go 1.0.
The core of this patch is removing one line:
s.db.addDep(dc, s)
A database conn (dc) is a resource that be re-created any time
(but cached for speed) should not be held open forever with a
dependency refcount just because the Stmt (s) is alive (which
typically last for long periods of time, like forever).
The meat of the patch is new tests. In fixing the real issue,
a lot of tests then failed due to the fakedb_test.go's paranoia
about closing a fakeConn while it has open fakeStmts on it. I
could've ignored that, but that's been a problem in the past for
other bugs.
Instead, I now track per-Conn open statements and close them
when the the conn closes. The proper way to do this would've
been making *driverStmt a finalCloser and using the dep mechanism,
but it was much more invasive. Added a TODO instead.
I'd like to give a way for drivers to opt-out of caring about
driver.Stmt closes before a driver.Conn close, but that's a TODO
for the future, and that TODO is added in this CL.
I know this is very late for Go 1.1, but database/sql is
currently nearly useless without this.
I'd like to believe all these database/sql bugs in the past
release cycle are the result of increased usage, number of
drivers, and good feedback from increasingly-capable Go
developers, and not the result of me sucking. It's also hard
with all the real drivers being out-of-tree, so I'm having to
add more and more hooks to fakedb_test.go to simulate things
which real drivers end up doing.
Fixes #5323
R=golang-dev, snaury, gwenn.kahz, google, r
CC=golang-dev
https://golang.org/cl/8836045
2013-04-25 14:45:56 -07:00
|
|
|
|
|
|
|
|
for si := range dc.openStmt {
|
|
|
|
|
si.Close()
|
|
|
|
|
}
|
|
|
|
|
dc.openStmt = nil
|
|
|
|
|
|
2013-03-25 16:50:27 -07:00
|
|
|
err := dc.ci.Close()
|
|
|
|
|
dc.ci = nil
|
database/sql: fix driver Conn refcounting with prepared statements
The refcounting of driver Conns was completedly busted and
would leak (be held open forever) with any reasonable
load. This was a significant regression from Go 1.0.
The core of this patch is removing one line:
s.db.addDep(dc, s)
A database conn (dc) is a resource that be re-created any time
(but cached for speed) should not be held open forever with a
dependency refcount just because the Stmt (s) is alive (which
typically last for long periods of time, like forever).
The meat of the patch is new tests. In fixing the real issue,
a lot of tests then failed due to the fakedb_test.go's paranoia
about closing a fakeConn while it has open fakeStmts on it. I
could've ignored that, but that's been a problem in the past for
other bugs.
Instead, I now track per-Conn open statements and close them
when the the conn closes. The proper way to do this would've
been making *driverStmt a finalCloser and using the dep mechanism,
but it was much more invasive. Added a TODO instead.
I'd like to give a way for drivers to opt-out of caring about
driver.Stmt closes before a driver.Conn close, but that's a TODO
for the future, and that TODO is added in this CL.
I know this is very late for Go 1.1, but database/sql is
currently nearly useless without this.
I'd like to believe all these database/sql bugs in the past
release cycle are the result of increased usage, number of
drivers, and good feedback from increasingly-capable Go
developers, and not the result of me sucking. It's also hard
with all the real drivers being out-of-tree, so I'm having to
add more and more hooks to fakedb_test.go to simulate things
which real drivers end up doing.
Fixes #5323
R=golang-dev, snaury, gwenn.kahz, google, r
CC=golang-dev
https://golang.org/cl/8836045
2013-04-25 14:45:56 -07:00
|
|
|
dc.finalClosed = true
|
2013-08-29 17:26:00 -07:00
|
|
|
dc.Unlock()
|
2013-08-30 09:27:33 -07:00
|
|
|
|
|
|
|
|
dc.db.mu.Lock()
|
|
|
|
|
dc.db.numOpen--
|
|
|
|
|
dc.db.maybeOpenNewConnections()
|
|
|
|
|
dc.db.mu.Unlock()
|
|
|
|
|
|
2015-01-23 20:02:37 +09:00
|
|
|
atomic.AddUint64(&dc.db.numClosed, 1)
|
2013-03-25 16:50:27 -07:00
|
|
|
return err
|
2013-03-14 15:01:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// driverStmt associates a driver.Stmt with the
|
|
|
|
|
// *driverConn from which it came, so the driverConn's lock can be
|
|
|
|
|
// held during calls.
|
|
|
|
|
type driverStmt struct {
|
|
|
|
|
sync.Locker // the *driverConn
|
|
|
|
|
si driver.Stmt
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ds *driverStmt) Close() error {
|
|
|
|
|
ds.Lock()
|
|
|
|
|
defer ds.Unlock()
|
|
|
|
|
return ds.si.Close()
|
2013-02-20 15:35:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// depSet is a finalCloser's outstanding dependencies
|
|
|
|
|
type depSet map[interface{}]bool // set of true bools
|
|
|
|
|
|
database/sql: fix driver Conn refcounting with prepared statements
The refcounting of driver Conns was completedly busted and
would leak (be held open forever) with any reasonable
load. This was a significant regression from Go 1.0.
The core of this patch is removing one line:
s.db.addDep(dc, s)
A database conn (dc) is a resource that be re-created any time
(but cached for speed) should not be held open forever with a
dependency refcount just because the Stmt (s) is alive (which
typically last for long periods of time, like forever).
The meat of the patch is new tests. In fixing the real issue,
a lot of tests then failed due to the fakedb_test.go's paranoia
about closing a fakeConn while it has open fakeStmts on it. I
could've ignored that, but that's been a problem in the past for
other bugs.
Instead, I now track per-Conn open statements and close them
when the the conn closes. The proper way to do this would've
been making *driverStmt a finalCloser and using the dep mechanism,
but it was much more invasive. Added a TODO instead.
I'd like to give a way for drivers to opt-out of caring about
driver.Stmt closes before a driver.Conn close, but that's a TODO
for the future, and that TODO is added in this CL.
I know this is very late for Go 1.1, but database/sql is
currently nearly useless without this.
I'd like to believe all these database/sql bugs in the past
release cycle are the result of increased usage, number of
drivers, and good feedback from increasingly-capable Go
developers, and not the result of me sucking. It's also hard
with all the real drivers being out-of-tree, so I'm having to
add more and more hooks to fakedb_test.go to simulate things
which real drivers end up doing.
Fixes #5323
R=golang-dev, snaury, gwenn.kahz, google, r
CC=golang-dev
https://golang.org/cl/8836045
2013-04-25 14:45:56 -07:00
|
|
|
// The finalCloser interface is used by (*DB).addDep and related
|
|
|
|
|
// dependency reference counting.
|
2013-02-20 15:35:27 -08:00
|
|
|
type finalCloser interface {
|
|
|
|
|
// finalClose is called when the reference count of an object
|
|
|
|
|
// goes to zero. (*DB).mu is not held while calling it.
|
|
|
|
|
finalClose() error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// addDep notes that x now depends on dep, and x's finalClose won't be
|
|
|
|
|
// called until all of x's dependencies are removed with removeDep.
|
|
|
|
|
func (db *DB) addDep(x finalCloser, dep interface{}) {
|
|
|
|
|
//println(fmt.Sprintf("addDep(%T %p, %T %p)", x, x, dep, dep))
|
|
|
|
|
db.mu.Lock()
|
|
|
|
|
defer db.mu.Unlock()
|
2013-03-25 16:50:27 -07:00
|
|
|
db.addDepLocked(x, dep)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (db *DB) addDepLocked(x finalCloser, dep interface{}) {
|
2013-02-20 15:35:27 -08:00
|
|
|
if db.dep == nil {
|
|
|
|
|
db.dep = make(map[finalCloser]depSet)
|
|
|
|
|
}
|
|
|
|
|
xdep := db.dep[x]
|
|
|
|
|
if xdep == nil {
|
|
|
|
|
xdep = make(depSet)
|
|
|
|
|
db.dep[x] = xdep
|
|
|
|
|
}
|
|
|
|
|
xdep[dep] = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// removeDep notes that x no longer depends on dep.
|
|
|
|
|
// If x still has dependencies, nil is returned.
|
|
|
|
|
// If x no longer has any dependencies, its finalClose method will be
|
|
|
|
|
// called and its error value will be returned.
|
|
|
|
|
func (db *DB) removeDep(x finalCloser, dep interface{}) error {
|
2013-03-25 16:50:27 -07:00
|
|
|
db.mu.Lock()
|
|
|
|
|
fn := db.removeDepLocked(x, dep)
|
|
|
|
|
db.mu.Unlock()
|
|
|
|
|
return fn()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (db *DB) removeDepLocked(x finalCloser, dep interface{}) func() error {
|
2013-02-20 15:35:27 -08:00
|
|
|
//println(fmt.Sprintf("removeDep(%T %p, %T %p)", x, x, dep, dep))
|
|
|
|
|
|
2013-05-21 14:58:08 -07:00
|
|
|
xdep, ok := db.dep[x]
|
|
|
|
|
if !ok {
|
|
|
|
|
panic(fmt.Sprintf("unpaired removeDep: no deps for %T", x))
|
2013-02-20 15:35:27 -08:00
|
|
|
}
|
|
|
|
|
|
2013-05-21 14:58:08 -07:00
|
|
|
l0 := len(xdep)
|
|
|
|
|
delete(xdep, dep)
|
|
|
|
|
|
|
|
|
|
switch len(xdep) {
|
|
|
|
|
case l0:
|
|
|
|
|
// Nothing removed. Shouldn't happen.
|
|
|
|
|
panic(fmt.Sprintf("unpaired removeDep: no %T dep on %T", dep, x))
|
|
|
|
|
case 0:
|
|
|
|
|
// No more dependencies.
|
|
|
|
|
delete(db.dep, x)
|
|
|
|
|
return x.finalClose
|
|
|
|
|
default:
|
|
|
|
|
// Dependencies remain.
|
2013-03-25 16:50:27 -07:00
|
|
|
return func() error { return nil }
|
|
|
|
|
}
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
2013-08-30 09:27:33 -07:00
|
|
|
// This is the size of the connectionOpener request chan (dn.openerCh).
|
|
|
|
|
// This value should be larger than the maximum typical value
|
|
|
|
|
// used for db.maxOpen. If maxOpen is significantly larger than
|
|
|
|
|
// connectionRequestQueueSize then it is possible for ALL calls into the *DB
|
2014-04-29 12:44:40 -04:00
|
|
|
// to block until the connectionOpener can satisfy the backlog of requests.
|
2013-08-30 09:27:33 -07:00
|
|
|
var connectionRequestQueueSize = 1000000
|
|
|
|
|
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
// Open opens a database specified by its database driver name and a
|
|
|
|
|
// driver-specific data source name, usually consisting of at least a
|
|
|
|
|
// database name and connection information.
|
|
|
|
|
//
|
|
|
|
|
// Most users will open a database via a driver-specific connection
|
2013-03-25 17:38:51 -07:00
|
|
|
// helper function that returns a *DB. No database drivers are included
|
|
|
|
|
// in the Go standard library. See http://golang.org/s/sqldrivers for
|
|
|
|
|
// a list of third-party drivers.
|
2013-03-14 14:06:46 -07:00
|
|
|
//
|
|
|
|
|
// Open may just validate its arguments without creating a connection
|
|
|
|
|
// to the database. To verify that the data source name is valid, call
|
|
|
|
|
// Ping.
|
2014-05-19 09:54:47 -07:00
|
|
|
//
|
|
|
|
|
// 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.
|
2011-11-01 22:04:37 -04:00
|
|
|
func Open(driverName, dataSourceName string) (*DB, error) {
|
2013-02-20 15:35:27 -08:00
|
|
|
driveri, ok := drivers[driverName]
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
if !ok {
|
2011-12-15 10:14:57 -08:00
|
|
|
return nil, fmt.Errorf("sql: unknown driver %q (forgotten import?)", driverName)
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
2013-02-20 15:35:27 -08:00
|
|
|
db := &DB{
|
2013-08-30 09:27:33 -07:00
|
|
|
driver: driveri,
|
|
|
|
|
dsn: dataSourceName,
|
|
|
|
|
openerCh: make(chan struct{}, connectionRequestQueueSize),
|
|
|
|
|
lastPut: make(map[*driverConn]string),
|
|
|
|
|
}
|
|
|
|
|
go db.connectionOpener()
|
2013-02-20 15:35:27 -08:00
|
|
|
return db, nil
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
2013-03-14 14:06:46 -07:00
|
|
|
// Ping verifies a connection to the database is still alive,
|
|
|
|
|
// establishing a connection if necessary.
|
|
|
|
|
func (db *DB) Ping() error {
|
|
|
|
|
// TODO(bradfitz): give drivers an optional hook to implement
|
|
|
|
|
// this in a more efficient or more reliable way, if they
|
|
|
|
|
// have one.
|
2015-03-27 19:45:12 +01:00
|
|
|
dc, err := db.conn(cachedOrNewConn)
|
2013-03-14 14:06:46 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2013-03-14 15:01:45 -07:00
|
|
|
db.putConn(dc, nil)
|
2013-03-14 14:06:46 -07:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 10:48:26 -08:00
|
|
|
// Close closes the database, releasing any open resources.
|
2014-05-19 09:54:47 -07:00
|
|
|
//
|
|
|
|
|
// It is rare to Close a DB, as the DB handle is meant to be
|
|
|
|
|
// long-lived and shared between many goroutines.
|
2011-11-14 10:48:26 -08:00
|
|
|
func (db *DB) Close() error {
|
|
|
|
|
db.mu.Lock()
|
2013-08-30 09:27:33 -07:00
|
|
|
if db.closed { // Make DB.Close idempotent
|
|
|
|
|
db.mu.Unlock()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
close(db.openerCh)
|
2011-11-14 10:48:26 -08:00
|
|
|
var err error
|
2014-08-28 08:49:56 -07:00
|
|
|
fns := make([]func() error, 0, len(db.freeConn))
|
|
|
|
|
for _, dc := range db.freeConn {
|
2013-08-30 09:27:33 -07:00
|
|
|
fns = append(fns, dc.closeDBLocked())
|
|
|
|
|
}
|
2014-08-28 08:49:56 -07:00
|
|
|
db.freeConn = nil
|
2013-08-30 09:27:33 -07:00
|
|
|
db.closed = true
|
2014-08-28 08:49:56 -07:00
|
|
|
for _, req := range db.connRequests {
|
2013-08-30 09:27:33 -07:00
|
|
|
close(req)
|
|
|
|
|
}
|
|
|
|
|
db.mu.Unlock()
|
|
|
|
|
for _, fn := range fns {
|
|
|
|
|
err1 := fn()
|
2011-11-14 10:48:26 -08:00
|
|
|
if err1 != nil {
|
|
|
|
|
err = err1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-18 15:33:04 -07:00
|
|
|
const defaultMaxIdleConns = 2
|
|
|
|
|
|
|
|
|
|
func (db *DB) maxIdleConnsLocked() int {
|
|
|
|
|
n := db.maxIdle
|
|
|
|
|
switch {
|
|
|
|
|
case n == 0:
|
|
|
|
|
// TODO(bradfitz): ask driver, if supported, for its default preference
|
|
|
|
|
return defaultMaxIdleConns
|
|
|
|
|
case n < 0:
|
|
|
|
|
return 0
|
|
|
|
|
default:
|
|
|
|
|
return n
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetMaxIdleConns sets the maximum number of connections in the idle
|
|
|
|
|
// connection pool.
|
|
|
|
|
//
|
2013-08-30 09:27:33 -07:00
|
|
|
// If MaxOpenConns is greater than 0 but less than the new MaxIdleConns
|
|
|
|
|
// then the new MaxIdleConns will be reduced to match the MaxOpenConns limit
|
|
|
|
|
//
|
2013-03-18 15:33:04 -07:00
|
|
|
// If n <= 0, no idle connections are retained.
|
|
|
|
|
func (db *DB) SetMaxIdleConns(n int) {
|
|
|
|
|
db.mu.Lock()
|
|
|
|
|
if n > 0 {
|
|
|
|
|
db.maxIdle = n
|
|
|
|
|
} else {
|
|
|
|
|
// No idle connections.
|
|
|
|
|
db.maxIdle = -1
|
|
|
|
|
}
|
2013-08-30 09:27:33 -07:00
|
|
|
// Make sure maxIdle doesn't exceed maxOpen
|
|
|
|
|
if db.maxOpen > 0 && db.maxIdleConnsLocked() > db.maxOpen {
|
|
|
|
|
db.maxIdle = db.maxOpen
|
|
|
|
|
}
|
2013-10-16 09:17:25 -07:00
|
|
|
var closing []*driverConn
|
2014-08-28 08:49:56 -07:00
|
|
|
idleCount := len(db.freeConn)
|
|
|
|
|
maxIdle := db.maxIdleConnsLocked()
|
|
|
|
|
if idleCount > maxIdle {
|
|
|
|
|
closing = db.freeConn[maxIdle:]
|
|
|
|
|
db.freeConn = db.freeConn[:maxIdle]
|
2013-10-16 09:17:25 -07:00
|
|
|
}
|
|
|
|
|
db.mu.Unlock()
|
|
|
|
|
for _, c := range closing {
|
|
|
|
|
c.Close()
|
2013-03-18 15:33:04 -07:00
|
|
|
}
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
2013-08-30 09:27:33 -07:00
|
|
|
// SetMaxOpenConns sets the maximum number of open connections to the database.
|
|
|
|
|
//
|
|
|
|
|
// If MaxIdleConns is greater than 0 and the new MaxOpenConns is less than
|
|
|
|
|
// MaxIdleConns, then MaxIdleConns will be reduced to match the new
|
|
|
|
|
// MaxOpenConns limit
|
|
|
|
|
//
|
|
|
|
|
// If n <= 0, then there is no limit on the number of open connections.
|
|
|
|
|
// The default is 0 (unlimited).
|
|
|
|
|
func (db *DB) SetMaxOpenConns(n int) {
|
|
|
|
|
db.mu.Lock()
|
|
|
|
|
db.maxOpen = n
|
|
|
|
|
if n < 0 {
|
|
|
|
|
db.maxOpen = 0
|
|
|
|
|
}
|
|
|
|
|
syncMaxIdle := db.maxOpen > 0 && db.maxIdleConnsLocked() > db.maxOpen
|
|
|
|
|
db.mu.Unlock()
|
|
|
|
|
if syncMaxIdle {
|
|
|
|
|
db.SetMaxIdleConns(n)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-23 18:23:53 +03:00
|
|
|
// DBStats contains database statistics.
|
|
|
|
|
type DBStats struct {
|
|
|
|
|
// OpenConnections is the number of open connections to the database.
|
|
|
|
|
OpenConnections int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stats returns database statistics.
|
|
|
|
|
func (db *DB) Stats() DBStats {
|
|
|
|
|
db.mu.Lock()
|
|
|
|
|
stats := DBStats{
|
|
|
|
|
OpenConnections: db.numOpen,
|
|
|
|
|
}
|
|
|
|
|
db.mu.Unlock()
|
|
|
|
|
return stats
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-30 09:27:33 -07:00
|
|
|
// Assumes db.mu is locked.
|
|
|
|
|
// If there are connRequests and the connection limit hasn't been reached,
|
|
|
|
|
// then tell the connectionOpener to open new connections.
|
|
|
|
|
func (db *DB) maybeOpenNewConnections() {
|
2014-08-28 08:49:56 -07:00
|
|
|
numRequests := len(db.connRequests) - db.pendingOpens
|
2013-08-30 09:27:33 -07:00
|
|
|
if db.maxOpen > 0 {
|
|
|
|
|
numCanOpen := db.maxOpen - (db.numOpen + db.pendingOpens)
|
|
|
|
|
if numRequests > numCanOpen {
|
|
|
|
|
numRequests = numCanOpen
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for numRequests > 0 {
|
|
|
|
|
db.pendingOpens++
|
|
|
|
|
numRequests--
|
|
|
|
|
db.openerCh <- struct{}{}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-27 08:59:02 -08:00
|
|
|
// Runs in a separate goroutine, opens new connections when requested.
|
2013-08-30 09:27:33 -07:00
|
|
|
func (db *DB) connectionOpener() {
|
2014-07-16 16:29:51 -07:00
|
|
|
for range db.openerCh {
|
2013-08-30 09:27:33 -07:00
|
|
|
db.openNewConnection()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Open one new connection
|
|
|
|
|
func (db *DB) openNewConnection() {
|
|
|
|
|
ci, err := db.driver.Open(db.dsn)
|
|
|
|
|
db.mu.Lock()
|
|
|
|
|
defer db.mu.Unlock()
|
|
|
|
|
if db.closed {
|
|
|
|
|
if err == nil {
|
|
|
|
|
ci.Close()
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
db.pendingOpens--
|
|
|
|
|
if err != nil {
|
|
|
|
|
db.putConnDBLocked(nil, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
dc := &driverConn{
|
|
|
|
|
db: db,
|
|
|
|
|
ci: ci,
|
|
|
|
|
}
|
2013-10-16 09:22:57 -07:00
|
|
|
if db.putConnDBLocked(dc, err) {
|
|
|
|
|
db.addDepLocked(dc, dc)
|
|
|
|
|
db.numOpen++
|
|
|
|
|
} else {
|
|
|
|
|
ci.Close()
|
|
|
|
|
}
|
2013-08-30 09:27:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// connRequest represents one request for a new connection
|
|
|
|
|
// When there are no idle connections available, DB.conn will create
|
|
|
|
|
// a new connRequest and put it on the db.connRequests list.
|
2014-08-28 08:49:56 -07:00
|
|
|
type connRequest struct {
|
|
|
|
|
conn *driverConn
|
|
|
|
|
err error
|
|
|
|
|
}
|
2013-08-30 09:27:33 -07:00
|
|
|
|
|
|
|
|
var errDBClosed = errors.New("sql: database is closed")
|
|
|
|
|
|
2015-03-27 19:45:12 +01:00
|
|
|
// conn returns a newly-opened or cached *driverConn.
|
|
|
|
|
func (db *DB) conn(strategy connReuseStrategy) (*driverConn, error) {
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
db.mu.Lock()
|
2011-11-14 10:48:26 -08:00
|
|
|
if db.closed {
|
2011-12-12 13:56:56 -08:00
|
|
|
db.mu.Unlock()
|
2013-08-30 09:27:33 -07:00
|
|
|
return nil, errDBClosed
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-27 19:45:12 +01:00
|
|
|
// Prefer a free connection, if possible.
|
|
|
|
|
numFree := len(db.freeConn)
|
|
|
|
|
if strategy == cachedOrNewConn && numFree > 0 {
|
|
|
|
|
conn := db.freeConn[0]
|
|
|
|
|
copy(db.freeConn, db.freeConn[1:])
|
|
|
|
|
db.freeConn = db.freeConn[:numFree-1]
|
|
|
|
|
conn.inUse = true
|
|
|
|
|
db.mu.Unlock()
|
|
|
|
|
return conn, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Out of free connections or we were asked not to use one. If we're not
|
|
|
|
|
// allowed to open any more connections, make a request and wait.
|
|
|
|
|
if db.maxOpen > 0 && db.numOpen >= db.maxOpen {
|
2013-08-30 09:27:33 -07:00
|
|
|
// Make the connRequest channel. It's buffered so that the
|
|
|
|
|
// connectionOpener doesn't block while waiting for the req to be read.
|
2014-08-28 11:07:29 -07:00
|
|
|
req := make(chan connRequest, 1)
|
2014-08-28 08:49:56 -07:00
|
|
|
db.connRequests = append(db.connRequests, req)
|
2013-08-30 09:27:33 -07:00
|
|
|
db.mu.Unlock()
|
2014-08-28 08:49:56 -07:00
|
|
|
ret := <-req
|
|
|
|
|
return ret.conn, ret.err
|
2011-11-14 10:48:26 -08:00
|
|
|
}
|
2013-08-30 09:27:33 -07:00
|
|
|
|
2014-05-07 11:54:29 -07:00
|
|
|
db.numOpen++ // optimistically
|
2013-08-30 09:27:33 -07:00
|
|
|
db.mu.Unlock()
|
2013-03-14 15:01:45 -07:00
|
|
|
ci, err := db.driver.Open(db.dsn)
|
|
|
|
|
if err != nil {
|
2014-05-07 11:54:29 -07:00
|
|
|
db.mu.Lock()
|
|
|
|
|
db.numOpen-- // correct for earlier optimism
|
|
|
|
|
db.mu.Unlock()
|
2013-03-14 15:01:45 -07:00
|
|
|
return nil, err
|
2013-02-20 15:35:27 -08:00
|
|
|
}
|
2013-08-30 09:27:33 -07:00
|
|
|
db.mu.Lock()
|
2013-03-25 16:50:27 -07:00
|
|
|
dc := &driverConn{
|
|
|
|
|
db: db,
|
|
|
|
|
ci: ci,
|
|
|
|
|
}
|
|
|
|
|
db.addDepLocked(dc, dc)
|
2013-04-03 11:13:40 -07:00
|
|
|
dc.inUse = true
|
2013-03-14 15:01:45 -07:00
|
|
|
db.mu.Unlock()
|
|
|
|
|
return dc, nil
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
database/sql: fix driver Conn refcounting with prepared statements
The refcounting of driver Conns was completedly busted and
would leak (be held open forever) with any reasonable
load. This was a significant regression from Go 1.0.
The core of this patch is removing one line:
s.db.addDep(dc, s)
A database conn (dc) is a resource that be re-created any time
(but cached for speed) should not be held open forever with a
dependency refcount just because the Stmt (s) is alive (which
typically last for long periods of time, like forever).
The meat of the patch is new tests. In fixing the real issue,
a lot of tests then failed due to the fakedb_test.go's paranoia
about closing a fakeConn while it has open fakeStmts on it. I
could've ignored that, but that's been a problem in the past for
other bugs.
Instead, I now track per-Conn open statements and close them
when the the conn closes. The proper way to do this would've
been making *driverStmt a finalCloser and using the dep mechanism,
but it was much more invasive. Added a TODO instead.
I'd like to give a way for drivers to opt-out of caring about
driver.Stmt closes before a driver.Conn close, but that's a TODO
for the future, and that TODO is added in this CL.
I know this is very late for Go 1.1, but database/sql is
currently nearly useless without this.
I'd like to believe all these database/sql bugs in the past
release cycle are the result of increased usage, number of
drivers, and good feedback from increasingly-capable Go
developers, and not the result of me sucking. It's also hard
with all the real drivers being out-of-tree, so I'm having to
add more and more hooks to fakedb_test.go to simulate things
which real drivers end up doing.
Fixes #5323
R=golang-dev, snaury, gwenn.kahz, google, r
CC=golang-dev
https://golang.org/cl/8836045
2013-04-25 14:45:56 -07:00
|
|
|
var (
|
|
|
|
|
errConnClosed = errors.New("database/sql: internal sentinel error: conn is closed")
|
|
|
|
|
errConnBusy = errors.New("database/sql: internal sentinel error: conn is busy")
|
|
|
|
|
)
|
|
|
|
|
|
2012-03-10 10:00:02 -08:00
|
|
|
// putConnHook is a hook for testing.
|
2013-03-14 15:01:45 -07:00
|
|
|
var putConnHook func(*DB, *driverConn)
|
2012-03-10 10:00:02 -08:00
|
|
|
|
2013-02-20 15:35:27 -08:00
|
|
|
// noteUnusedDriverStatement notes that si is no longer used and should
|
|
|
|
|
// be closed whenever possible (when c is next not in use), unless c is
|
|
|
|
|
// already closed.
|
2013-03-14 15:01:45 -07:00
|
|
|
func (db *DB) noteUnusedDriverStatement(c *driverConn, si driver.Stmt) {
|
2013-02-20 15:35:27 -08:00
|
|
|
db.mu.Lock()
|
|
|
|
|
defer db.mu.Unlock()
|
2013-04-03 11:13:40 -07:00
|
|
|
if c.inUse {
|
|
|
|
|
c.onPut = append(c.onPut, func() {
|
2013-02-20 15:35:27 -08:00
|
|
|
si.Close()
|
|
|
|
|
})
|
|
|
|
|
} else {
|
database/sql: fix driver Conn refcounting with prepared statements
The refcounting of driver Conns was completedly busted and
would leak (be held open forever) with any reasonable
load. This was a significant regression from Go 1.0.
The core of this patch is removing one line:
s.db.addDep(dc, s)
A database conn (dc) is a resource that be re-created any time
(but cached for speed) should not be held open forever with a
dependency refcount just because the Stmt (s) is alive (which
typically last for long periods of time, like forever).
The meat of the patch is new tests. In fixing the real issue,
a lot of tests then failed due to the fakedb_test.go's paranoia
about closing a fakeConn while it has open fakeStmts on it. I
could've ignored that, but that's been a problem in the past for
other bugs.
Instead, I now track per-Conn open statements and close them
when the the conn closes. The proper way to do this would've
been making *driverStmt a finalCloser and using the dep mechanism,
but it was much more invasive. Added a TODO instead.
I'd like to give a way for drivers to opt-out of caring about
driver.Stmt closes before a driver.Conn close, but that's a TODO
for the future, and that TODO is added in this CL.
I know this is very late for Go 1.1, but database/sql is
currently nearly useless without this.
I'd like to believe all these database/sql bugs in the past
release cycle are the result of increased usage, number of
drivers, and good feedback from increasingly-capable Go
developers, and not the result of me sucking. It's also hard
with all the real drivers being out-of-tree, so I'm having to
add more and more hooks to fakedb_test.go to simulate things
which real drivers end up doing.
Fixes #5323
R=golang-dev, snaury, gwenn.kahz, google, r
CC=golang-dev
https://golang.org/cl/8836045
2013-04-25 14:45:56 -07:00
|
|
|
c.Lock()
|
|
|
|
|
defer c.Unlock()
|
|
|
|
|
if !c.finalClosed {
|
|
|
|
|
si.Close()
|
|
|
|
|
}
|
2013-02-20 15:35:27 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// debugGetPut determines whether getConn & putConn calls' stack traces
|
|
|
|
|
// are returned for more verbose crashes.
|
|
|
|
|
const debugGetPut = false
|
|
|
|
|
|
2012-03-08 10:09:52 -08:00
|
|
|
// putConn adds a connection to the db's free pool.
|
2012-12-19 03:04:09 +08:00
|
|
|
// err is optionally the last error that occurred on this connection.
|
2013-03-14 15:01:45 -07:00
|
|
|
func (db *DB) putConn(dc *driverConn, err error) {
|
2013-02-20 15:35:27 -08:00
|
|
|
db.mu.Lock()
|
2013-04-03 11:13:40 -07:00
|
|
|
if !dc.inUse {
|
2013-02-20 15:35:27 -08:00
|
|
|
if debugGetPut {
|
2013-03-14 15:01:45 -07:00
|
|
|
fmt.Printf("putConn(%v) DUPLICATE was: %s\n\nPREVIOUS was: %s", dc, stack(), db.lastPut[dc])
|
2013-02-20 15:35:27 -08:00
|
|
|
}
|
|
|
|
|
panic("sql: connection returned that was never out")
|
|
|
|
|
}
|
|
|
|
|
if debugGetPut {
|
2013-03-14 15:01:45 -07:00
|
|
|
db.lastPut[dc] = stack()
|
2013-02-20 15:35:27 -08:00
|
|
|
}
|
2013-04-03 11:13:40 -07:00
|
|
|
dc.inUse = false
|
2013-02-20 15:35:27 -08:00
|
|
|
|
2013-04-03 11:13:40 -07:00
|
|
|
for _, fn := range dc.onPut {
|
|
|
|
|
fn()
|
2013-02-20 15:35:27 -08:00
|
|
|
}
|
2013-04-03 11:13:40 -07:00
|
|
|
dc.onPut = nil
|
2013-02-20 15:35:27 -08:00
|
|
|
|
2012-03-08 10:09:52 -08:00
|
|
|
if err == driver.ErrBadConn {
|
|
|
|
|
// Don't reuse bad connections.
|
2013-08-30 09:27:33 -07:00
|
|
|
// Since the conn is considered bad and is being discarded, treat it
|
2013-10-16 09:17:25 -07:00
|
|
|
// as closed. Don't decrement the open count here, finalClose will
|
|
|
|
|
// take care of that.
|
2013-08-30 09:27:33 -07:00
|
|
|
db.maybeOpenNewConnections()
|
2013-02-20 15:35:27 -08:00
|
|
|
db.mu.Unlock()
|
2013-08-14 09:27:30 -07:00
|
|
|
dc.Close()
|
2012-03-08 10:09:52 -08:00
|
|
|
return
|
|
|
|
|
}
|
2012-03-10 10:00:02 -08:00
|
|
|
if putConnHook != nil {
|
2013-03-14 15:01:45 -07:00
|
|
|
putConnHook(db, dc)
|
2012-03-10 10:00:02 -08:00
|
|
|
}
|
2013-08-30 09:27:33 -07:00
|
|
|
added := db.putConnDBLocked(dc, nil)
|
2013-08-29 17:26:00 -07:00
|
|
|
db.mu.Unlock()
|
2013-03-14 15:01:45 -07:00
|
|
|
|
2013-08-30 09:27:33 -07:00
|
|
|
if !added {
|
|
|
|
|
dc.Close()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Satisfy a connRequest or put the driverConn in the idle pool and return true
|
|
|
|
|
// or return false.
|
|
|
|
|
// putConnDBLocked will satisfy a connRequest if there is one, or it will
|
2013-12-17 14:53:31 -08:00
|
|
|
// return the *driverConn to the freeConn list if err == nil and the idle
|
|
|
|
|
// connection limit will not be exceeded.
|
2013-08-30 09:27:33 -07:00
|
|
|
// If err != nil, the value of dc is ignored.
|
|
|
|
|
// If err == nil, then dc must not equal nil.
|
2014-04-29 12:44:40 -04:00
|
|
|
// If a connRequest was fulfilled or the *driverConn was placed in the
|
2013-08-30 09:27:33 -07:00
|
|
|
// freeConn list, then true is returned, otherwise false is returned.
|
|
|
|
|
func (db *DB) putConnDBLocked(dc *driverConn, err error) bool {
|
2014-12-30 16:12:50 +08:00
|
|
|
if db.maxOpen > 0 && db.numOpen > db.maxOpen {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2014-08-28 08:49:56 -07:00
|
|
|
if c := len(db.connRequests); c > 0 {
|
|
|
|
|
req := db.connRequests[0]
|
2014-08-28 11:07:29 -07:00
|
|
|
// This copy is O(n) but in practice faster than a linked list.
|
|
|
|
|
// TODO: consider compacting it down less often and
|
|
|
|
|
// moving the base instead?
|
2014-08-28 08:49:56 -07:00
|
|
|
copy(db.connRequests, db.connRequests[1:])
|
|
|
|
|
db.connRequests = db.connRequests[:c-1]
|
|
|
|
|
if err == nil {
|
2013-08-30 09:27:33 -07:00
|
|
|
dc.inUse = true
|
2014-08-28 08:49:56 -07:00
|
|
|
}
|
2014-08-28 11:07:29 -07:00
|
|
|
req <- connRequest{
|
2014-08-28 08:49:56 -07:00
|
|
|
conn: dc,
|
|
|
|
|
err: err,
|
2013-08-30 09:27:33 -07:00
|
|
|
}
|
|
|
|
|
return true
|
2014-08-28 08:49:56 -07:00
|
|
|
} else if err == nil && !db.closed && db.maxIdleConnsLocked() > len(db.freeConn) {
|
|
|
|
|
db.freeConn = append(db.freeConn, dc)
|
2013-08-30 09:27:33 -07:00
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
2013-12-17 11:57:30 -08:00
|
|
|
// maxBadConnRetries is the number of maximum retries if the driver returns
|
2015-03-27 19:45:12 +01:00
|
|
|
// driver.ErrBadConn to signal a broken connection before forcing a new
|
|
|
|
|
// connection to be opened.
|
|
|
|
|
const maxBadConnRetries = 2
|
2013-12-17 11:57:30 -08:00
|
|
|
|
2013-02-20 22:15:36 -08:00
|
|
|
// Prepare creates a prepared statement for later queries or executions.
|
|
|
|
|
// Multiple queries or executions may be run concurrently from the
|
|
|
|
|
// returned statement.
|
2011-11-01 22:04:37 -04:00
|
|
|
func (db *DB) Prepare(query string) (*Stmt, error) {
|
2012-03-08 10:09:52 -08:00
|
|
|
var stmt *Stmt
|
|
|
|
|
var err error
|
2013-12-17 11:57:30 -08:00
|
|
|
for i := 0; i < maxBadConnRetries; i++ {
|
2015-03-27 19:45:12 +01:00
|
|
|
stmt, err = db.prepare(query, cachedOrNewConn)
|
2012-03-08 10:09:52 -08:00
|
|
|
if err != driver.ErrBadConn {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-03-27 19:45:12 +01:00
|
|
|
if err == driver.ErrBadConn {
|
|
|
|
|
return db.prepare(query, alwaysNewConn)
|
|
|
|
|
}
|
2012-03-08 10:09:52 -08:00
|
|
|
return stmt, err
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-27 19:45:12 +01:00
|
|
|
func (db *DB) prepare(query string, strategy connReuseStrategy) (*Stmt, error) {
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
// TODO: check if db.driver supports an optional
|
|
|
|
|
// driver.Preparer interface and call that instead, if so,
|
|
|
|
|
// otherwise we make a prepared statement that's bound
|
|
|
|
|
// to a connection, and to execute this prepared statement
|
|
|
|
|
// we either need to use this connection (if it's free), else
|
|
|
|
|
// get a new connection + re-prepare + execute on that one.
|
2015-03-27 19:45:12 +01:00
|
|
|
dc, err := db.conn(strategy)
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2013-03-14 15:01:45 -07:00
|
|
|
dc.Lock()
|
database/sql: fix driver Conn refcounting with prepared statements
The refcounting of driver Conns was completedly busted and
would leak (be held open forever) with any reasonable
load. This was a significant regression from Go 1.0.
The core of this patch is removing one line:
s.db.addDep(dc, s)
A database conn (dc) is a resource that be re-created any time
(but cached for speed) should not be held open forever with a
dependency refcount just because the Stmt (s) is alive (which
typically last for long periods of time, like forever).
The meat of the patch is new tests. In fixing the real issue,
a lot of tests then failed due to the fakedb_test.go's paranoia
about closing a fakeConn while it has open fakeStmts on it. I
could've ignored that, but that's been a problem in the past for
other bugs.
Instead, I now track per-Conn open statements and close them
when the the conn closes. The proper way to do this would've
been making *driverStmt a finalCloser and using the dep mechanism,
but it was much more invasive. Added a TODO instead.
I'd like to give a way for drivers to opt-out of caring about
driver.Stmt closes before a driver.Conn close, but that's a TODO
for the future, and that TODO is added in this CL.
I know this is very late for Go 1.1, but database/sql is
currently nearly useless without this.
I'd like to believe all these database/sql bugs in the past
release cycle are the result of increased usage, number of
drivers, and good feedback from increasingly-capable Go
developers, and not the result of me sucking. It's also hard
with all the real drivers being out-of-tree, so I'm having to
add more and more hooks to fakedb_test.go to simulate things
which real drivers end up doing.
Fixes #5323
R=golang-dev, snaury, gwenn.kahz, google, r
CC=golang-dev
https://golang.org/cl/8836045
2013-04-25 14:45:56 -07:00
|
|
|
si, err := dc.prepareLocked(query)
|
2013-03-14 15:01:45 -07:00
|
|
|
dc.Unlock()
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
if err != nil {
|
2013-03-14 15:01:45 -07:00
|
|
|
db.putConn(dc, err)
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
return nil, err
|
|
|
|
|
}
|
2013-02-20 15:35:27 -08:00
|
|
|
stmt := &Stmt{
|
2015-01-23 20:02:37 +09:00
|
|
|
db: db,
|
|
|
|
|
query: query,
|
|
|
|
|
css: []connStmt{{dc, si}},
|
|
|
|
|
lastNumClosed: atomic.LoadUint64(&db.numClosed),
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
2013-02-20 15:35:27 -08:00
|
|
|
db.addDep(stmt, stmt)
|
2013-03-14 15:01:45 -07:00
|
|
|
db.putConn(dc, nil)
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
return stmt, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Exec executes a query without returning any rows.
|
2013-02-13 15:25:39 -08:00
|
|
|
// The args are for any placeholder parameters in the query.
|
2011-11-01 22:04:37 -04:00
|
|
|
func (db *DB) Exec(query string, args ...interface{}) (Result, error) {
|
2012-03-08 10:09:52 -08:00
|
|
|
var res Result
|
2012-05-29 11:09:09 -07:00
|
|
|
var err error
|
2013-12-17 11:57:30 -08:00
|
|
|
for i := 0; i < maxBadConnRetries; i++ {
|
2015-03-27 19:45:12 +01:00
|
|
|
res, err = db.exec(query, args, cachedOrNewConn)
|
2012-03-08 10:09:52 -08:00
|
|
|
if err != driver.ErrBadConn {
|
|
|
|
|
break
|
|
|
|
|
}
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
2015-03-27 19:45:12 +01:00
|
|
|
if err == driver.ErrBadConn {
|
|
|
|
|
return db.exec(query, args, alwaysNewConn)
|
|
|
|
|
}
|
2012-03-08 10:09:52 -08:00
|
|
|
return res, err
|
|
|
|
|
}
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
|
2015-03-27 19:45:12 +01:00
|
|
|
func (db *DB) exec(query string, args []interface{}, strategy connReuseStrategy) (res Result, err error) {
|
|
|
|
|
dc, err := db.conn(strategy)
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2012-08-23 19:29:47 -07:00
|
|
|
defer func() {
|
2013-03-14 15:01:45 -07:00
|
|
|
db.putConn(dc, err)
|
2012-08-23 19:29:47 -07:00
|
|
|
}()
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
|
2013-03-14 15:01:45 -07:00
|
|
|
if execer, ok := dc.ci.(driver.Execer); ok {
|
2012-05-29 11:09:09 -07:00
|
|
|
dargs, err := driverArgs(nil, args)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2013-03-14 15:01:45 -07:00
|
|
|
dc.Lock()
|
2012-05-29 11:09:09 -07:00
|
|
|
resi, err := execer.Exec(query, dargs)
|
2013-03-14 15:01:45 -07:00
|
|
|
dc.Unlock()
|
2011-11-14 10:48:26 -08:00
|
|
|
if err != driver.ErrSkip {
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2013-03-14 15:01:45 -07:00
|
|
|
return driverResult{dc, resi}, nil
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-14 15:01:45 -07:00
|
|
|
dc.Lock()
|
|
|
|
|
si, err := dc.ci.Prepare(query)
|
|
|
|
|
dc.Unlock()
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2013-03-14 15:01:45 -07:00
|
|
|
defer withLock(dc, func() { si.Close() })
|
|
|
|
|
return resultFromStatement(driverStmt{dc, si}, args...)
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Query executes a query that returns rows, typically a SELECT.
|
2013-01-11 14:46:49 -08:00
|
|
|
// The args are for any placeholder parameters in the query.
|
2011-11-01 22:04:37 -04:00
|
|
|
func (db *DB) Query(query string, args ...interface{}) (*Rows, error) {
|
2013-02-13 15:25:39 -08:00
|
|
|
var rows *Rows
|
|
|
|
|
var err error
|
2013-12-17 11:57:30 -08:00
|
|
|
for i := 0; i < maxBadConnRetries; i++ {
|
2015-03-27 19:45:12 +01:00
|
|
|
rows, err = db.query(query, args, cachedOrNewConn)
|
2013-02-13 15:25:39 -08:00
|
|
|
if err != driver.ErrBadConn {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-03-27 19:45:12 +01:00
|
|
|
if err == driver.ErrBadConn {
|
|
|
|
|
return db.query(query, args, alwaysNewConn)
|
|
|
|
|
}
|
2013-02-13 15:25:39 -08:00
|
|
|
return rows, err
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-27 19:45:12 +01:00
|
|
|
func (db *DB) query(query string, args []interface{}, strategy connReuseStrategy) (*Rows, error) {
|
|
|
|
|
ci, err := db.conn(strategy)
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2013-02-13 15:25:39 -08:00
|
|
|
|
2013-05-14 16:35:31 -07:00
|
|
|
return db.queryConn(ci, ci.releaseConn, query, args)
|
2013-02-13 15:25:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// queryConn executes a query on the given connection.
|
|
|
|
|
// The connection gets released by the releaseConn function.
|
2013-03-14 15:01:45 -07:00
|
|
|
func (db *DB) queryConn(dc *driverConn, releaseConn func(error), query string, args []interface{}) (*Rows, error) {
|
|
|
|
|
if queryer, ok := dc.ci.(driver.Queryer); ok {
|
2013-02-13 15:25:39 -08:00
|
|
|
dargs, err := driverArgs(nil, args)
|
|
|
|
|
if err != nil {
|
|
|
|
|
releaseConn(err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2013-03-14 15:01:45 -07:00
|
|
|
dc.Lock()
|
2013-02-13 15:25:39 -08:00
|
|
|
rowsi, err := queryer.Query(query, dargs)
|
2013-03-14 15:01:45 -07:00
|
|
|
dc.Unlock()
|
2013-02-13 15:25:39 -08:00
|
|
|
if err != driver.ErrSkip {
|
|
|
|
|
if err != nil {
|
|
|
|
|
releaseConn(err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2013-03-18 11:39:00 -07:00
|
|
|
// Note: ownership of dc passes to the *Rows, to be freed
|
2013-02-13 15:25:39 -08:00
|
|
|
// with releaseConn.
|
|
|
|
|
rows := &Rows{
|
2013-03-14 15:01:45 -07:00
|
|
|
dc: dc,
|
2013-02-13 15:25:39 -08:00
|
|
|
releaseConn: releaseConn,
|
|
|
|
|
rowsi: rowsi,
|
|
|
|
|
}
|
|
|
|
|
return rows, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-14 15:01:45 -07:00
|
|
|
dc.Lock()
|
|
|
|
|
si, err := dc.ci.Prepare(query)
|
|
|
|
|
dc.Unlock()
|
2012-01-13 15:25:07 -08:00
|
|
|
if err != nil {
|
2013-02-13 15:25:39 -08:00
|
|
|
releaseConn(err)
|
2012-01-13 15:25:07 -08:00
|
|
|
return nil, err
|
|
|
|
|
}
|
2013-02-13 15:25:39 -08:00
|
|
|
|
2013-03-14 15:01:45 -07:00
|
|
|
ds := driverStmt{dc, si}
|
|
|
|
|
rowsi, err := rowsiFromStatement(ds, args...)
|
2013-02-13 15:25:39 -08:00
|
|
|
if err != nil {
|
2013-03-14 15:01:45 -07:00
|
|
|
dc.Lock()
|
|
|
|
|
si.Close()
|
|
|
|
|
dc.Unlock()
|
2013-07-23 14:09:53 +10:00
|
|
|
releaseConn(err)
|
2013-02-13 15:25:39 -08:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Note: ownership of ci passes to the *Rows, to be freed
|
|
|
|
|
// with releaseConn.
|
|
|
|
|
rows := &Rows{
|
2013-03-14 15:01:45 -07:00
|
|
|
dc: dc,
|
2013-02-13 15:25:39 -08:00
|
|
|
releaseConn: releaseConn,
|
|
|
|
|
rowsi: rowsi,
|
2013-03-14 15:01:45 -07:00
|
|
|
closeStmt: si,
|
2013-02-13 15:25:39 -08:00
|
|
|
}
|
2012-01-13 15:25:07 -08:00
|
|
|
return rows, nil
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// QueryRow executes a query that is expected to return at most one row.
|
|
|
|
|
// QueryRow always return a non-nil value. Errors are deferred until
|
|
|
|
|
// Row's Scan method is called.
|
|
|
|
|
func (db *DB) QueryRow(query string, args ...interface{}) *Row {
|
|
|
|
|
rows, err := db.Query(query, args...)
|
2011-11-02 11:46:04 -07:00
|
|
|
return &Row{rows: rows, err: err}
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
2011-11-02 11:46:04 -07:00
|
|
|
// Begin starts a transaction. The isolation level is dependent on
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
// the driver.
|
2011-11-01 22:04:37 -04:00
|
|
|
func (db *DB) Begin() (*Tx, error) {
|
2012-03-08 10:09:52 -08:00
|
|
|
var tx *Tx
|
|
|
|
|
var err error
|
2013-12-17 11:57:30 -08:00
|
|
|
for i := 0; i < maxBadConnRetries; i++ {
|
2015-03-27 19:45:12 +01:00
|
|
|
tx, err = db.begin(cachedOrNewConn)
|
2012-03-08 10:09:52 -08:00
|
|
|
if err != driver.ErrBadConn {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-03-27 19:45:12 +01:00
|
|
|
if err == driver.ErrBadConn {
|
|
|
|
|
return db.begin(alwaysNewConn)
|
|
|
|
|
}
|
2012-03-08 10:09:52 -08:00
|
|
|
return tx, err
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-27 19:45:12 +01:00
|
|
|
func (db *DB) begin(strategy connReuseStrategy) (tx *Tx, err error) {
|
|
|
|
|
dc, err := db.conn(strategy)
|
2011-11-02 11:46:04 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2013-03-14 15:01:45 -07:00
|
|
|
dc.Lock()
|
|
|
|
|
txi, err := dc.ci.Begin()
|
|
|
|
|
dc.Unlock()
|
2011-11-02 11:46:04 -07:00
|
|
|
if err != nil {
|
2013-03-14 15:01:45 -07:00
|
|
|
db.putConn(dc, err)
|
2012-12-12 22:04:55 -08:00
|
|
|
return nil, err
|
2011-11-02 11:46:04 -07:00
|
|
|
}
|
|
|
|
|
return &Tx{
|
|
|
|
|
db: db,
|
2013-03-14 15:01:45 -07:00
|
|
|
dc: dc,
|
2011-11-02 11:46:04 -07:00
|
|
|
txi: txi,
|
|
|
|
|
}, nil
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
2012-02-10 09:12:32 +11:00
|
|
|
// Driver returns the database's underlying driver.
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
func (db *DB) Driver() driver.Driver {
|
|
|
|
|
return db.driver
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tx is an in-progress database transaction.
|
2011-11-02 11:46:04 -07:00
|
|
|
//
|
|
|
|
|
// A transaction must end with a call to Commit or Rollback.
|
|
|
|
|
//
|
|
|
|
|
// After a call to Commit or Rollback, all operations on the
|
2012-02-10 09:12:32 +11:00
|
|
|
// transaction fail with ErrTxDone.
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
type Tx struct {
|
2011-11-02 11:46:04 -07:00
|
|
|
db *DB
|
|
|
|
|
|
2013-03-14 15:01:45 -07:00
|
|
|
// dc is owned exclusively until Commit or Rollback, at which point
|
2011-11-02 11:46:04 -07:00
|
|
|
// it's returned with putConn.
|
2013-03-14 15:01:45 -07:00
|
|
|
dc *driverConn
|
2011-11-02 11:46:04 -07:00
|
|
|
txi driver.Tx
|
|
|
|
|
|
|
|
|
|
// done transitions from false to true exactly once, on Commit
|
|
|
|
|
// or Rollback. once done, all operations fail with
|
2012-02-10 09:12:32 +11:00
|
|
|
// ErrTxDone.
|
2011-11-02 11:46:04 -07:00
|
|
|
done bool
|
2014-09-22 09:19:27 -04:00
|
|
|
|
|
|
|
|
// All Stmts prepared for this transaction. These will be closed after the
|
|
|
|
|
// transaction has been committed or rolled back.
|
|
|
|
|
stmts struct {
|
|
|
|
|
sync.Mutex
|
|
|
|
|
v []*Stmt
|
|
|
|
|
}
|
2011-11-02 11:46:04 -07:00
|
|
|
}
|
|
|
|
|
|
2012-02-10 09:12:32 +11:00
|
|
|
var ErrTxDone = errors.New("sql: Transaction has already been committed or rolled back")
|
2011-11-02 11:46:04 -07:00
|
|
|
|
|
|
|
|
func (tx *Tx) close() {
|
|
|
|
|
if tx.done {
|
|
|
|
|
panic("double close") // internal error
|
|
|
|
|
}
|
|
|
|
|
tx.done = true
|
2013-03-14 15:01:45 -07:00
|
|
|
tx.db.putConn(tx.dc, nil)
|
|
|
|
|
tx.dc = nil
|
2011-11-02 11:46:04 -07:00
|
|
|
tx.txi = nil
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-14 15:01:45 -07:00
|
|
|
func (tx *Tx) grabConn() (*driverConn, error) {
|
2011-11-02 11:46:04 -07:00
|
|
|
if tx.done {
|
2012-02-10 09:12:32 +11:00
|
|
|
return nil, ErrTxDone
|
2011-11-02 11:46:04 -07:00
|
|
|
}
|
2013-03-14 15:01:45 -07:00
|
|
|
return tx.dc, nil
|
2011-11-02 11:46:04 -07:00
|
|
|
}
|
|
|
|
|
|
2014-09-22 09:19:27 -04:00
|
|
|
// Closes all Stmts prepared for this transaction.
|
|
|
|
|
func (tx *Tx) closePrepared() {
|
|
|
|
|
tx.stmts.Lock()
|
|
|
|
|
for _, stmt := range tx.stmts.v {
|
|
|
|
|
stmt.Close()
|
|
|
|
|
}
|
|
|
|
|
tx.stmts.Unlock()
|
|
|
|
|
}
|
|
|
|
|
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
// Commit commits the transaction.
|
2011-11-01 22:04:37 -04:00
|
|
|
func (tx *Tx) Commit() error {
|
2011-11-02 11:46:04 -07:00
|
|
|
if tx.done {
|
2012-02-10 09:12:32 +11:00
|
|
|
return ErrTxDone
|
2011-11-02 11:46:04 -07:00
|
|
|
}
|
|
|
|
|
defer tx.close()
|
2013-03-14 15:01:45 -07:00
|
|
|
tx.dc.Lock()
|
2014-09-22 09:19:27 -04:00
|
|
|
err := tx.txi.Commit()
|
|
|
|
|
tx.dc.Unlock()
|
|
|
|
|
if err != driver.ErrBadConn {
|
|
|
|
|
tx.closePrepared()
|
|
|
|
|
}
|
|
|
|
|
return err
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Rollback aborts the transaction.
|
2011-11-01 22:04:37 -04:00
|
|
|
func (tx *Tx) Rollback() error {
|
2011-11-02 11:46:04 -07:00
|
|
|
if tx.done {
|
2012-02-10 09:12:32 +11:00
|
|
|
return ErrTxDone
|
2011-11-02 11:46:04 -07:00
|
|
|
}
|
|
|
|
|
defer tx.close()
|
2013-03-14 15:01:45 -07:00
|
|
|
tx.dc.Lock()
|
2014-09-22 09:19:27 -04:00
|
|
|
err := tx.txi.Rollback()
|
|
|
|
|
tx.dc.Unlock()
|
|
|
|
|
if err != driver.ErrBadConn {
|
|
|
|
|
tx.closePrepared()
|
|
|
|
|
}
|
|
|
|
|
return err
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
2011-11-28 11:00:32 -05:00
|
|
|
// Prepare creates a prepared statement for use within a transaction.
|
2011-11-02 11:46:04 -07:00
|
|
|
//
|
2011-11-28 11:00:32 -05:00
|
|
|
// The returned statement operates within the transaction and can no longer
|
|
|
|
|
// be used once the transaction has been committed or rolled back.
|
|
|
|
|
//
|
|
|
|
|
// To use an existing prepared statement on this transaction, see Tx.Stmt.
|
2011-11-01 22:04:37 -04:00
|
|
|
func (tx *Tx) Prepare(query string) (*Stmt, error) {
|
2011-11-28 11:00:32 -05:00
|
|
|
// TODO(bradfitz): We could be more efficient here and either
|
|
|
|
|
// provide a method to take an existing Stmt (created on
|
|
|
|
|
// perhaps a different Conn), and re-create it on this Conn if
|
|
|
|
|
// necessary. Or, better: keep a map in DB of query string to
|
|
|
|
|
// Stmts, and have Stmt.Execute do the right thing and
|
|
|
|
|
// re-prepare if the Conn in use doesn't have that prepared
|
|
|
|
|
// statement. But we'll want to avoid caching the statement
|
|
|
|
|
// in the case where we only call conn.Prepare implicitly
|
|
|
|
|
// (such as in db.Exec or tx.Exec), but the caller package
|
|
|
|
|
// can't be holding a reference to the returned statement.
|
|
|
|
|
// Perhaps just looking at the reference count (by noting
|
|
|
|
|
// Stmt.Close) would be enough. We might also want a finalizer
|
|
|
|
|
// on Stmt to drop the reference count.
|
2013-03-14 15:01:45 -07:00
|
|
|
dc, err := tx.grabConn()
|
2011-11-02 11:46:04 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-14 15:01:45 -07:00
|
|
|
dc.Lock()
|
|
|
|
|
si, err := dc.ci.Prepare(query)
|
|
|
|
|
dc.Unlock()
|
2011-11-02 11:46:04 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stmt := &Stmt{
|
2013-03-14 15:01:45 -07:00
|
|
|
db: tx.db,
|
|
|
|
|
tx: tx,
|
|
|
|
|
txsi: &driverStmt{
|
|
|
|
|
Locker: dc,
|
|
|
|
|
si: si,
|
|
|
|
|
},
|
2011-11-02 11:46:04 -07:00
|
|
|
query: query,
|
|
|
|
|
}
|
2014-09-22 09:19:27 -04:00
|
|
|
tx.stmts.Lock()
|
|
|
|
|
tx.stmts.v = append(tx.stmts.v, stmt)
|
|
|
|
|
tx.stmts.Unlock()
|
2011-11-02 11:46:04 -07:00
|
|
|
return stmt, nil
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
2011-11-28 11:00:32 -05:00
|
|
|
// Stmt returns a transaction-specific prepared statement from
|
|
|
|
|
// an existing statement.
|
|
|
|
|
//
|
|
|
|
|
// Example:
|
|
|
|
|
// updateMoney, err := db.Prepare("UPDATE balance SET money=money+? WHERE id=?")
|
|
|
|
|
// ...
|
|
|
|
|
// tx, err := db.Begin()
|
|
|
|
|
// ...
|
|
|
|
|
// res, err := tx.Stmt(updateMoney).Exec(123.45, 98293203)
|
|
|
|
|
func (tx *Tx) Stmt(stmt *Stmt) *Stmt {
|
|
|
|
|
// TODO(bradfitz): optimize this. Currently this re-prepares
|
|
|
|
|
// each time. This is fine for now to illustrate the API but
|
|
|
|
|
// we should really cache already-prepared statements
|
|
|
|
|
// per-Conn. See also the big comment in Tx.Prepare.
|
|
|
|
|
|
|
|
|
|
if tx.db != stmt.db {
|
|
|
|
|
return &Stmt{stickyErr: errors.New("sql: Tx.Stmt: statement from different database used")}
|
|
|
|
|
}
|
2013-03-14 15:01:45 -07:00
|
|
|
dc, err := tx.grabConn()
|
2011-11-28 11:00:32 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return &Stmt{stickyErr: err}
|
|
|
|
|
}
|
2013-03-14 15:01:45 -07:00
|
|
|
dc.Lock()
|
|
|
|
|
si, err := dc.ci.Prepare(stmt.query)
|
|
|
|
|
dc.Unlock()
|
2014-09-22 09:19:27 -04:00
|
|
|
txs := &Stmt{
|
2013-03-14 15:01:45 -07:00
|
|
|
db: tx.db,
|
|
|
|
|
tx: tx,
|
|
|
|
|
txsi: &driverStmt{
|
|
|
|
|
Locker: dc,
|
|
|
|
|
si: si,
|
|
|
|
|
},
|
2011-11-28 11:00:32 -05:00
|
|
|
query: stmt.query,
|
|
|
|
|
stickyErr: err,
|
|
|
|
|
}
|
2014-09-22 09:19:27 -04:00
|
|
|
tx.stmts.Lock()
|
|
|
|
|
tx.stmts.v = append(tx.stmts.v, txs)
|
|
|
|
|
tx.stmts.Unlock()
|
|
|
|
|
return txs
|
2011-11-28 11:00:32 -05:00
|
|
|
}
|
|
|
|
|
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
// Exec executes a query that doesn't return rows.
|
|
|
|
|
// For example: an INSERT and UPDATE.
|
2011-11-02 11:46:04 -07:00
|
|
|
func (tx *Tx) Exec(query string, args ...interface{}) (Result, error) {
|
2013-03-14 15:01:45 -07:00
|
|
|
dc, err := tx.grabConn()
|
2011-11-02 11:46:04 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-14 15:01:45 -07:00
|
|
|
if execer, ok := dc.ci.(driver.Execer); ok {
|
2012-05-29 11:09:09 -07:00
|
|
|
dargs, err := driverArgs(nil, args)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2013-03-14 15:01:45 -07:00
|
|
|
dc.Lock()
|
2012-05-29 11:09:09 -07:00
|
|
|
resi, err := execer.Exec(query, dargs)
|
2013-03-14 15:01:45 -07:00
|
|
|
dc.Unlock()
|
2012-02-10 09:19:22 +11:00
|
|
|
if err == nil {
|
2013-03-14 15:01:45 -07:00
|
|
|
return driverResult{dc, resi}, nil
|
2012-02-10 09:19:22 +11:00
|
|
|
}
|
|
|
|
|
if err != driver.ErrSkip {
|
2011-11-02 11:46:04 -07:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-14 15:01:45 -07:00
|
|
|
dc.Lock()
|
|
|
|
|
si, err := dc.ci.Prepare(query)
|
|
|
|
|
dc.Unlock()
|
2011-11-02 11:46:04 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2013-03-14 15:01:45 -07:00
|
|
|
defer withLock(dc, func() { si.Close() })
|
2011-11-14 10:48:26 -08:00
|
|
|
|
2013-03-14 15:01:45 -07:00
|
|
|
return resultFromStatement(driverStmt{dc, si}, args...)
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Query executes a query that returns rows, typically a SELECT.
|
2011-11-01 22:04:37 -04:00
|
|
|
func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) {
|
2013-03-18 11:39:00 -07:00
|
|
|
dc, err := tx.grabConn()
|
2012-03-10 15:21:44 -08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2012-01-25 17:49:30 -08:00
|
|
|
}
|
2013-03-18 11:39:00 -07:00
|
|
|
releaseConn := func(error) {}
|
|
|
|
|
return tx.db.queryConn(dc, releaseConn, query, args)
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// QueryRow executes a query that is expected to return at most one row.
|
|
|
|
|
// QueryRow always return a non-nil value. Errors are deferred until
|
|
|
|
|
// Row's Scan method is called.
|
|
|
|
|
func (tx *Tx) QueryRow(query string, args ...interface{}) *Row {
|
2011-11-02 11:46:04 -07:00
|
|
|
rows, err := tx.Query(query, args...)
|
|
|
|
|
return &Row{rows: rows, err: err}
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// connStmt is a prepared statement on a particular connection.
|
|
|
|
|
type connStmt struct {
|
2013-03-14 15:01:45 -07:00
|
|
|
dc *driverConn
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
si driver.Stmt
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stmt is a prepared statement. Stmt is safe for concurrent use by multiple goroutines.
|
|
|
|
|
type Stmt struct {
|
|
|
|
|
// Immutable:
|
2011-11-28 11:00:32 -05:00
|
|
|
db *DB // where we came from
|
|
|
|
|
query string // that created the Stmt
|
|
|
|
|
stickyErr error // if non-nil, this error is returned for all operations
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
|
2013-02-20 15:35:27 -08:00
|
|
|
closemu sync.RWMutex // held exclusively during close, for read otherwise.
|
|
|
|
|
|
2011-11-02 11:46:04 -07:00
|
|
|
// If in a transaction, else both nil:
|
|
|
|
|
tx *Tx
|
2013-03-14 15:01:45 -07:00
|
|
|
txsi *driverStmt
|
2011-11-02 11:46:04 -07:00
|
|
|
|
|
|
|
|
mu sync.Mutex // protects the rest of the fields
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
closed bool
|
|
|
|
|
|
2011-11-02 11:46:04 -07:00
|
|
|
// css is a list of underlying driver statement interfaces
|
|
|
|
|
// that are valid on particular connections. This is only
|
|
|
|
|
// used if tx == nil and one is found that has idle
|
|
|
|
|
// connections. If tx != nil, txsi is always used.
|
|
|
|
|
css []connStmt
|
2015-01-23 20:02:37 +09:00
|
|
|
|
|
|
|
|
// lastNumClosed is copied from db.numClosed when Stmt is created
|
|
|
|
|
// without tx and closed connections in css are removed.
|
|
|
|
|
lastNumClosed uint64
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Exec executes a prepared statement with the given arguments and
|
|
|
|
|
// returns a Result summarizing the effect of the statement.
|
2011-11-01 22:04:37 -04:00
|
|
|
func (s *Stmt) Exec(args ...interface{}) (Result, error) {
|
2013-02-20 15:35:27 -08:00
|
|
|
s.closemu.RLock()
|
|
|
|
|
defer s.closemu.RUnlock()
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
|
2013-12-17 11:57:30 -08:00
|
|
|
var res Result
|
|
|
|
|
for i := 0; i < maxBadConnRetries; i++ {
|
|
|
|
|
dc, releaseConn, si, err := s.connStmt()
|
|
|
|
|
if err != nil {
|
|
|
|
|
if err == driver.ErrBadConn {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res, err = resultFromStatement(driverStmt{dc, si}, args...)
|
|
|
|
|
releaseConn(err)
|
|
|
|
|
if err != driver.ErrBadConn {
|
|
|
|
|
return res, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil, driver.ErrBadConn
|
2013-01-11 13:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
2013-03-14 15:01:45 -07:00
|
|
|
func resultFromStatement(ds driverStmt, args ...interface{}) (Result, error) {
|
|
|
|
|
ds.Lock()
|
|
|
|
|
want := ds.si.NumInput()
|
|
|
|
|
ds.Unlock()
|
|
|
|
|
|
2011-11-15 16:29:43 -08:00
|
|
|
// -1 means the driver doesn't know how to count the number of
|
|
|
|
|
// placeholders, so we won't sanity check input here and instead let the
|
|
|
|
|
// driver deal with errors.
|
2013-03-14 15:01:45 -07:00
|
|
|
if want != -1 && len(args) != want {
|
2011-12-15 10:14:57 -08:00
|
|
|
return nil, fmt.Errorf("sql: expected %d arguments, got %d", want, len(args))
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
2013-03-14 15:01:45 -07:00
|
|
|
dargs, err := driverArgs(&ds, args)
|
2012-05-29 11:09:09 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
2013-03-14 15:01:45 -07:00
|
|
|
ds.Lock()
|
|
|
|
|
resi, err := ds.si.Exec(dargs)
|
|
|
|
|
ds.Unlock()
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2013-03-14 15:01:45 -07:00
|
|
|
return driverResult{ds.Locker, resi}, nil
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
2015-01-23 20:02:37 +09:00
|
|
|
// removeClosedStmtLocked removes closed conns in s.css.
|
|
|
|
|
//
|
|
|
|
|
// To avoid lock contention on DB.mu, we do it only when
|
|
|
|
|
// s.db.numClosed - s.lastNum is large enough.
|
|
|
|
|
func (s *Stmt) removeClosedStmtLocked() {
|
|
|
|
|
t := len(s.css)/2 + 1
|
|
|
|
|
if t > 10 {
|
|
|
|
|
t = 10
|
|
|
|
|
}
|
|
|
|
|
dbClosed := atomic.LoadUint64(&s.db.numClosed)
|
|
|
|
|
if dbClosed-s.lastNumClosed < uint64(t) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s.db.mu.Lock()
|
|
|
|
|
for i := 0; i < len(s.css); i++ {
|
|
|
|
|
if s.css[i].dc.dbmuClosed {
|
|
|
|
|
s.css[i] = s.css[len(s.css)-1]
|
|
|
|
|
s.css = s.css[:len(s.css)-1]
|
|
|
|
|
i--
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
s.db.mu.Unlock()
|
|
|
|
|
s.lastNumClosed = dbClosed
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-02 11:46:04 -07:00
|
|
|
// connStmt returns a free driver connection on which to execute the
|
|
|
|
|
// statement, a function to call to release the connection, and a
|
|
|
|
|
// statement bound to that connection.
|
2013-03-14 15:01:45 -07:00
|
|
|
func (s *Stmt) connStmt() (ci *driverConn, releaseConn func(error), si driver.Stmt, err error) {
|
2012-01-10 12:51:27 -08:00
|
|
|
if err = s.stickyErr; err != nil {
|
|
|
|
|
return
|
2011-11-28 11:00:32 -05:00
|
|
|
}
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
s.mu.Lock()
|
|
|
|
|
if s.closed {
|
2011-11-02 11:46:04 -07:00
|
|
|
s.mu.Unlock()
|
2011-12-15 10:14:57 -08:00
|
|
|
err = errors.New("sql: statement is closed")
|
2011-11-02 11:46:04 -07:00
|
|
|
return
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
2011-11-02 11:46:04 -07:00
|
|
|
|
|
|
|
|
// In a transaction, we always use the connection that the
|
|
|
|
|
// transaction was created on.
|
|
|
|
|
if s.tx != nil {
|
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
ci, err = s.tx.grabConn() // blocks, waiting for the connection.
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2013-03-18 11:39:00 -07:00
|
|
|
releaseConn = func(error) {}
|
2013-03-14 15:01:45 -07:00
|
|
|
return ci, releaseConn, s.txsi.si, nil
|
2011-11-02 11:46:04 -07:00
|
|
|
}
|
|
|
|
|
|
2015-01-23 20:02:37 +09:00
|
|
|
s.removeClosedStmtLocked()
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
2014-09-02 09:08:41 -07:00
|
|
|
// TODO(bradfitz): or always wait for one? make configurable later?
|
2015-03-27 19:45:12 +01:00
|
|
|
dc, err := s.db.conn(cachedOrNewConn)
|
2014-09-02 09:08:41 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s.mu.Lock()
|
|
|
|
|
for _, v := range s.css {
|
|
|
|
|
if v.dc == dc {
|
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
return dc, dc.releaseConn, v.si, nil
|
2013-12-17 11:57:30 -08:00
|
|
|
}
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
2014-09-02 09:08:41 -07:00
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
|
|
|
|
// No luck; we need to prepare the statement on this connection
|
|
|
|
|
dc.Lock()
|
|
|
|
|
si, err = dc.prepareLocked(s.query)
|
|
|
|
|
dc.Unlock()
|
|
|
|
|
if err != nil {
|
|
|
|
|
s.db.putConn(dc, err)
|
|
|
|
|
return nil, nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
s.mu.Lock()
|
|
|
|
|
cs := connStmt{dc, si}
|
|
|
|
|
s.css = append(s.css, cs)
|
|
|
|
|
s.mu.Unlock()
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
|
2014-09-02 09:08:41 -07:00
|
|
|
return dc, dc.releaseConn, si, nil
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Query executes a prepared query statement with the given arguments
|
|
|
|
|
// and returns the query results as a *Rows.
|
2011-11-01 22:04:37 -04:00
|
|
|
func (s *Stmt) Query(args ...interface{}) (*Rows, error) {
|
2013-02-20 15:35:27 -08:00
|
|
|
s.closemu.RLock()
|
|
|
|
|
defer s.closemu.RUnlock()
|
|
|
|
|
|
2013-12-17 11:57:30 -08:00
|
|
|
var rowsi driver.Rows
|
|
|
|
|
for i := 0; i < maxBadConnRetries; i++ {
|
|
|
|
|
dc, releaseConn, si, err := s.connStmt()
|
|
|
|
|
if err != nil {
|
|
|
|
|
if err == driver.ErrBadConn {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2011-11-15 16:29:43 -08:00
|
|
|
|
2013-12-17 11:57:30 -08:00
|
|
|
rowsi, err = rowsiFromStatement(driverStmt{dc, si}, args...)
|
|
|
|
|
if err == nil {
|
|
|
|
|
// Note: ownership of ci passes to the *Rows, to be freed
|
|
|
|
|
// with releaseConn.
|
|
|
|
|
rows := &Rows{
|
|
|
|
|
dc: dc,
|
|
|
|
|
rowsi: rowsi,
|
|
|
|
|
// releaseConn set below
|
|
|
|
|
}
|
|
|
|
|
s.db.addDep(s, rows)
|
|
|
|
|
rows.releaseConn = func(err error) {
|
|
|
|
|
releaseConn(err)
|
|
|
|
|
s.db.removeDep(s, rows)
|
|
|
|
|
}
|
|
|
|
|
return rows, nil
|
|
|
|
|
}
|
2013-02-13 15:25:39 -08:00
|
|
|
|
2013-02-20 15:35:27 -08:00
|
|
|
releaseConn(err)
|
2013-12-17 11:57:30 -08:00
|
|
|
if err != driver.ErrBadConn {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2013-02-13 15:25:39 -08:00
|
|
|
}
|
2013-12-17 11:57:30 -08:00
|
|
|
return nil, driver.ErrBadConn
|
2013-02-13 15:25:39 -08:00
|
|
|
}
|
|
|
|
|
|
2013-03-14 15:01:45 -07:00
|
|
|
func rowsiFromStatement(ds driverStmt, args ...interface{}) (driver.Rows, error) {
|
|
|
|
|
ds.Lock()
|
|
|
|
|
want := ds.si.NumInput()
|
|
|
|
|
ds.Unlock()
|
|
|
|
|
|
2011-11-15 16:29:43 -08:00
|
|
|
// -1 means the driver doesn't know how to count the number of
|
|
|
|
|
// placeholders, so we won't sanity check input here and instead let the
|
|
|
|
|
// driver deal with errors.
|
2013-03-14 15:01:45 -07:00
|
|
|
if want != -1 && len(args) != want {
|
|
|
|
|
return nil, fmt.Errorf("sql: statement expects %d inputs; got %d", want, len(args))
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
2012-05-29 11:09:09 -07:00
|
|
|
|
2013-03-14 15:01:45 -07:00
|
|
|
dargs, err := driverArgs(&ds, args)
|
2011-11-14 10:48:26 -08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2012-05-29 11:09:09 -07:00
|
|
|
|
2013-03-14 15:01:45 -07:00
|
|
|
ds.Lock()
|
|
|
|
|
rowsi, err := ds.si.Query(dargs)
|
|
|
|
|
ds.Unlock()
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2013-02-13 15:25:39 -08:00
|
|
|
return rowsi, nil
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// QueryRow executes a prepared query statement with the given arguments.
|
|
|
|
|
// If an error occurs during the execution of the statement, that error will
|
|
|
|
|
// be returned by a call to Scan on the returned *Row, which is always non-nil.
|
|
|
|
|
// 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.
|
|
|
|
|
//
|
|
|
|
|
// Example usage:
|
|
|
|
|
//
|
|
|
|
|
// var name string
|
2012-02-10 10:20:49 +11:00
|
|
|
// err := nameByUseridStmt.QueryRow(id).Scan(&name)
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
func (s *Stmt) QueryRow(args ...interface{}) *Row {
|
|
|
|
|
rows, err := s.Query(args...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return &Row{err: err}
|
|
|
|
|
}
|
|
|
|
|
return &Row{rows: rows}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close closes the statement.
|
2011-11-01 22:04:37 -04:00
|
|
|
func (s *Stmt) Close() error {
|
2013-02-20 15:35:27 -08:00
|
|
|
s.closemu.Lock()
|
|
|
|
|
defer s.closemu.Unlock()
|
|
|
|
|
|
2011-11-28 11:00:32 -05:00
|
|
|
if s.stickyErr != nil {
|
|
|
|
|
return s.stickyErr
|
|
|
|
|
}
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
s.mu.Lock()
|
|
|
|
|
if s.closed {
|
2013-08-30 09:27:33 -07:00
|
|
|
s.mu.Unlock()
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
s.closed = true
|
2011-11-02 11:46:04 -07:00
|
|
|
|
|
|
|
|
if s.tx != nil {
|
|
|
|
|
s.txsi.Close()
|
2013-08-30 09:27:33 -07:00
|
|
|
s.mu.Unlock()
|
2013-02-20 15:35:27 -08:00
|
|
|
return nil
|
|
|
|
|
}
|
2013-08-30 09:27:33 -07:00
|
|
|
s.mu.Unlock()
|
2013-02-20 15:35:27 -08:00
|
|
|
|
|
|
|
|
return s.db.removeDep(s, s)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Stmt) finalClose() error {
|
2013-08-30 09:27:33 -07:00
|
|
|
s.mu.Lock()
|
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
if s.css != nil {
|
|
|
|
|
for _, v := range s.css {
|
|
|
|
|
s.db.noteUnusedDriverStatement(v.dc, v.si)
|
|
|
|
|
v.dc.removeOpenStmt(v.si)
|
|
|
|
|
}
|
|
|
|
|
s.css = nil
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Rows is the result of a query. Its cursor starts before the first row
|
|
|
|
|
// of the result set. Use Next to advance through the rows:
|
|
|
|
|
//
|
|
|
|
|
// rows, err := db.Query("SELECT ...")
|
|
|
|
|
// ...
|
2014-03-25 13:32:18 +11:00
|
|
|
// defer rows.Close()
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
// for rows.Next() {
|
|
|
|
|
// var id int
|
|
|
|
|
// var name string
|
|
|
|
|
// err = rows.Scan(&id, &name)
|
|
|
|
|
// ...
|
|
|
|
|
// }
|
2011-11-04 09:50:20 -04:00
|
|
|
// err = rows.Err() // get any error encountered during iteration
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
// ...
|
|
|
|
|
type Rows struct {
|
2013-03-14 15:01:45 -07:00
|
|
|
dc *driverConn // owned; must call releaseConn when closed to release
|
2012-03-10 10:00:02 -08:00
|
|
|
releaseConn func(error)
|
2011-11-02 11:46:04 -07:00
|
|
|
rowsi driver.Rows
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
|
2012-01-13 15:25:07 -08:00
|
|
|
closed bool
|
2012-02-20 14:25:28 +11:00
|
|
|
lastcols []driver.Value
|
2013-08-16 11:23:35 +10:00
|
|
|
lasterr error // non-nil only if closed is true
|
2013-02-13 15:25:39 -08:00
|
|
|
closeStmt driver.Stmt // if non-nil, statement to Close on close
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
2013-12-16 12:48:35 -08:00
|
|
|
// Next prepares the next result row for reading with the Scan method. It
|
|
|
|
|
// returns true on success, or false if there is no next result row or an error
|
|
|
|
|
// happened while preparing it. Err should be consulted to distinguish between
|
|
|
|
|
// the two cases.
|
|
|
|
|
//
|
|
|
|
|
// Every call to Scan, even the first one, must be preceded by a call to Next.
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
func (rs *Rows) Next() bool {
|
|
|
|
|
if rs.closed {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
if rs.lastcols == nil {
|
2012-02-20 14:25:28 +11:00
|
|
|
rs.lastcols = make([]driver.Value, len(rs.rowsi.Columns()))
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
rs.lasterr = rs.rowsi.Next(rs.lastcols)
|
2013-08-16 11:23:35 +10:00
|
|
|
if rs.lasterr != nil {
|
2012-01-10 12:51:27 -08:00
|
|
|
rs.Close()
|
2013-08-16 11:23:35 +10:00
|
|
|
return false
|
2012-01-10 12:51:27 -08:00
|
|
|
}
|
2013-08-16 11:23:35 +10:00
|
|
|
return true
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
2011-11-04 09:50:20 -04:00
|
|
|
// Err returns the error, if any, that was encountered during iteration.
|
2013-08-16 11:23:35 +10:00
|
|
|
// Err may be called after an explicit or implicit Close.
|
2011-11-04 09:50:20 -04:00
|
|
|
func (rs *Rows) Err() error {
|
2011-11-01 22:04:37 -04:00
|
|
|
if rs.lasterr == io.EOF {
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return rs.lasterr
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-15 10:14:57 -08:00
|
|
|
// Columns returns the column names.
|
|
|
|
|
// Columns returns an error if the rows are closed, or if the rows
|
|
|
|
|
// are from QueryRow and there was a deferred error.
|
|
|
|
|
func (rs *Rows) Columns() ([]string, error) {
|
|
|
|
|
if rs.closed {
|
|
|
|
|
return nil, errors.New("sql: Rows are closed")
|
|
|
|
|
}
|
|
|
|
|
if rs.rowsi == nil {
|
|
|
|
|
return nil, errors.New("sql: no Rows available")
|
|
|
|
|
}
|
|
|
|
|
return rs.rowsi.Columns(), nil
|
|
|
|
|
}
|
|
|
|
|
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
// Scan copies the columns in the current row into the values pointed
|
2012-01-17 10:44:35 -08:00
|
|
|
// at by dest.
|
|
|
|
|
//
|
|
|
|
|
// If an argument has type *[]byte, Scan saves in that argument a copy
|
|
|
|
|
// of the corresponding data. The copy is owned by the caller and can
|
|
|
|
|
// be modified and held indefinitely. The copy can be avoided by using
|
|
|
|
|
// an argument of type *RawBytes instead; see the documentation for
|
|
|
|
|
// RawBytes for restrictions on its use.
|
2012-02-06 10:06:22 -08:00
|
|
|
//
|
|
|
|
|
// If an argument has type *interface{}, Scan copies the value
|
|
|
|
|
// provided by the underlying driver without conversion. If the value
|
|
|
|
|
// is of type []byte, a copy is made and the caller owns the result.
|
2011-11-01 22:04:37 -04:00
|
|
|
func (rs *Rows) Scan(dest ...interface{}) error {
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
if rs.closed {
|
2013-08-16 11:23:35 +10:00
|
|
|
return errors.New("sql: Rows are closed")
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
if rs.lastcols == nil {
|
2011-12-15 10:14:57 -08:00
|
|
|
return errors.New("sql: Scan called without calling Next")
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
if len(dest) != len(rs.lastcols) {
|
2011-12-15 10:14:57 -08:00
|
|
|
return fmt.Errorf("sql: expected %d destination arguments in Scan, not %d", len(rs.lastcols), len(dest))
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
for i, sv := range rs.lastcols {
|
|
|
|
|
err := convertAssign(dest[i], sv)
|
|
|
|
|
if err != nil {
|
2011-12-15 10:14:57 -08:00
|
|
|
return fmt.Errorf("sql: Scan error on column index %d: %v", i, err)
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-13 14:56:40 -07:00
|
|
|
var rowsCloseHook func(*Rows, *error)
|
|
|
|
|
|
2013-08-16 11:23:35 +10:00
|
|
|
// Close closes the Rows, preventing further enumeration. If Next returns
|
|
|
|
|
// false, the Rows are closed automatically and it will suffice to check the
|
|
|
|
|
// result of Err. Close is idempotent and does not affect the result of Err.
|
2011-11-01 22:04:37 -04:00
|
|
|
func (rs *Rows) Close() error {
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
if rs.closed {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
rs.closed = true
|
|
|
|
|
err := rs.rowsi.Close()
|
2013-08-13 14:56:40 -07:00
|
|
|
if fn := rowsCloseHook; fn != nil {
|
|
|
|
|
fn(rs, &err)
|
|
|
|
|
}
|
2012-01-13 15:25:07 -08:00
|
|
|
if rs.closeStmt != nil {
|
|
|
|
|
rs.closeStmt.Close()
|
|
|
|
|
}
|
2013-04-15 14:06:41 -07:00
|
|
|
rs.releaseConn(err)
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Row is the result of calling QueryRow to select a single row.
|
|
|
|
|
type Row struct {
|
|
|
|
|
// One of these two will be non-nil:
|
2011-11-01 22:04:37 -04:00
|
|
|
err error // deferred error for easy chaining
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
rows *Rows
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Scan copies the columns from the matched row into the values
|
|
|
|
|
// pointed at by dest. If more than one row matches the query,
|
|
|
|
|
// Scan uses the first row and discards the rest. If no row matches
|
|
|
|
|
// the query, Scan returns ErrNoRows.
|
2011-11-01 22:04:37 -04:00
|
|
|
func (r *Row) Scan(dest ...interface{}) error {
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
if r.err != nil {
|
|
|
|
|
return r.err
|
|
|
|
|
}
|
2012-01-12 11:23:33 -08:00
|
|
|
|
|
|
|
|
// TODO(bradfitz): for now we need to defensively clone all
|
2012-03-10 15:21:44 -08:00
|
|
|
// []byte that the driver returned (not permitting
|
2012-01-26 15:12:48 -08:00
|
|
|
// *RawBytes in Rows.Scan), since we're about to close
|
2012-01-12 11:23:33 -08:00
|
|
|
// the Rows in our defer, when we return from this function.
|
|
|
|
|
// the contract with the driver.Next(...) interface is that it
|
|
|
|
|
// can return slices into read-only temporary memory that's
|
|
|
|
|
// only valid until the next Scan/Close. But the TODO is that
|
|
|
|
|
// for a lot of drivers, this copy will be unnecessary. We
|
|
|
|
|
// should provide an optional interface for drivers to
|
|
|
|
|
// implement to say, "don't worry, the []bytes that I return
|
|
|
|
|
// from Next will not be modified again." (for instance, if
|
|
|
|
|
// they were obtained from the network anyway) But for now we
|
|
|
|
|
// don't care.
|
2013-10-16 09:17:25 -07:00
|
|
|
defer r.rows.Close()
|
2012-01-12 11:23:33 -08:00
|
|
|
for _, dp := range dest {
|
2012-01-17 10:44:35 -08:00
|
|
|
if _, ok := dp.(*RawBytes); ok {
|
|
|
|
|
return errors.New("sql: RawBytes isn't allowed on Row.Scan")
|
|
|
|
|
}
|
2012-01-12 11:23:33 -08:00
|
|
|
}
|
2012-01-26 15:12:48 -08:00
|
|
|
|
|
|
|
|
if !r.rows.Next() {
|
2013-12-16 12:48:35 -08:00
|
|
|
if err := r.rows.Err(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2012-01-26 15:12:48 -08:00
|
|
|
return ErrNoRows
|
|
|
|
|
}
|
|
|
|
|
err := r.rows.Scan(dest...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2013-12-16 12:48:35 -08:00
|
|
|
// Make sure the query can be processed to completion with no errors.
|
|
|
|
|
if err := r.rows.Close(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2012-01-26 15:12:48 -08:00
|
|
|
|
2012-01-12 11:23:33 -08:00
|
|
|
return nil
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A Result summarizes an executed SQL command.
|
|
|
|
|
type Result interface {
|
2013-10-29 17:38:43 -07:00
|
|
|
// LastInsertId returns the integer generated by the database
|
|
|
|
|
// in response to a command. Typically this will be from an
|
|
|
|
|
// "auto increment" column when inserting a new row. Not all
|
|
|
|
|
// databases support this feature, and the syntax of such
|
|
|
|
|
// statements varies.
|
2011-11-01 22:04:37 -04:00
|
|
|
LastInsertId() (int64, error)
|
2013-10-29 17:38:43 -07:00
|
|
|
|
|
|
|
|
// RowsAffected returns the number of rows affected by an
|
|
|
|
|
// update, insert, or delete. Not every database or database
|
|
|
|
|
// driver may support this.
|
2011-11-01 22:04:37 -04:00
|
|
|
RowsAffected() (int64, error)
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
2013-03-14 15:01:45 -07:00
|
|
|
type driverResult struct {
|
|
|
|
|
sync.Locker // the *driverConn
|
|
|
|
|
resi driver.Result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (dr driverResult) LastInsertId() (int64, error) {
|
|
|
|
|
dr.Lock()
|
|
|
|
|
defer dr.Unlock()
|
|
|
|
|
return dr.resi.LastInsertId()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (dr driverResult) RowsAffected() (int64, error) {
|
|
|
|
|
dr.Lock()
|
|
|
|
|
defer dr.Unlock()
|
|
|
|
|
return dr.resi.RowsAffected()
|
exp/sql{,/driver}: new database packages
R=gustavo, rsc, borman, dave, kevlar, nigeltao, dvyukov, kardianos, fw, r, r, david.crawshaw
CC=golang-dev
https://golang.org/cl/4973055
2011-09-29 16:12:21 -07:00
|
|
|
}
|
2013-02-20 15:35:27 -08:00
|
|
|
|
|
|
|
|
func stack() string {
|
database/sql: fix driver Conn refcounting with prepared statements
The refcounting of driver Conns was completedly busted and
would leak (be held open forever) with any reasonable
load. This was a significant regression from Go 1.0.
The core of this patch is removing one line:
s.db.addDep(dc, s)
A database conn (dc) is a resource that be re-created any time
(but cached for speed) should not be held open forever with a
dependency refcount just because the Stmt (s) is alive (which
typically last for long periods of time, like forever).
The meat of the patch is new tests. In fixing the real issue,
a lot of tests then failed due to the fakedb_test.go's paranoia
about closing a fakeConn while it has open fakeStmts on it. I
could've ignored that, but that's been a problem in the past for
other bugs.
Instead, I now track per-Conn open statements and close them
when the the conn closes. The proper way to do this would've
been making *driverStmt a finalCloser and using the dep mechanism,
but it was much more invasive. Added a TODO instead.
I'd like to give a way for drivers to opt-out of caring about
driver.Stmt closes before a driver.Conn close, but that's a TODO
for the future, and that TODO is added in this CL.
I know this is very late for Go 1.1, but database/sql is
currently nearly useless without this.
I'd like to believe all these database/sql bugs in the past
release cycle are the result of increased usage, number of
drivers, and good feedback from increasingly-capable Go
developers, and not the result of me sucking. It's also hard
with all the real drivers being out-of-tree, so I'm having to
add more and more hooks to fakedb_test.go to simulate things
which real drivers end up doing.
Fixes #5323
R=golang-dev, snaury, gwenn.kahz, google, r
CC=golang-dev
https://golang.org/cl/8836045
2013-04-25 14:45:56 -07:00
|
|
|
var buf [2 << 10]byte
|
2013-02-20 15:35:27 -08:00
|
|
|
return string(buf[:runtime.Stack(buf[:], false)])
|
|
|
|
|
}
|
2013-03-14 15:01:45 -07:00
|
|
|
|
|
|
|
|
// withLock runs while holding lk.
|
|
|
|
|
func withLock(lk sync.Locker, fn func()) {
|
|
|
|
|
lk.Lock()
|
|
|
|
|
fn()
|
|
|
|
|
lk.Unlock()
|
|
|
|
|
}
|