From d7427fc9ca4c06fda458e4951559f57163d90b94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Mon, 19 Mar 2018 00:16:16 +0100 Subject: Re-connect to postgresql when the connection is lost fix #3336 --- CHANGELOG.rst | 3 +++ src/database/postgresql_statement.hpp | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 8f8d057..ee31f7d 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -8,6 +8,9 @@ Version 8.0 the user customize the address to use when connecting to a server. See https://lab.louiz.org/louiz/biboumi/issues/3273 for more details. - 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 ======================== diff --git a/src/database/postgresql_statement.hpp b/src/database/postgresql_statement.hpp index 5665aed..37e8ea0 100644 --- a/src/database/postgresql_statement.hpp +++ b/src/database/postgresql_statement.hpp @@ -92,7 +92,7 @@ class PostgresqlStatement: public Statement private: private: - bool execute() + bool execute(const bool second_attempt=false) { std::vector params; params.reserve(this->params.size()); @@ -113,7 +113,17 @@ private: const char* original = PQerrorMessage(this->conn); if (original && std::strlen(original) > 0) log_error("Failed to execute command: ", std::string{original, std::strlen(original) - 1}); - return false; + 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 true; } -- cgit v1.2.3