Skip to content

Commit 0b49f12

Browse files
authored
fix: implement InstanceDisposer to close db connections (#136)
* fix: implement InstanceDisposer to close db connections * fix: adapt Dispose() test to new functionality
1 parent 0c5cd13 commit 0b49f12

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

connector.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ func (ds *Connector) storeDBConnection(key string, dbConn dbConnection) {
134134
ds.connections.Store(key, dbConn)
135135
}
136136

137+
// Dispose is called when an existing SQLDatasource needs to be replaced
138+
func (c *Connector) Dispose() {
139+
c.connections.Range(func(_, conn interface{}) bool {
140+
_ = conn.(dbConnection).db.Close()
141+
return true
142+
})
143+
c.connections.Clear()
144+
}
145+
137146
func (c *Connector) GetConnectionFromQuery(ctx context.Context, q *Query) (string, dbConnection, error) {
138147
if !c.enableMultipleConnections && !c.driverSettings.ForwardHeaders && len(q.ConnectionArgs) > 0 {
139148
return "", dbConnection{}, MissingMultipleConnectionsConfig

datasource.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func NewDatasource(c Driver) *SQLDatasource {
8282
// Dispose cleans up datasource instance resources.
8383
// Note: Called when testing and saving a datasource
8484
func (ds *SQLDatasource) Dispose() {
85+
ds.connector.Dispose()
8586
}
8687

8788
// QueryData creates the Responses list and executes each query

datasource_connect_test.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sqlds
33
import (
44
"context"
55
"database/sql"
6+
"database/sql/driver"
67
"encoding/json"
78
"errors"
89
"testing"
@@ -29,6 +30,16 @@ func (d fakeDriver) Converters() []sqlutil.Converter {
2930
return []sqlutil.Converter{}
3031
}
3132

33+
type fakeSQLConnector struct{}
34+
35+
func (f fakeSQLConnector) Connect(_ context.Context) (driver.Conn, error) {
36+
return nil, nil
37+
}
38+
39+
func (f fakeSQLConnector) Driver() driver.Driver {
40+
return nil
41+
}
42+
3243
func Test_getDBConnectionFromQuery(t *testing.T) {
3344
db := &sql.DB{}
3445
db2 := &sql.DB{}
@@ -108,20 +119,21 @@ func Test_getDBConnectionFromQuery(t *testing.T) {
108119
}
109120

110121
func Test_Dispose(t *testing.T) {
111-
t.Run("it should not delete connections", func(t *testing.T) {
112-
conn := &Connector{}
113-
122+
t.Run("it should close connections", func(t *testing.T) {
123+
db := sql.OpenDB(fakeSQLConnector{})
124+
d := &fakeDriver{openDBfn: func(msg json.RawMessage) (*sql.DB, error) { return db, nil }}
125+
conn := &Connector{driver: d}
114126
ds := &SQLDatasource{connector: conn}
115-
conn.connections.Store(defaultKey("uid1"), dbConnection{})
116-
conn.connections.Store("foo", dbConnection{})
127+
conn.connections.Store(defaultKey("uid1"), dbConnection{db: db})
128+
conn.connections.Store("foo", dbConnection{db: db})
117129
ds.Dispose()
118130
count := 0
119131
conn.connections.Range(func(key, value interface{}) bool {
120132
count++
121133
return true
122134
})
123-
if count != 2 {
124-
t.Errorf("missing connections")
135+
if count != 0 {
136+
t.Errorf("did not close all connections")
125137
}
126138
})
127139
}

0 commit comments

Comments
 (0)