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
returnsCONNECTION_OK !!!
3) when there is no connections in PQsendQuery and PQgetResult, then PQgetResult hangs and it will hang (my
patienceend after 10 minutes) until a connection come back again. It's better then PQexec, but not that I need. I
wantthat 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