From 5824d29d9f0cba078a5229dd3b881a748a304e00 Mon Sep 17 00:00:00 2001 From: jchappelow <140431406+jchappelow@users.noreply.github.com> Date: Thu, 16 May 2024 10:04:01 -0500 Subject: [PATCH] pg: always release conn on commit/rollback error --- internal/sql/pg/tx.go | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/internal/sql/pg/tx.go b/internal/sql/pg/tx.go index a0c4c4869..f301523c6 100644 --- a/internal/sql/pg/tx.go +++ b/internal/sql/pg/tx.go @@ -84,31 +84,23 @@ func (tx *dbTx) AccessMode() common.AccessMode { // readTx is a tx that handles a read-only transaction. // It will release the connection back to the reader pool -// when it is closed. +// when it is committed or rolled back. type readTx struct { *nestedTx release func() } // Commit is a no-op for read-only transactions. -// It will return the connection to the pool. +// It will unconditionally return the connection to the pool. func (tx *readTx) Commit(ctx context.Context) error { - err := tx.nestedTx.Commit(ctx) - if err != nil { - return err - } + defer tx.release() - tx.release() - return nil + return tx.nestedTx.Commit(ctx) } -// Rollback will return the connection to the pool. +// Rollback will unconditionally return the connection to the pool. func (tx *readTx) Rollback(ctx context.Context) error { - err := tx.nestedTx.Rollback(ctx) - if err != nil { - return err - } + defer tx.release() - tx.release() - return nil + return tx.nestedTx.Rollback(ctx) }