Skip to content

Commit 67c6fc5

Browse files
authored
Merge pull request #1294 from ydb-platform/wrap-bad-session
wrapped error BAD_SESSION in query session calls
2 parents 96864d9 + 45abf4e commit 67c6fc5

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

internal/query/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func defaults() *Config {
4343
sessionCreateTimeout: DefaultSessionCreateTimeout,
4444
sessionDeleteTimeout: DefaultSessionDeleteTimeout,
4545
trace: &trace.Query{},
46+
useSessionPool: false,
4647
}
4748
}
4849

internal/query/session.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"sync/atomic"
77

88
"github.com/ydb-platform/ydb-go-genproto/Ydb_Query_V1"
9+
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
910
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Query"
1011

1112
"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
@@ -78,6 +79,16 @@ func createSession(ctx context.Context, client Ydb_Query_V1.QueryServiceClient,
7879
cfg: cfg,
7980
grpcClient: client,
8081
statusCode: statusUnknown,
82+
checks: []func(s *Session) bool{
83+
func(s *Session) bool {
84+
switch s.status() {
85+
case statusClosed, statusClosing, statusError:
86+
return false
87+
default:
88+
return true
89+
}
90+
},
91+
},
8192
}
8293
defer func() {
8394
if finalErr != nil && s != nil {
@@ -263,6 +274,10 @@ func (s *Session) Begin(
263274

264275
tx, err = begin(ctx, s.grpcClient, s, txSettings)
265276
if err != nil {
277+
if xerrors.IsOperationError(err, Ydb.StatusIds_BAD_SESSION) {
278+
s.setStatus(statusClosed)
279+
}
280+
266281
return nil, xerrors.WithStackTrace(err)
267282
}
268283
tx.s = s
@@ -301,6 +316,10 @@ func (s *Session) Execute(
301316

302317
tx, r, err := execute(ctx, s, s.grpcClient, q, options.ExecuteSettings(opts...))
303318
if err != nil {
319+
if xerrors.IsOperationError(err, Ydb.StatusIds_BAD_SESSION) {
320+
s.setStatus(statusClosed)
321+
}
322+
304323
return nil, nil, xerrors.WithStackTrace(err)
305324
}
306325

internal/query/transaction.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55

66
"github.com/ydb-platform/ydb-go-genproto/Ydb_Query_V1"
7+
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
78
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Query"
89

910
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query/options"
@@ -80,6 +81,10 @@ func (tx transaction) Execute(ctx context.Context, q string, opts ...options.TxE
8081

8182
_, res, err := execute(ctx, tx.s, tx.s.grpcClient, q, options.TxExecuteSettings(tx.ID(), opts...).ExecuteSettings)
8283
if err != nil {
84+
if xerrors.IsOperationError(err) {
85+
tx.s.setStatus(statusClosed)
86+
}
87+
8388
return nil, xerrors.WithStackTrace(err)
8489
}
8590

@@ -98,8 +103,17 @@ func commitTx(ctx context.Context, client Ydb_Query_V1.QueryServiceClient, sessi
98103
return nil
99104
}
100105

101-
func (tx transaction) CommitTx(ctx context.Context) (err error) {
102-
return commitTx(ctx, tx.s.grpcClient, tx.s.id, tx.ID())
106+
func (tx transaction) CommitTx(ctx context.Context) error {
107+
err := commitTx(ctx, tx.s.grpcClient, tx.s.id, tx.ID())
108+
if err != nil {
109+
if xerrors.IsOperationError(err, Ydb.StatusIds_BAD_SESSION) {
110+
tx.s.setStatus(statusClosed)
111+
}
112+
113+
return xerrors.WithStackTrace(err)
114+
}
115+
116+
return nil
103117
}
104118

105119
func rollback(ctx context.Context, client Ydb_Query_V1.QueryServiceClient, sessionID, txID string) error {
@@ -114,6 +128,15 @@ func rollback(ctx context.Context, client Ydb_Query_V1.QueryServiceClient, sessi
114128
return nil
115129
}
116130

117-
func (tx transaction) Rollback(ctx context.Context) (err error) {
118-
return rollback(ctx, tx.s.grpcClient, tx.s.id, tx.ID())
131+
func (tx transaction) Rollback(ctx context.Context) error {
132+
err := rollback(ctx, tx.s.grpcClient, tx.s.id, tx.ID())
133+
if err != nil {
134+
if xerrors.IsOperationError(err, Ydb.StatusIds_BAD_SESSION) {
135+
tx.s.setStatus(statusClosed)
136+
}
137+
138+
return xerrors.WithStackTrace(err)
139+
}
140+
141+
return nil
119142
}

0 commit comments

Comments
 (0)