Skip to content

Commit 6538a32

Browse files
committed
fix begin tx
1 parent 2793c90 commit 6538a32

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

internal/table/conn/conn.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,14 @@ func (c *Conn) ID() string {
448448
return c.session.ID()
449449
}
450450

451-
func (c *Conn) beginTx(ctx context.Context, txOptions driver.TxOptions) (currentTx, error) {
451+
func (c *Conn) beginTx(ctx context.Context, txOptions driver.TxOptions) (tx currentTx, finalErr error) {
452+
onDone := trace.DatabaseSQLOnConnBegin(c.parent.Trace(), &ctx,
453+
stack.FunctionID("github.com/ydb-platform/ydb-go-sdk/v3/internal/table/conn.(*Conn).beginTx"),
454+
)
455+
defer func() {
456+
onDone(tx, finalErr)
457+
}()
458+
452459
if c.currentTx != nil {
453460
return nil, badconn.Map(
454461
xerrors.WithStackTrace(xerrors.AlreadyHasTx(c.currentTx.ID())),
@@ -470,13 +477,10 @@ func (c *Conn) beginTx(ctx context.Context, txOptions driver.TxOptions) (current
470477
}
471478

472479
func (c *Conn) BeginTx(ctx context.Context, txOptions driver.TxOptions) (driver.Tx, error) {
473-
onDone := trace.DatabaseSQLOnConnBegin(c.parent.Trace(), &ctx,
474-
stack.FunctionID("github.com/ydb-platform/ydb-go-sdk/v3/internal/table/conn.(*Conn).BeginTx"),
475-
)
476480
tx, err := c.beginTx(ctx, txOptions)
477-
defer func() {
478-
onDone(tx, err)
479-
}()
481+
if err != nil {
482+
return nil, xerrors.WithStackTrace(err)
483+
}
480484

481485
c.currentTx = tx
482486

internal/xerrors/tx.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ func AlreadyHasTx(txID string) error {
2525

2626
func IsAlreadyHasTx(err error) bool {
2727
var txErr *alreadyHasTxError
28+
2829
return As(err, &txErr)
2930
}

tests/integration/tx_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ func TestNoEffectsIfForgetCommitTx(t *testing.T) {
143143

144144
// second tx on existing conn === session
145145
_, err = cc.BeginTx(ctx, &sql.TxOptions{})
146+
require.Error(t, err)
146147
require.True(t, xerrors.IsAlreadyHasTx(err))
147148
})
148149
}

0 commit comments

Comments
 (0)