Re-connect to postgresql when the connection is lost

fix #3336
This commit is contained in:
louiz’
2018-03-19 00:16:16 +01:00
parent 8f74d4dd40
commit d7427fc9ca
2 changed files with 15 additions and 2 deletions
+3
View File
@@ -8,6 +8,9 @@ Version 8.0
the user customize the address to use when connecting to a server. the user customize the address to use when connecting to a server.
See https://lab.louiz.org/louiz/biboumi/issues/3273 for more details. See https://lab.louiz.org/louiz/biboumi/issues/3273 for more details.
- Messages id are properly reflected to the sender - Messages id are properly reflected to the sender
- We now properly deal with a PostgreSQL server restart: whenever the
connection is lost with the server, we try to reconnect and re-execute the
query once.
Version 7.2 - 2018-01-24 Version 7.2 - 2018-01-24
======================== ========================
+11 -1
View File
@@ -92,7 +92,7 @@ class PostgresqlStatement: public Statement
private: private:
private: private:
bool execute() bool execute(const bool second_attempt=false)
{ {
std::vector<const char*> params; std::vector<const char*> params;
params.reserve(this->params.size()); params.reserve(this->params.size());
@@ -113,8 +113,18 @@ private:
const char* original = PQerrorMessage(this->conn); const char* original = PQerrorMessage(this->conn);
if (original && std::strlen(original) > 0) if (original && std::strlen(original) > 0)
log_error("Failed to execute command: ", std::string{original, std::strlen(original) - 1}); log_error("Failed to execute command: ", std::string{original, std::strlen(original) - 1});
if (PQstatus(this->conn) != CONNECTION_OK && !second_attempt)
{
log_info("Trying to reconnect to PostgreSQL server and execute the query again.");
PQreset(this->conn);
return this->execute(true);
}
else
{
log_error("Givin up.");
return false; return false;
} }
}
return true; return true;
} }