When calling the SetPersonStatusByName function, a non-closing session occurs. Question: Did I miss anything to close the session after calling this function?
func (m *DBManager) SetPersonStatusByName(ctx context.Context, status int, name string) error {
tx, err := m.db.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("begin transaction: %w", err)
}
defer func(tx *sql.Tx) {
rErr := tx.Rollback()
if rErr != nil && !errors.Is(rErr, sql.ErrTxDone) {
err = errors.Join(err, rErr)
}
}(tx)
const selectForUpdate = `select status from persons where name=$1 for update;`
if err = m.db.QueryRowContext(ctx, selectForUpdate, name).Err(); err != nil {
return fmt.Errorf("select for update: %w", err)
}
const updateStatus = `update persons set status=$1 where name=$2;`
if _, err = m.db.ExecContext(ctx, updateStatus, status, name); err != nil {
return fmt.Errorf("update row: %w", err)
}
err = tx.Commit()
if err != nil {
return fmt.Errorf("commit transaction: %w", err)
}
return nil
}