Skip site navigation (1) Skip section navigation (2)

Re: [2] libpq - check PGconn* - is it valid

From: Dmitriy Chumack <saint(at)apriorit(dot)com>
To: pgsql-interfaces(at)postgresql(dot)org
Subject: Re: [2] libpq - check PGconn* - is it valid
Date: 2006-10-12 15:26:00
Message-ID: 14310614201.20061012182600@apriorit.com (view raw or flat)
Thread:
Lists: pgsql-interfaces
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(at)apriorit(dot)com
Software Developer of ApriorIT



pgsql-interfaces by date

Next:From: Sandeep KhandelwalDate: 2006-10-16 07:07:53
Subject: Bulk Load and Extract from PostgreSQL
Previous:From: Jeroen T. VermeulenDate: 2006-10-12 05:19:16
Subject: Re: libpq - check PGconn* - is it valid

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group