do not close wrapped *sql.DB (#1985)

This commit is contained in:
antness 2018-07-27 02:43:09 +03:00 committed by Jinzhu
parent 588e2eef5d
commit d68403b29d
1 changed files with 4 additions and 1 deletions

View File

@ -48,6 +48,7 @@ func Open(dialect string, args ...interface{}) (db *DB, err error) {
} }
var source string var source string
var dbSQL SQLCommon var dbSQL SQLCommon
var ownDbSQL bool
switch value := args[0].(type) { switch value := args[0].(type) {
case string: case string:
@ -59,8 +60,10 @@ func Open(dialect string, args ...interface{}) (db *DB, err error) {
source = args[1].(string) source = args[1].(string)
} }
dbSQL, err = sql.Open(driver, source) dbSQL, err = sql.Open(driver, source)
ownDbSQL = true
case SQLCommon: case SQLCommon:
dbSQL = value dbSQL = value
ownDbSQL = false
default: default:
return nil, fmt.Errorf("invalid database source: %v is not a valid type", value) return nil, fmt.Errorf("invalid database source: %v is not a valid type", value)
} }
@ -78,7 +81,7 @@ func Open(dialect string, args ...interface{}) (db *DB, err error) {
} }
// Send a ping to make sure the database connection is alive. // Send a ping to make sure the database connection is alive.
if d, ok := dbSQL.(*sql.DB); ok { if d, ok := dbSQL.(*sql.DB); ok {
if err = d.Ping(); err != nil { if err = d.Ping(); err != nil && ownDbSQL {
d.Close() d.Close()
} }
} }