`PQstatus()` only checks the known state of the connection. It's
useful because if the connection is already known to be bad we don't
need to go further, but it isn't sufficient to detect whether it has
been closed since the last time we used it.
bool SPgSQL::isConnectionUsable()
{
- return PQstatus(d_db) == CONNECTION_OK;
+ if (PQstatus(d_db) != CONNECTION_OK) {
+ return false;
+ }
+
+ bool usable = false;
+ int sd = PQsocket(d_db);
+ bool wasNonBlocking = isNonBlocking(sd);
+
+ if (!wasNonBlocking) {
+ if (!setNonBlocking(sd)) {
+ return usable;
+ }
+ }
+
+ usable = isTCPSocketUsable(sd);
+
+ if (!wasNonBlocking) {
+ if (!setBlocking(sd)) {
+ usable = false;
+ }
+ }
+
+ return usable;
}
void SPgSQL::reconnect()