Skip to content

Commit

Permalink
fix begin tx
Browse files Browse the repository at this point in the history
  • Loading branch information
asmyasnikov committed Nov 5, 2024
1 parent 2793c90 commit 6538a32
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
18 changes: 11 additions & 7 deletions internal/table/conn/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,14 @@ func (c *Conn) ID() string {
return c.session.ID()
}

func (c *Conn) beginTx(ctx context.Context, txOptions driver.TxOptions) (currentTx, error) {
func (c *Conn) beginTx(ctx context.Context, txOptions driver.TxOptions) (tx currentTx, finalErr error) {
onDone := trace.DatabaseSQLOnConnBegin(c.parent.Trace(), &ctx,
stack.FunctionID("github.com/ydb-platform/ydb-go-sdk/v3/internal/table/conn.(*Conn).beginTx"),
)
defer func() {
onDone(tx, finalErr)
}()

if c.currentTx != nil {
return nil, badconn.Map(
xerrors.WithStackTrace(xerrors.AlreadyHasTx(c.currentTx.ID())),
Expand All @@ -470,13 +477,10 @@ func (c *Conn) beginTx(ctx context.Context, txOptions driver.TxOptions) (current
}

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

c.currentTx = tx

Expand Down
1 change: 1 addition & 0 deletions internal/xerrors/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ func AlreadyHasTx(txID string) error {

func IsAlreadyHasTx(err error) bool {
var txErr *alreadyHasTxError

return As(err, &txErr)
}
1 change: 1 addition & 0 deletions tests/integration/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func TestNoEffectsIfForgetCommitTx(t *testing.T) {

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

0 comments on commit 6538a32

Please sign in to comment.