1
0
mirror of https://github.com/golang/go synced 2024-11-19 11:04:47 -07:00

database/sql: make Register safe for concurrent use

Adding a mutex was easier than documenting it, and is consistent with
gob.

Fixes #9847

Change-Id: Ifa94c17e7c11643add81b35431ef840b794d78b1
Reviewed-on: https://go-review.googlesource.com/11682
Reviewed-by: Andrew Gerrand <adg@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Brad Fitzpatrick 2015-06-29 17:56:20 -07:00
parent 300d9a2158
commit fc2eee87ed

View File

@ -23,12 +23,17 @@ import (
"sync/atomic"
)
var drivers = make(map[string]driver.Driver)
var (
driversMu sync.Mutex
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) {
driversMu.Lock()
defer driversMu.Unlock()
if driver == nil {
panic("sql: Register driver is nil")
}
@ -39,12 +44,16 @@ func Register(name string, driver driver.Driver) {
}
func unregisterAllDrivers() {
driversMu.Lock()
defer driversMu.Unlock()
// For tests.
drivers = make(map[string]driver.Driver)
}
// Drivers returns a sorted list of the names of the registered drivers.
func Drivers() []string {
driversMu.Lock()
defer driversMu.Unlock()
var list []string
for name := range drivers {
list = append(list, name)
@ -457,7 +466,9 @@ var connectionRequestQueueSize = 1000000
// function should be called just once. It is rarely necessary to
// close a DB.
func Open(driverName, dataSourceName string) (*DB, error) {
driversMu.Lock()
driveri, ok := drivers[driverName]
driversMu.Unlock()
if !ok {
return nil, fmt.Errorf("sql: unknown driver %q (forgotten import?)", driverName)
}