From 76e7d56cf476f1ad529caafb4b3e87f3cb7a7c17 Mon Sep 17 00:00:00 2001 From: Owen Smallwood Date: Fri, 12 Dec 2025 12:01:48 -0600 Subject: [PATCH] have to manually close connection on commit/rollback --- pkg/storage/unified/sql/db/dbimpl/db.go | 34 ++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/pkg/storage/unified/sql/db/dbimpl/db.go b/pkg/storage/unified/sql/db/dbimpl/db.go index 859eb38da3a..fc15d027150 100644 --- a/pkg/storage/unified/sql/db/dbimpl/db.go +++ b/pkg/storage/unified/sql/db/dbimpl/db.go @@ -79,20 +79,46 @@ func (d sqlDB) BeginTx(ctx context.Context, opts *sql.TxOptions) (db.Tx, error) // once we have a connection, begin the transaction tx, err = conn.BeginTx(ctx, opts) - if err == nil { - return sqlTx{tx}, nil + if err != nil { + if closeErr := conn.Close(); closeErr != nil { + d.log.Error("Failed to close connection after BeginTx error", "error", closeErr) + } + return nil, err } - return nil, err + return sqlTx{Tx: tx, conn: conn}, nil } +// sqlTx wraps sql.Tx to add connection management +// since we are manually acquiring the connection in BeginTx() we need to also manually close it after Commit/Rollback type sqlTx struct { *sql.Tx + conn *sql.Conn } // NewTx wraps an existing *sql.Tx with sqlTx func NewTx(tx *sql.Tx) db.Tx { - return sqlTx{tx} + return sqlTx{Tx: tx} +} + +func (tx sqlTx) Commit() error { + err := tx.Tx.Commit() + if tx.conn != nil { + if err = tx.conn.Close(); err != nil { + return err + } + } + return err +} + +func (tx sqlTx) Rollback() error { + err := tx.Tx.Rollback() + if tx.conn != nil { + if err = tx.conn.Close(); err != nil { + return err + } + } + return err } func (tx sqlTx) QueryContext(ctx context.Context, query string, args ...any) (db.Rows, error) {