pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Dmitriy Chumack <saint@apriorit.com>
To: pgsql-interfaces@postgresql.org
Subject: Re: [2] libpq - check PGconn* - is it valid
Date: Thu, 12 Oct 2006 18:26:00 +0300
Message-ID: <14310614201.20061012182600@apriorit.com> (raw)

I try to use async libpq functions to test connection to db.
I have such a piece of code:

bool check_connection(PGconn* connection_)
{
  int rv_ = PQsendQuery(connection_, "select 0");
  PGresult* result_ = PQgetResult(connection_);

  if(result_)
    PQclear(result_);

  PGresult* must_be_null_ = PQgetResult(connection_);
  ConnStatusType status_ = PQstatus(connection_);

  if(status_ == CONNECTION_BAD)
    return false;
  else
    return true;
}

And I have a few problems with this code:

  1) I have to call PQgetResult twice (second call returns null),
     otherwice from time to time PQsendQuery will return error
     "another command is already in progress".

  2) when there is no network connection in call of both PQsendQuery
     and PQgetResult, then PQstatus returns CONNECTION_BAD. But when
     connection is ok in PQsendQuery call and is bad in PQgetResult
     call, then PQstatus returns CONNECTION_OK !!!

  3) when there is no connections in PQsendQuery and PQgetResult, then
     PQgetResult hangs and it will hang (my patience end after 10
     minutes) until a connection come back again. It's better then
     PQexec, but not that I need. I want that PQgetResult returns
     after a timeout.

I use libpq that comes with PostgreSQL 8.1.3 on Fedora Core 5 x86_64.

Does anyone have any suggestions?
     
Thanks in advance.

-- 
Best regards,
 Dmitriy Chumack                         mailto:saint@apriorit.com
Software Developer of ApriorIT





view thread (4+ messages)

Message-ID: <14310614201.20061012182600@apriorit.com>
Permalink:  ../14310614201.20061012182600@apriorit.com/
Also on:    postgresql.org/message-id/14310614201.20061012182600@apriorit.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-interfaces@postgresql.org
  Cc: saint@apriorit.com
  Subject: Re: [2] libpq - check PGconn* - is it valid
  In-Reply-To: <14310614201.20061012182600@apriorit.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox