Peter Geoghegan <peter.geoghegan86@gmail.com> writes:
> I'm developing a PostgreSQL application for Windows CE 5 on a PDT/ PDA
> in C++/Qt, using Hiroshi Saito's libpq port for that platform. Since
> the connection is established over wi-fi, and wi-fi connectivity is
> often flaky, I feel that I have to "fail gracefully" to as great an
> extent as possible. The following utility function,
> VerifyDbConnection(), is called before every piece of database work:
> void MainInterface::VerifyDbConnection()
> {
> if (PQstatus(conn) != CONNECTION_OK)
> {
> ... try to recover ...
> }
> }
PQstatus does *not* probe to see if there's a live connection to the
database; it just returns the state left behind by the last operation.
So this code will just fall through and not do anything useful until
after you've already had a failure. The forcible PQreset is probably
not the most graceful way of recovering, either.
What I'd try is a "ping" to the database server, and not initiate any
libpq operation unless the server is answering pings. If you get
a failure due to connectivity loss midway through an operation,
PQreset is appropriate then --- but don't do it to recover from
a momentary network outage.
regards, tom lane