Skip to content

Commit 641ad08

Browse files
committed
fix thread safety issue
1 parent a00980d commit 641ad08

File tree

4 files changed

+13
-40
lines changed

4 files changed

+13
-40
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## 0.1.x (Unreleased)
44

5+
- Fix thread safety issue in connector
6+
57
## 0.2.0 (2022-11-18)
68

79
- Support for DirectResults

connector.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ import (
1515
)
1616

1717
type connector struct {
18-
cfg *config.Config
19-
client cli_service.TCLIService
18+
cfg *config.Config
2019
}
2120

2221
func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
@@ -29,8 +28,13 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
2928
schemaName = cli_service.TIdentifierPtr(cli_service.TIdentifier(c.cfg.Schema))
3029
}
3130

31+
tclient, err := client.InitThriftClient(c.cfg)
32+
if err != nil {
33+
return nil, wrapErr(err, "error initializing thrift client")
34+
}
35+
3236
// we need to ensure that open session will eventually end
33-
session, err := c.client.OpenSession(ctx, &cli_service.TOpenSessionReq{
37+
session, err := tclient.OpenSession(ctx, &cli_service.TOpenSessionReq{
3438
ClientProtocol: c.cfg.ThriftProtocolVersion,
3539
Configuration: make(map[string]string),
3640
InitialNamespace: &cli_service.TNamespace{
@@ -47,7 +51,7 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
4751
conn := &conn{
4852
id: client.SprintGuid(session.SessionHandle.GetSessionId().GUID),
4953
cfg: c.cfg,
50-
client: c.client,
54+
client: tclient,
5155
session: session,
5256
}
5357
log := logger.WithContext(conn.id, driverctx.CorrelationIdFromContext(ctx), "")
@@ -82,13 +86,8 @@ func NewConnector(options ...connOption) (driver.Connector, error) {
8286
for _, opt := range options {
8387
opt(cfg)
8488
}
85-
// validate config?
86-
tclient, err := client.InitThriftClient(cfg)
87-
if err != nil {
88-
return nil, wrapErr(err, "error initializing thrift client")
89-
}
9089

91-
return &connector{cfg, tclient}, nil
90+
return &connector{cfg: cfg}, nil
9291
}
9392

9493
// WithServerHostname sets up the server hostname. Mandatory.

connector_test.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,14 @@
11
package dbsql
22

33
import (
4-
"context"
54
"testing"
65
"time"
76

8-
"github.com/databricks/databricks-sql-go/internal/cli_service"
9-
"github.com/databricks/databricks-sql-go/internal/client"
107
"github.com/databricks/databricks-sql-go/internal/config"
118
"github.com/stretchr/testify/assert"
129
"github.com/stretchr/testify/require"
1310
)
1411

15-
func TestConnector_Connect(t *testing.T) {
16-
t.Run("Connect returns err when thrift client initialization fails", func(t *testing.T) {
17-
cfg := config.WithDefaults()
18-
var openSessionCount int
19-
openSession := func(ctx context.Context, req *cli_service.TOpenSessionReq) (r *cli_service.TOpenSessionResp, err error) {
20-
openSessionCount++
21-
return getTestSession(), nil
22-
}
23-
testClient := &client.TestClient{
24-
FnOpenSession: openSession,
25-
}
26-
testConnector := connector{
27-
cfg: cfg,
28-
client: testClient,
29-
}
30-
conn, err := testConnector.Connect(context.Background())
31-
assert.NotNil(t, conn)
32-
assert.NoError(t, err)
33-
})
34-
}
35-
3612
func TestNewConnector(t *testing.T) {
3713
t.Run("Connector initialized with functional options should have all options set", func(t *testing.T) {
3814
host := "databricks-host"

driver.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"database/sql"
66
"database/sql/driver"
77

8-
"github.com/databricks/databricks-sql-go/internal/client"
98
"github.com/databricks/databricks-sql-go/internal/config"
109
_ "github.com/databricks/databricks-sql-go/logger"
1110
)
@@ -36,11 +35,8 @@ func (d *databricksDriver) OpenConnector(dsn string) (driver.Connector, error) {
3635
return nil, err
3736
}
3837
cfg.UserConfig = ucfg
39-
tclient, err := client.InitThriftClient(cfg)
40-
if err != nil {
41-
return nil, wrapErr(err, "error initializing thrift client")
42-
}
43-
return &connector{cfg, tclient}, nil
38+
39+
return &connector{cfg: cfg}, nil
4440
}
4541

4642
var _ driver.Driver = (*databricksDriver)(nil)

0 commit comments

Comments
 (0)