diff --git a/coredb/tx.go b/coredb/tx.go index 460a69b..d4f077b 100644 --- a/coredb/tx.go +++ b/coredb/tx.go @@ -206,7 +206,14 @@ func (t *TxProvider) Tx(ctx context.Context, fn func(TxContext) error) error { } func (t *TxProvider) TxWithLock(ctx context.Context, lock string, durationInSec int, fn func(txContext TxContext) error) error { - dbConn, err := t.conn.Conn(ctx) + connCtx, cancel := context.WithCancel(context.Background()) + dbConn, err := t.conn.Conn(connCtx) + defer func() { + cancel() + if dbConn != nil { + dbConn.Close() + } + }() if err != nil { return fmt.Errorf("fail to get db connection: %w", err) } diff --git a/go.mod b/go.mod index 9f7fa0b..c066d97 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/mitchellh/cli v1.1.4 github.com/pkg/errors v0.9.1 github.com/spf13/viper v1.15.0 + github.com/stretchr/testify v1.8.1 golang.org/x/text v0.7.0 ) @@ -60,7 +61,6 @@ require ( github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/src-d/go-oniguruma v1.1.0 // indirect - github.com/stretchr/testify v1.8.1 // indirect github.com/subosito/gotenv v1.4.2 // indirect golang.org/x/crypto v0.5.0 // indirect golang.org/x/mod v0.7.0 // indirect