I have a method doing a database connection loop, that calls every 2 seconds this next piece of code until being successful:
167 this->conn = new pqxx::connection(psqlConnectionString);
169 catch(const exception &ex) {
171 sprintf(message, "Cannot connect to the database %s on host %s:%s as %s: %s", dbname.c_str(), hostname.c_str(), port.c_str(), username.c_str(), ex.what());
172 LOG4CPLUS_ERROR(_logger, message);
175 sprintf(message2, "whole param string was:n%s", psqlConnectionString.c_str());
176 LOG4CPLUS_ERROR(_logger, message2);
177 throw invalid_argument(message2);
<code>166 try {
167 this->conn = new pqxx::connection(psqlConnectionString);
168 }
169 catch(const exception &ex) {
170 char message[5000];
171 sprintf(message, "Cannot connect to the database %s on host %s:%s as %s: %s", dbname.c_str(), hostname.c_str(), port.c_str(), username.c_str(), ex.what());
172 LOG4CPLUS_ERROR(_logger, message);
173
174 char message2[5000];
175 sprintf(message2, "whole param string was:n%s", psqlConnectionString.c_str());
176 LOG4CPLUS_ERROR(_logger, message2);
177 throw invalid_argument(message2);
178 }
</code>
166 try {
167 this->conn = new pqxx::connection(psqlConnectionString);
168 }
169 catch(const exception &ex) {
170 char message[5000];
171 sprintf(message, "Cannot connect to the database %s on host %s:%s as %s: %s", dbname.c_str(), hostname.c_str(), port.c_str(), username.c_str(), ex.what());
172 LOG4CPLUS_ERROR(_logger, message);
173
174 char message2[5000];
175 sprintf(message2, "whole param string was:n%s", psqlConnectionString.c_str());
176 LOG4CPLUS_ERROR(_logger, message2);
177 throw invalid_argument(message2);
178 }
How the method succeeds or fails to connect ins’t important.
What is, is that the log4cplus 2.1.1
_logger
makes eventually my program hanging (I don’t think it can be sprintf
). And I cannot enter in debug mode into it easily.
In the following logs, you can see in my program logging that it logs at these statements:
db_adapter.cpp 172, 176, 56, 26
,
db_adapter.cpp 172, 176, 56, 26
,
db_adapter.cpp 172
then it hangs.
<code>a_cpp_template | [INFO ][07/10/24 09:05:46,110][/tmp/cpp-app/src/adapters/app.cpp:31] 2/6 - Starting DB Adapter
a_cpp_template | [ERROR][07/10/24 09:05:46,113][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:172] Cannot connect to the database template_manager on host cdp-database:5432 as cdp_template_manager_owner_user: connection to server at "cdp-database" (172.18.0.2), port 5432 failed: Connection refused
a_cpp_template | Is the server running on that host and accepting TCP/IP connections?
a_cpp_template | [ERROR][07/10/24 09:05:46,113][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:176] whole param string was:
a_cpp_template | user='cdp_template_manager_owner_user' host='cdp-database' port='5432' dbname='template_manager' sslmode='require' password='cdp_template_manager_pwd'
a_cpp_template | [ERROR][07/10/24 09:05:46,113][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:56] Aborting transaction and/or connection in response of previous exception received: whole param string was:
a_cpp_template | user='cdp_template_manager_owner_user' host='cdp-database' port='5432' dbname='template_manager' sslmode='require' password='cdp_template_manager_pwd'
a_cpp_template | [ERROR][07/10/24 09:05:46,113][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:26] Next attempt in 2 seconds, as attempt to connect to database failed! Aborting transaction and/or connection in response of previous exception received: whole param string was:
a_cpp_template | user='cdp_template_manager_owner_user' host='cdp-database' port='5432' dbname='template_manager' sslmode='require' password='cdp_template_manager_pwd'
a_cpp_template | [ERROR][07/10/24 09:05:48,114][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:172] Cannot connect to the database template_manager on host cdp-database:5432 as cdp_template_manager_owner_user: connection to server at "cdp-database" (172.18.0.2), port 5432 failed: Connection refused
a_cpp_template | Is the server running on that host and accepting TCP/IP connections?
a_cpp_template | [ERROR][07/10/24 09:05:48,114][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:176] whole param string was:
a_cpp_template | user='cdp_template_manager_owner_user' host='cdp-database' port='5432' dbname='template_manager' sslmode='require' password='cdp_template_manager_pwd'
a_cpp_template | [ERROR][07/10/24 09:05:48,115][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:56] Aborting transaction and/or connection in response of previous exception received: whole param string was:
a_cpp_template | user='cdp_template_manager_owner_user' host='cdp-database' port='5432' dbname='template_manager' sslmode='require' password='cdp_template_manager_pwd'
a_cpp_template | [ERROR][07/10/24 09:05:48,115][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:26] Next attempt in 2 seconds, as attempt to connect to database failed! Aborting transaction and/or connection in response of previous exception received: whole param string was:
a_cpp_template | user='cdp_template_manager_owner_user' host='cdp-database' port='5432' dbname='template_manager' sslmode='require' password='cdp_template_manager_pwd'
a_cpp_template | [ERROR][07/10/24 09:05:50,117][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:172] Cannot connect to the database template_manager on host cdp-database:5432 as cdp_template_manager_owner_user: connection to server at "cdp-database" (172.18.0.2), port 5432 failed: Connection refused
a_cpp_template | Is the server running on that host and accepting TCP/IP connections?
[...and then a_cpp_template is never heard again...]
<code>a_cpp_template | [INFO ][07/10/24 09:05:46,110][/tmp/cpp-app/src/adapters/app.cpp:31] 2/6 - Starting DB Adapter
a_cpp_template | [ERROR][07/10/24 09:05:46,113][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:172] Cannot connect to the database template_manager on host cdp-database:5432 as cdp_template_manager_owner_user: connection to server at "cdp-database" (172.18.0.2), port 5432 failed: Connection refused
a_cpp_template | Is the server running on that host and accepting TCP/IP connections?
a_cpp_template |
a_cpp_template | [ERROR][07/10/24 09:05:46,113][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:176] whole param string was:
a_cpp_template | user='cdp_template_manager_owner_user' host='cdp-database' port='5432' dbname='template_manager' sslmode='require' password='cdp_template_manager_pwd'
a_cpp_template | [ERROR][07/10/24 09:05:46,113][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:56] Aborting transaction and/or connection in response of previous exception received: whole param string was:
a_cpp_template | user='cdp_template_manager_owner_user' host='cdp-database' port='5432' dbname='template_manager' sslmode='require' password='cdp_template_manager_pwd'
a_cpp_template | [ERROR][07/10/24 09:05:46,113][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:26] Next attempt in 2 seconds, as attempt to connect to database failed! Aborting transaction and/or connection in response of previous exception received: whole param string was:
a_cpp_template | user='cdp_template_manager_owner_user' host='cdp-database' port='5432' dbname='template_manager' sslmode='require' password='cdp_template_manager_pwd'
a_cpp_template | [ERROR][07/10/24 09:05:48,114][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:172] Cannot connect to the database template_manager on host cdp-database:5432 as cdp_template_manager_owner_user: connection to server at "cdp-database" (172.18.0.2), port 5432 failed: Connection refused
a_cpp_template | Is the server running on that host and accepting TCP/IP connections?
a_cpp_template |
a_cpp_template | [ERROR][07/10/24 09:05:48,114][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:176] whole param string was:
a_cpp_template | user='cdp_template_manager_owner_user' host='cdp-database' port='5432' dbname='template_manager' sslmode='require' password='cdp_template_manager_pwd'
a_cpp_template | [ERROR][07/10/24 09:05:48,115][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:56] Aborting transaction and/or connection in response of previous exception received: whole param string was:
a_cpp_template | user='cdp_template_manager_owner_user' host='cdp-database' port='5432' dbname='template_manager' sslmode='require' password='cdp_template_manager_pwd'
a_cpp_template | [ERROR][07/10/24 09:05:48,115][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:26] Next attempt in 2 seconds, as attempt to connect to database failed! Aborting transaction and/or connection in response of previous exception received: whole param string was:
a_cpp_template | user='cdp_template_manager_owner_user' host='cdp-database' port='5432' dbname='template_manager' sslmode='require' password='cdp_template_manager_pwd'
a_cpp_template | [ERROR][07/10/24 09:05:50,117][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:172] Cannot connect to the database template_manager on host cdp-database:5432 as cdp_template_manager_owner_user: connection to server at "cdp-database" (172.18.0.2), port 5432 failed: Connection refused
a_cpp_template | Is the server running on that host and accepting TCP/IP connections?
[...and then a_cpp_template is never heard again...]
</code>
a_cpp_template | [INFO ][07/10/24 09:05:46,110][/tmp/cpp-app/src/adapters/app.cpp:31] 2/6 - Starting DB Adapter
a_cpp_template | [ERROR][07/10/24 09:05:46,113][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:172] Cannot connect to the database template_manager on host cdp-database:5432 as cdp_template_manager_owner_user: connection to server at "cdp-database" (172.18.0.2), port 5432 failed: Connection refused
a_cpp_template | Is the server running on that host and accepting TCP/IP connections?
a_cpp_template |
a_cpp_template | [ERROR][07/10/24 09:05:46,113][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:176] whole param string was:
a_cpp_template | user='cdp_template_manager_owner_user' host='cdp-database' port='5432' dbname='template_manager' sslmode='require' password='cdp_template_manager_pwd'
a_cpp_template | [ERROR][07/10/24 09:05:46,113][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:56] Aborting transaction and/or connection in response of previous exception received: whole param string was:
a_cpp_template | user='cdp_template_manager_owner_user' host='cdp-database' port='5432' dbname='template_manager' sslmode='require' password='cdp_template_manager_pwd'
a_cpp_template | [ERROR][07/10/24 09:05:46,113][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:26] Next attempt in 2 seconds, as attempt to connect to database failed! Aborting transaction and/or connection in response of previous exception received: whole param string was:
a_cpp_template | user='cdp_template_manager_owner_user' host='cdp-database' port='5432' dbname='template_manager' sslmode='require' password='cdp_template_manager_pwd'
a_cpp_template | [ERROR][07/10/24 09:05:48,114][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:172] Cannot connect to the database template_manager on host cdp-database:5432 as cdp_template_manager_owner_user: connection to server at "cdp-database" (172.18.0.2), port 5432 failed: Connection refused
a_cpp_template | Is the server running on that host and accepting TCP/IP connections?
a_cpp_template |
a_cpp_template | [ERROR][07/10/24 09:05:48,114][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:176] whole param string was:
a_cpp_template | user='cdp_template_manager_owner_user' host='cdp-database' port='5432' dbname='template_manager' sslmode='require' password='cdp_template_manager_pwd'
a_cpp_template | [ERROR][07/10/24 09:05:48,115][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:56] Aborting transaction and/or connection in response of previous exception received: whole param string was:
a_cpp_template | user='cdp_template_manager_owner_user' host='cdp-database' port='5432' dbname='template_manager' sslmode='require' password='cdp_template_manager_pwd'
a_cpp_template | [ERROR][07/10/24 09:05:48,115][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:26] Next attempt in 2 seconds, as attempt to connect to database failed! Aborting transaction and/or connection in response of previous exception received: whole param string was:
a_cpp_template | user='cdp_template_manager_owner_user' host='cdp-database' port='5432' dbname='template_manager' sslmode='require' password='cdp_template_manager_pwd'
a_cpp_template | [ERROR][07/10/24 09:05:50,117][/tmp/cpp-app/src/adapters/dbadapter/db_adapter.cpp:172] Cannot connect to the database template_manager on host cdp-database:5432 as cdp_template_manager_owner_user: connection to server at "cdp-database" (172.18.0.2), port 5432 failed: Connection refused
a_cpp_template | Is the server running on that host and accepting TCP/IP connections?
[...and then a_cpp_template is never heard again...]
In the following part of my code:
<code>172 LOG4CPLUS_ERROR(_logger, message);
175 sprintf(message2, "whole param string was:n%s", psqlConnectionString.c_str());
176 LOG4CPLUS_ERROR(_logger, message2);
<code>172 LOG4CPLUS_ERROR(_logger, message);
173
174 char message2[5000];
175 sprintf(message2, "whole param string was:n%s", psqlConnectionString.c_str());
176 LOG4CPLUS_ERROR(_logger, message2);
</code>
172 LOG4CPLUS_ERROR(_logger, message);
173
174 char message2[5000];
175 sprintf(message2, "whole param string was:n%s", psqlConnectionString.c_str());
176 LOG4CPLUS_ERROR(_logger, message2);
what might prevent it going from line 172
(that was executed, I can see it in the logs) to 176
one, it never joined? I wonder.
I’ve investigated around _logger
here, and found that it was declared in another class, as a global variable like that:
<code>log4cplus::Logger _logger;
void log4cplus::initialise()
::log4cplus::initialize();
log4cplus::PropertyConfigurator::doConfigure(LOG4CPLUS_TEXT("./src/utils/custom-logger/log4cpp.properties"));
_logger = Logger::getInstance(LOG4CPLUS_TEXT("main"));
<code>log4cplus::Logger _logger;
void log4cplus::initialise()
{
::log4cplus::initialize();
log4cplus::PropertyConfigurator::doConfigure(LOG4CPLUS_TEXT("./src/utils/custom-logger/log4cpp.properties"));
_logger = Logger::getInstance(LOG4CPLUS_TEXT("main"));
}
</code>
log4cplus::Logger _logger;
void log4cplus::initialise()
{
::log4cplus::initialize();
log4cplus::PropertyConfigurator::doConfigure(LOG4CPLUS_TEXT("./src/utils/custom-logger/log4cpp.properties"));
_logger = Logger::getInstance(LOG4CPLUS_TEXT("main"));
}
Is such initialization trouble prone?
What can cause an dead lock?