Skip to content

Commit

Permalink
🐛 Bugfix: set max idle connection time
Browse files Browse the repository at this point in the history
  • Loading branch information
mullerpeter committed Sep 17, 2023
1 parent be414a1 commit 6ee6a46
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 43 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Changelog

## 1.1.6
## 1.1.7

- Bugfix: Close idle databricks connections after 6 hours

---

### 1.1.6

- Bugfix: Correctly scope Databricks Connection to Datasource instance, in order to support multiple Databricks Datasources

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mullerpeter-databricks-datasource",
"private": true,
"version": "1.1.6",
"version": "1.1.7",
"description": "Databricks SQL Connector",
"scripts": {
"build": "grafana-toolkit plugin:build",
Expand Down
44 changes: 3 additions & 41 deletions pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
_ "github.com/databricks/databricks-sql-go"
"github.com/grafana/grafana-plugin-sdk-go/backend"
Expand All @@ -13,7 +12,6 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
"reflect"
"strings"
"time"
)

Expand Down Expand Up @@ -59,6 +57,7 @@ func NewSampleDatasource(settings backend.DataSourceInstanceSettings) (instancem
log.DefaultLogger.Info("DB Init Error", "err", err)
} else {
databricksDB = db
databricksDB.SetConnMaxIdleTime(6 * time.Hour)
log.DefaultLogger.Info("Store Databricks SQL DB Connection")
}
}
Expand All @@ -69,43 +68,6 @@ func NewSampleDatasource(settings backend.DataSourceInstanceSettings) (instancem
}, nil
}

func (d *SampleDatasource) RefreshDBConnection() error {
if d.databricksConnectionsString != "" {
log.DefaultLogger.Info("Refreshing Databricks SQL DB Connection")
db, err := sql.Open("databricks", d.databricksConnectionsString)
if err != nil {
log.DefaultLogger.Info("DB Init Error", "err", err)
return err
} else {
d.databricksDB = db
log.DefaultLogger.Info("Store Databricks SQL DB Connection")
return nil
}
}

return errors.New("no connection string set")
}

func (d *SampleDatasource) ExecuteQuery(queryString string) (*sql.Rows, error) {
rows, err := d.databricksDB.Query(queryString)
if err != nil {
if strings.HasPrefix(err.Error(), "Invalid SessionHandle") {
err = d.RefreshDBConnection()
if err != nil {
return nil, err
}
rows, err = d.databricksDB.Query(queryString)
if err != nil {
return nil, err
}
} else {
return nil, err
}
}

return rows, nil
}

// SampleDatasource is an example datasource which can respond to data queries, reports
// its health and has streaming skills.
type SampleDatasource struct {
Expand Down Expand Up @@ -173,7 +135,7 @@ func (d *SampleDatasource) query(_ context.Context, pCtx backend.PluginContext,

frame := data.NewFrame("response")

rows, err := d.ExecuteQuery(queryString)
rows, err := d.databricksDB.Query(queryString)
if err != nil {
response.Error = err
log.DefaultLogger.Info("Error", "err", err)
Expand Down Expand Up @@ -242,7 +204,7 @@ func (d *SampleDatasource) CheckHealth(_ context.Context, req *backend.CheckHeal
}, nil
}

rows, err := d.ExecuteQuery("SELECT 1")
rows, err := d.databricksDB.Query("SELECT 1")

if err != nil {
return &backend.CheckHealthResult{
Expand Down

0 comments on commit 6ee6a46

Please sign in to comment.