mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
database/sql: document non-open of Open; add Ping
Fixes #4804 R=golang-dev, r CC=golang-dev https://golang.org/cl/7819043
This commit is contained in:
parent
cf46e561b2
commit
a4a8651419
1 changed files with 18 additions and 2 deletions
|
|
@ -258,13 +258,15 @@ func (db *DB) removeDep(x finalCloser, dep interface{}) error {
|
||||||
//
|
//
|
||||||
// Most users will open a database via a driver-specific connection
|
// Most users will open a database via a driver-specific connection
|
||||||
// helper function that returns a *DB.
|
// helper function that returns a *DB.
|
||||||
|
//
|
||||||
|
// Open may just validate its arguments without creating a connection
|
||||||
|
// to the database. To verify that the data source name is valid, call
|
||||||
|
// Ping.
|
||||||
func Open(driverName, dataSourceName string) (*DB, error) {
|
func Open(driverName, dataSourceName string) (*DB, error) {
|
||||||
driveri, ok := drivers[driverName]
|
driveri, ok := drivers[driverName]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("sql: unknown driver %q (forgotten import?)", driverName)
|
return nil, fmt.Errorf("sql: unknown driver %q (forgotten import?)", driverName)
|
||||||
}
|
}
|
||||||
// TODO: optionally proactively connect to a Conn to check
|
|
||||||
// the dataSourceName: golang.org/issue/4804
|
|
||||||
db := &DB{
|
db := &DB{
|
||||||
driver: driveri,
|
driver: driveri,
|
||||||
dsn: dataSourceName,
|
dsn: dataSourceName,
|
||||||
|
|
@ -275,6 +277,20 @@ func Open(driverName, dataSourceName string) (*DB, error) {
|
||||||
return db, nil
|
return db, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
c, err := db.conn()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
db.putConn(c, nil)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Close closes the database, releasing any open resources.
|
// Close closes the database, releasing any open resources.
|
||||||
func (db *DB) Close() error {
|
func (db *DB) Close() error {
|
||||||
db.mu.Lock()
|
db.mu.Lock()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue