Skip to content

Commit

Permalink
pg: always release conn on commit/rollback error
Browse files Browse the repository at this point in the history
  • Loading branch information
jchappelow committed May 16, 2024
1 parent 30256c8 commit 5824d29
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions internal/sql/pg/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 5824d29

Please sign in to comment.