1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-22 18:50:08 +03:00

B #5094: Fix DB reconnect (#304)

* B #5094: Improve MySQL reconnect

* B #5094: PostgreSQL reconnect
This commit is contained in:
Pavel Czerný 2020-10-08 16:22:10 +02:00 committed by GitHub
parent 9ddd72499c
commit 8f82badc23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions

View File

@ -202,6 +202,9 @@ MySqlDB::MySqlDB(const string& s, int p, const string& u, const string& _p,
{
connections[i] = mysql_init(NULL);
bool reconnect = true;
mysql_options(connections[i], MYSQL_OPT_RECONNECT, &reconnect);
rc = mysql_real_connect(connections[i], server.c_str(), user.c_str(),
password.c_str(), 0, port, NULL, 0);

View File

@ -163,6 +163,29 @@ int PostgreSqlDB::exec_ext(std::ostringstream& cmd, Callbackable *obj, bool quie
PGresult* res = PQexec(conn, c_str);
if ( PQstatus(conn) == CONNECTION_BAD )
{
PQreset(conn);
if ( PQstatus(conn) == CONNECTION_BAD )
{
NebulaLog::error("ONE", "Lost connection to DB, unable to reconnect");
PQclear(res);
free_db_connection(conn);
return SqlDB::CONNECTION;
}
else
{
NebulaLog::info("ONE", "Succesfully reconnected to DB");
// Re-execute the query
PQclear(res);
res = PQexec(conn, c_str);
}
}
if ( PQresultStatus(res) != PGRES_COMMAND_OK &&
PQresultStatus(res) != PGRES_TUPLES_OK )
{