Skip to content

Commit 006f25a

Browse files
committed
* Skipped explicit Rollback of transaction on errors (server-side automatically rolled back transactions on errors)
1 parent 51e23f0 commit 006f25a

File tree

4 files changed

+22
-27
lines changed

4 files changed

+22
-27
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
* Fixed race of stop internal processes on close topic writer
22
* Fixed goroutines leak within topic reader on network problems
3+
* Skipped explicit `Rollback` of transaction on errors (server-side automatically rolled back transactions on errors)
34

45
## v3.67.0
56
* Added `ydb.WithNodeAddressMutator` experimental option for mutate node addresses from `discovery.ListEndpoints` response

internal/table/client.go

-14
Original file line numberDiff line numberDiff line change
@@ -704,20 +704,6 @@ func (c *Client) DoTx(ctx context.Context, op table.TxOperation, opts ...table.O
704704
return xerrors.WithStackTrace(err)
705705
}
706706

707-
defer func() {
708-
if err != nil {
709-
errRollback := tx.Rollback(ctx)
710-
if errRollback != nil {
711-
err = xerrors.NewWithIssues("",
712-
xerrors.WithStackTrace(err),
713-
xerrors.WithStackTrace(errRollback),
714-
)
715-
} else {
716-
err = xerrors.WithStackTrace(err)
717-
}
718-
}
719-
}()
720-
721707
err = func() error {
722708
if panicCallback := c.config.PanicCallback(); panicCallback != nil {
723709
defer func() {

internal/table/transaction.go

+18
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
var (
2323
errTxAlreadyCommitted = xerrors.Wrap(fmt.Errorf("transaction already committed"))
2424
errTxRollbackedEarly = xerrors.Wrap(fmt.Errorf("transaction rollbacked early"))
25+
errTxFailedEarly = xerrors.Wrap(fmt.Errorf("transaction failed early"))
2526
)
2627

2728
type txState struct {
@@ -42,6 +43,7 @@ const (
4243
txStateInitialized txStateEnum = iota
4344
txStateCommitted
4445
txStateRollbacked
46+
txStateFailed
4547
)
4648

4749
type transaction struct {
@@ -73,11 +75,15 @@ func (tx *transaction) Execute(
7375
switch tx.state.Load() {
7476
case txStateCommitted:
7577
return nil, xerrors.WithStackTrace(errTxAlreadyCommitted)
78+
case txStateFailed:
79+
return nil, xerrors.WithStackTrace(errTxFailedEarly)
7680
case txStateRollbacked:
7781
return nil, xerrors.WithStackTrace(errTxRollbackedEarly)
7882
default:
7983
_, r, err = tx.s.Execute(ctx, tx.control, query, parameters, opts...)
8084
if err != nil {
85+
tx.state.Store(txStateFailed)
86+
8187
return nil, xerrors.WithStackTrace(err)
8288
}
8389

@@ -115,11 +121,15 @@ func (tx *transaction) ExecuteStatement(
115121
switch tx.state.Load() {
116122
case txStateCommitted:
117123
return nil, xerrors.WithStackTrace(errTxAlreadyCommitted)
124+
case txStateFailed:
125+
return nil, xerrors.WithStackTrace(errTxFailedEarly)
118126
case txStateRollbacked:
119127
return nil, xerrors.WithStackTrace(errTxRollbackedEarly)
120128
default:
121129
_, r, err = stmt.Execute(ctx, tx.control, parameters, opts...)
122130
if err != nil {
131+
tx.state.Store(txStateFailed)
132+
123133
return nil, xerrors.WithStackTrace(err)
124134
}
125135

@@ -148,6 +158,8 @@ func (tx *transaction) CommitTx(
148158
switch tx.state.Load() {
149159
case txStateCommitted:
150160
return nil, xerrors.WithStackTrace(errTxAlreadyCommitted)
161+
case txStateFailed:
162+
return nil, xerrors.WithStackTrace(errTxFailedEarly)
151163
case txStateRollbacked:
152164
return nil, xerrors.WithStackTrace(errTxRollbackedEarly)
153165
default:
@@ -174,6 +186,8 @@ func (tx *transaction) CommitTx(
174186

175187
response, err = tx.s.tableService.CommitTransaction(ctx, request)
176188
if err != nil {
189+
tx.state.Store(txStateFailed)
190+
177191
return nil, xerrors.WithStackTrace(err)
178192
}
179193

@@ -206,6 +220,8 @@ func (tx *transaction) Rollback(ctx context.Context) (err error) {
206220
switch tx.state.Load() {
207221
case txStateCommitted:
208222
return nil // nop for committed tx
223+
case txStateFailed:
224+
return xerrors.WithStackTrace(errTxFailedEarly)
209225
case txStateRollbacked:
210226
return xerrors.WithStackTrace(errTxRollbackedEarly)
211227
default:
@@ -222,6 +238,8 @@ func (tx *transaction) Rollback(ctx context.Context) (err error) {
222238
},
223239
)
224240
if err != nil {
241+
tx.state.Store(txStateFailed)
242+
225243
return xerrors.WithStackTrace(err)
226244
}
227245

retry/sql.go

+3-13
Original file line numberDiff line numberDiff line change
@@ -161,26 +161,16 @@ func DoTx(ctx context.Context, db *sql.DB, op func(context.Context, *sql.Tx) err
161161
}
162162
err := Retry(ctx, func(ctx context.Context) (finalErr error) {
163163
attempts++
164+
164165
tx, err := db.BeginTx(ctx, options.txOptions)
165166
if err != nil {
166167
return unwrapErrBadConn(xerrors.WithStackTrace(err))
167168
}
168-
defer func() {
169-
if finalErr == nil {
170-
return
171-
}
172-
errRollback := tx.Rollback()
173-
if errRollback == nil {
174-
return
175-
}
176-
finalErr = xerrors.NewWithIssues("",
177-
xerrors.WithStackTrace(finalErr),
178-
xerrors.WithStackTrace(fmt.Errorf("rollback failed: %w", errRollback)),
179-
)
180-
}()
169+
181170
if err = op(xcontext.MarkRetryCall(ctx), tx); err != nil {
182171
return unwrapErrBadConn(xerrors.WithStackTrace(err))
183172
}
173+
184174
if err = tx.Commit(); err != nil {
185175
return unwrapErrBadConn(xerrors.WithStackTrace(err))
186176
}

0 commit comments

Comments
 (0)