Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log and return error if dbConn.Close() fails #60

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions coredb/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"database/sql"
"errors"
"fmt"
"log"
)

// BeginTx returns a custom db.Tx based on opts. This method exists for flexibility.
Expand Down Expand Up @@ -205,17 +206,22 @@
return t.TxWithOpts(ctx, fn, nil, &DefaultTxOpts)
}

func (t *TxProvider) TxWithLock(ctx context.Context, lock string, durationInSec int, fn func(txContext TxContext) error) error {
// TxWithLock executes a transaction with a lock and a specified duration in seconds.
func (t *TxProvider) TxWithLock(ctx context.Context, lock string, durationInSec int, fn func(txContext TxContext) error) (err error) {
connCtx, cancel := context.WithCancel(context.Background())
dbConn, err := t.conn.Conn(connCtx)
dbConn, errConn := t.conn.Conn(connCtx)
defer func() {
cancel()
if dbConn != nil {
dbConn.Close()
errCloseConn := dbConn.Close()
if errCloseConn != nil {
log.Printf("fail to close db connection: %#v", errCloseConn)
err = errCloseConn
}
}
}()
if err != nil {
return fmt.Errorf("fail to get db connection: %w", err)
if errConn != nil {
return fmt.Errorf("fail to get db connection: %w", errConn)
}

{
Expand All @@ -236,7 +242,7 @@
if err == nil {
err = fmt.Errorf("release_lock failed: %w", errRelease)
} else {
err = errors.Join(err, fmt.Errorf("release_lock failed: %w", errRelease))

Check failure on line 245 in coredb/tx.go

View workflow job for this annotation

GitHub Actions / build

undefined: errors.Join
}
return
}
Expand All @@ -244,7 +250,7 @@
if err == nil {
err = newReleaseLockError(lock, durationInSec)
} else {
err = errors.Join(err, newReleaseLockError(lock, durationInSec))

Check failure on line 253 in coredb/tx.go

View workflow job for this annotation

GitHub Actions / build

undefined: errors.Join
}
}
}()
Expand Down
Loading