Skip to content

Commit

Permalink
do not close wrapped *sql.DB (go-gorm#1985)
Browse files Browse the repository at this point in the history
  • Loading branch information
antness authored and jinzhu committed Jul 26, 2018
1 parent 588e2ee commit d68403b
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func Open(dialect string, args ...interface{}) (db *DB, err error) {
}
var source string
var dbSQL SQLCommon
var ownDbSQL bool

switch value := args[0].(type) {
case string:
Expand All @@ -59,8 +60,10 @@ func Open(dialect string, args ...interface{}) (db *DB, err error) {
source = args[1].(string)
}
dbSQL, err = sql.Open(driver, source)
ownDbSQL = true
case SQLCommon:
dbSQL = value
ownDbSQL = false
default:
return nil, fmt.Errorf("invalid database source: %v is not a valid type", value)
}
Expand All @@ -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.
if d, ok := dbSQL.(*sql.DB); ok {
if err = d.Ping(); err != nil {
if err = d.Ping(); err != nil && ownDbSQL {
d.Close()
}
}
Expand Down

0 comments on commit d68403b

Please sign in to comment.