From 605db30f6c7a92c021bbeaf4013ffa82731174bd Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 5 Mar 2020 15:03:39 +0100 Subject: [PATCH] Reuse correct connection parameters for PostgreSQL failover If the failover callback doesn't provide the connection string to use, default to the originally used one instead of using empty connection string which is almost certainly not what we want. Note that this requires storing the original connection parameters in postgresql_session_backend, as we don't have access to the ones stored in the session object and it seems wrong to add backlinks from the backend to the session object. Closes #793. --- include/soci/postgresql/soci-postgresql.h | 2 ++ src/backends/postgresql/error.cpp | 7 +++++-- src/backends/postgresql/session.cpp | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/include/soci/postgresql/soci-postgresql.h b/include/soci/postgresql/soci-postgresql.h index 16dcc6fb..fed5dcbb 100644 --- a/include/soci/postgresql/soci-postgresql.h +++ b/include/soci/postgresql/soci-postgresql.h @@ -25,6 +25,7 @@ #endif #include +#include "soci/connection-parameters.h" #include #include @@ -395,6 +396,7 @@ struct postgresql_session_backend : details::session_backend int statementCount_; bool single_row_mode_; PGconn * conn_; + connection_parameters connectionParameters_; }; diff --git a/src/backends/postgresql/error.cpp b/src/backends/postgresql/error.cpp index 3cc46833..712dbda8 100644 --- a/src/backends/postgresql/error.cpp +++ b/src/backends/postgresql/error.cpp @@ -123,8 +123,11 @@ details::postgresql_result::check_for_data(char const* errMsg) const if (retry) { - connection_parameters parameters; - parameters.set_connect_string(newTarget); + connection_parameters parameters = + sessionBackend_.connectionParameters_; + + if (!newTarget.empty()) + parameters.set_connect_string(newTarget); sessionBackend_.clean_up(); diff --git a/src/backends/postgresql/session.cpp b/src/backends/postgresql/session.cpp index d398d552..34263e52 100644 --- a/src/backends/postgresql/session.cpp +++ b/src/backends/postgresql/session.cpp @@ -9,7 +9,6 @@ #include "soci/soci-platform.h" #include "soci/postgresql/soci-postgresql.h" #include "soci/session.h" -#include "soci/connection-parameters.h" #include // libpq #include #include @@ -69,6 +68,7 @@ void postgresql_session_backend::connect( "Cannot set extra_float_digits parameter"); conn_ = conn; + connectionParameters_ = parameters; } postgresql_session_backend::~postgresql_session_backend()