From 469294502e8ba99fd36feacd5e704b833b882ba9 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Fri, 29 Apr 2022 05:56:02 +0200 Subject: [PATCH] allow specify how many times we can try to connect to the database --- src/postgresqlconnector.cpp | 54 ++++++++++++++++++++++++++++++++----- src/postgresqlconnector.h | 28 +++++++++++++------ 2 files changed, 67 insertions(+), 15 deletions(-) diff --git a/src/postgresqlconnector.cpp b/src/postgresqlconnector.cpp index 62a7a18..6cc26ee 100644 --- a/src/postgresqlconnector.cpp +++ b/src/postgresqlconnector.cpp @@ -101,7 +101,7 @@ bool PostgreSQLConnector::do_query(const char * query_str, PostgreSQLQueryResult { if( PQstatus(pg_conn) != CONNECTION_OK ) { - assert_connection(); + assert_connection_is_working(); psql_result->psql_result = PQexec(pg_conn, query_str); } } @@ -431,6 +431,8 @@ void PostgreSQLConnector::set_conn_param(const std::wstring & database_name, con } + + void PostgreSQLConnector::overwrite(pt::TextStream & stream) { pt::TextStream::iterator i = stream.begin(); @@ -511,6 +513,17 @@ void PostgreSQLConnector::connect() // // +void PostgreSQLConnector::log_no_connection(size_t attempts) +{ + if( log ) + { + (*log) << pt::Log::log2 << "Morm: connection to the database cannot be established"; + (*log) << pt::Log::log3 << ", (" << attempts << " attempt(s))" << pt::Log::logend; + (*log) << pt::Log::logsave; + } +} + + void PostgreSQLConnector::log_connection_socket() { if( pg_conn && log ) @@ -521,8 +534,18 @@ void PostgreSQLConnector::log_connection_socket() } } -void PostgreSQLConnector::wait_for_connection() + +bool PostgreSQLConnector::wait_for_connection(size_t attempts_max, size_t attempt_delay) { + size_t attempts = 0; + bool attempts_exceeded = false; + + if( attempt_delay == 0 ) + attempt_delay = 1; + + if( attempt_delay > 120 ) + attempt_delay = 120; + if( !pg_conn || PQstatus(pg_conn) != CONNECTION_OK ) { if( log ) @@ -530,24 +553,41 @@ void PostgreSQLConnector::wait_for_connection() (*log) << pt::Log::log3 << "Morm: waiting for the db to be ready...." << pt::Log::logend << pt::Log::logsave; } - while( !assert_connection(false) ) + while( !attempts_exceeded && !assert_connection_is_working(false) ) { - sleep(5); - } + if( attempts_max != 0 ) + { + attempts += 1; + attempts_exceeded = (attempts >= attempts_max); + } + if( !attempts_exceeded ) + { + sleep(attempt_delay); + } + } + } + + if( attempts_exceeded ) + { + log_no_connection(attempts); + } + else + { log_connection_socket(); } + + return !attempts_exceeded; } // IMPROVE ME what about the exception now? -bool PostgreSQLConnector::assert_connection(bool put_log) +bool PostgreSQLConnector::assert_connection_is_working(bool put_log) { bool was_connection = true; - if( !pg_conn ) { was_connection = false; diff --git a/src/postgresqlconnector.h b/src/postgresqlconnector.h index 7755075..d5b99ae 100644 --- a/src/postgresqlconnector.h +++ b/src/postgresqlconnector.h @@ -104,17 +104,23 @@ public: virtual void set_conn_param(const std::wstring & database_host, const std::wstring & database_hostaddr, const std::wstring & database_port, const std::wstring & database, const std::wstring & user, const std::wstring & pass); - virtual void set_conn_param(const std::wstring & database, const std::wstring & user, const std::wstring & pass); - virtual void connect(); - virtual void wait_for_connection(); - virtual void close(); - //virtual bool assert_connection(bool put_log = true, bool throw_if_no_connection = true); - virtual bool assert_connection(bool put_log = true); - virtual void set_db_parameters(); - virtual void log_connection_socket(); + /* + * waiting for a valid connection to the database + * + * attempts_max - how many connection attempts are allowed (0 - infinite) + * attempt_delay - delay between each attempt (in seconds) + * + */ + virtual bool wait_for_connection(size_t attempts_max = 0, size_t attempt_delay = 5); + + + /* + * close the connection with the database if it was open + */ + virtual void close(); @@ -132,6 +138,12 @@ protected: std::wstring db_user; std::wstring db_pass; + virtual void set_db_parameters(); + virtual void log_no_connection(size_t attempts); + virtual void log_connection_socket(); + virtual bool assert_connection_is_working(bool put_log = true); + virtual void connect(); + virtual bool do_query(const char * query_str, PostgreSQLQueryResult * psql_result); virtual void allocate_default_expression(); virtual void overwrite(pt::TextStream & stream);