Skip to content

Commit 6fd1444

Browse files
committed
* Fixed closing of database/sql connection (aka YDB session)
1 parent 3517f26 commit 6fd1444

File tree

5 files changed

+37
-42
lines changed

5 files changed

+37
-42
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Fixed closing of database/sql connection (aka `YDB` session)
2+
13
## v3.42.4
24
* Added `ydb.WithDisableServerBalancer()` database/sql connector option
35

internal/xsql/conn.go

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ type conn struct {
6969
}
7070

7171
func (c *conn) IsValid() bool {
72-
return !c.isClosed()
72+
return c.isReady()
7373
}
7474

7575
type currentTx interface {
@@ -87,7 +87,6 @@ var (
8787
_ driver.QueryerContext = &conn{}
8888
_ driver.Pinger = &conn{}
8989
_ driver.NamedValueChecker = &conn{}
90-
_ driver.SessionResetter = &conn{}
9190
_ driver.Validator = &conn{}
9291
)
9392

@@ -109,7 +108,7 @@ func (c *conn) checkClosed(err error) error {
109108
return err
110109
}
111110

112-
func (c *conn) isClosed() bool {
111+
func (c *conn) isReady() bool {
113112
if atomic.LoadUint32(&c.closed) == 1 {
114113
return true
115114
}
@@ -129,8 +128,8 @@ func (c *conn) PrepareContext(ctx context.Context, query string) (_ driver.Stmt,
129128
defer func() {
130129
onDone(err)
131130
}()
132-
if c.isClosed() {
133-
return nil, errClosedConn
131+
if !c.isReady() {
132+
return nil, errNotReadyConn
134133
}
135134
return &stmt{
136135
conn: c,
@@ -203,8 +202,8 @@ func (c *conn) execContext(ctx context.Context, query string, args []driver.Name
203202
}
204203

205204
func (c *conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (_ driver.Result, err error) {
206-
if c.isClosed() {
207-
return nil, errClosedConn
205+
if !c.isReady() {
206+
return nil, errNotReadyConn
208207
}
209208
if c.currentTx != nil {
210209
return c.currentTx.ExecContext(ctx, query, args)
@@ -213,8 +212,8 @@ func (c *conn) ExecContext(ctx context.Context, query string, args []driver.Name
213212
}
214213

215214
func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (_ driver.Rows, err error) {
216-
if c.isClosed() {
217-
return nil, errClosedConn
215+
if !c.isReady() {
216+
return nil, errNotReadyConn
218217
}
219218
if c.currentTx != nil {
220219
return c.currentTx.QueryContext(ctx, query, args)
@@ -307,8 +306,8 @@ func (c *conn) Ping(ctx context.Context) (err error) {
307306
defer func() {
308307
onDone(err)
309308
}()
310-
if c.isClosed() {
311-
return errClosedConn
309+
if !c.isReady() {
310+
return errNotReadyConn
312311
}
313312
if err = c.session.KeepAlive(ctx); err != nil {
314313
return c.checkClosed(xerrors.WithStackTrace(err))
@@ -317,15 +316,18 @@ func (c *conn) Ping(ctx context.Context) (err error) {
317316
}
318317

319318
func (c *conn) Close() (err error) {
320-
onDone := trace.DatabaseSQLOnConnClose(c.trace)
321-
defer func() {
322-
onDone(err)
323-
}()
324-
err = c.session.Close(context.Background())
325-
if err != nil {
326-
return c.checkClosed(xerrors.WithStackTrace(err))
319+
if atomic.CompareAndSwapUint32(&c.closed, 0, 1) {
320+
onDone := trace.DatabaseSQLOnConnClose(c.trace)
321+
defer func() {
322+
onDone(err)
323+
}()
324+
err = c.session.Close(context.Background())
325+
if err != nil {
326+
return badconn.Map(xerrors.WithStackTrace(err))
327+
}
328+
return nil
327329
}
328-
return nil
330+
return errClosedConn
329331
}
330332

331333
func (c *conn) Prepare(string) (driver.Stmt, error) {
@@ -342,8 +344,8 @@ func (c *conn) BeginTx(ctx context.Context, txOptions driver.TxOptions) (_ drive
342344
defer func() {
343345
onDone(transaction, err)
344346
}()
345-
if c.isClosed() {
346-
return nil, errClosedConn
347+
if !c.isReady() {
348+
return nil, errNotReadyConn
347349
}
348350
if c.currentTx != nil {
349351
return nil, xerrors.WithStackTrace(
@@ -366,13 +368,3 @@ func (c *conn) BeginTx(ctx context.Context, txOptions driver.TxOptions) (_ drive
366368
}
367369
return c.currentTx, nil
368370
}
369-
370-
func (c *conn) ResetSession(_ context.Context) error {
371-
if c.currentTx != nil {
372-
_ = c.currentTx.Rollback()
373-
}
374-
if c.isClosed() {
375-
return errClosedConn
376-
}
377-
return nil
378-
}

internal/xsql/errors.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import (
99
)
1010

1111
var (
12-
ErrUnsupported = driver.ErrSkip
13-
errDeprecated = driver.ErrSkip
14-
errClosedConn = badconn.Map(xerrors.Retryable(errors.New("conn closed early"), xerrors.WithDeleteSession()))
12+
ErrUnsupported = driver.ErrSkip
13+
errDeprecated = driver.ErrSkip
14+
errClosedConn = badconn.Map(xerrors.Retryable(errors.New("conn closed early"), xerrors.WithDeleteSession()))
15+
errNotReadyConn = badconn.Map(xerrors.Retryable(errors.New("conn not ready"), xerrors.WithDeleteSession()))
1516
)

internal/xsql/stmt.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (_ dr
2626
defer func() {
2727
onDone(err)
2828
}()
29-
if s.conn.isClosed() {
30-
return nil, errClosedConn
29+
if !s.conn.isReady() {
30+
return nil, errNotReadyConn
3131
}
3232
switch m := queryModeFromContext(ctx, s.conn.defaultQueryMode); m {
3333
case DataQueryMode:
@@ -42,8 +42,8 @@ func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (_ dri
4242
defer func() {
4343
onDone(err)
4444
}()
45-
if s.conn.isClosed() {
46-
return nil, errClosedConn
45+
if !s.conn.isReady() {
46+
return nil, errNotReadyConn
4747
}
4848
switch m := queryModeFromContext(ctx, s.conn.defaultQueryMode); m {
4949
case DataQueryMode:

internal/xsql/tx.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ func (tx *tx) Commit() (err error) {
3636
defer func() {
3737
tx.conn.currentTx = nil
3838
}()
39-
if tx.conn.isClosed() {
40-
return errClosedConn
39+
if !tx.conn.isReady() {
40+
return errNotReadyConn
4141
}
4242
_, err = tx.tx.CommitTx(tx.ctx)
4343
if err != nil {
@@ -54,8 +54,8 @@ func (tx *tx) Rollback() (err error) {
5454
defer func() {
5555
tx.conn.currentTx = nil
5656
}()
57-
if tx.conn.isClosed() {
58-
return errClosedConn
57+
if !tx.conn.isReady() {
58+
return errNotReadyConn
5959
}
6060
err = tx.tx.Rollback(tx.ctx)
6161
if err != nil {

0 commit comments

Comments
 (0)