From c88d81a2fdb171021e7ac337868769539000f389 Mon Sep 17 00:00:00 2001 From: mpl Date: Wed, 6 Jan 2016 20:42:04 +0100 Subject: [PATCH] sorted/mysql: remove DB from pool when closing Otherwise when openOrCachedDB is called for the second time, it would return an actually closed DB. Change-Id: I820834e508fed1a34ab7bd19a8a91e398badff9c --- pkg/sorted/mysql/mysqlkv.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/pkg/sorted/mysql/mysqlkv.go b/pkg/sorted/mysql/mysqlkv.go index bc90424ef..13873bb96 100644 --- a/pkg/sorted/mysql/mysqlkv.go +++ b/pkg/sorted/mysql/mysqlkv.go @@ -95,7 +95,8 @@ func newKeyValueFromJSONConfig(cfg jsonconfig.Obj) (sorted.KeyValue, error) { } kv := &keyValue{ - db: db, + dsn: dsn, + db: db, KeyValue: &sqlkv.KeyValue{ DB: db, TablePrefix: database + ".", @@ -158,7 +159,17 @@ func openOrCachedDB(dsn string) (*sql.DB, error) { type keyValue struct { *sqlkv.KeyValue - db *sql.DB + dsn string + db *sql.DB +} + +// Close overrides KeyValue.Close because we need to remove the DB from the pool +// when closing. +func (kv *keyValue) Close() error { + dbsmu.Lock() + defer dbsmu.Unlock() + delete(dbs, kv.dsn) + return kv.DB.Close() } func (kv *keyValue) ping() error {