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

B #5196: fix mysql reconnect (#5198) (#1573)

(cherry picked from commit 3b12e4ae2dc1adb774d587c5272d4eba0f8ea5e4)
(cherry picked from commit 09716b971f068e40e150a727769d5aa0c9269c29)

Co-authored-by: Anton Todorov <atodorov-storpool@users.noreply.github.com>
This commit is contained in:
Pavel Czerný 2021-11-23 16:33:31 +01:00 committed by GitHub
parent b72aca9789
commit 1f578e1ad6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -196,7 +196,7 @@ MySqlDB::MySqlDB(const string& s, int p, const string& u, const string& _p,
}
// -------------------------------------------------------------------------
// Create connection pool to the server
// Create connection pool to the server and database
// -------------------------------------------------------------------------
for (int i=0 ; i < max_connections ; i++)
{
@ -205,8 +205,11 @@ MySqlDB::MySqlDB(const string& s, int p, const string& u, const string& _p,
bool reconnect = true;
mysql_options(connections[i], MYSQL_OPT_RECONNECT, &reconnect);
mysql_options(connections[i], MYSQL_SET_CHARSET_NAME, encoding.c_str());
rc = mysql_real_connect(connections[i], server.c_str(), user.c_str(),
password.c_str(), 0, port, NULL, 0);
password.c_str(), database.c_str(), port, NULL,
CLIENT_REMEMBER_OPTIONS);
if ( rc == nullptr)
{
@ -215,6 +218,8 @@ MySqlDB::MySqlDB(const string& s, int p, const string& u, const string& _p,
throw runtime_error(error);
}
db_connect.push(connections[i]);
}
// -------------------------------------------------------------------------
@ -241,32 +246,6 @@ MySqlDB::MySqlDB(const string& s, int p, const string& u, const string& _p,
throw runtime_error(error);
}
// -------------------------------------------------------------------------
// Connect to OpenNebula Database
// -------------------------------------------------------------------------
string use_sql = "USE " + database;
for (int i=0 ; i < max_connections ; i++)
{
if ( mysql_query(connections[i], use_sql.c_str()) != 0 )
{
string error = "Could not connect to database: ";
error.append(mysql_error(connections[i]));
throw runtime_error(error);
}
if ( mysql_set_character_set(connections[i], encoding.c_str()) != 0 )
{
string error = "Could not set encoding : ";
error.append(mysql_error(connections[i]));
throw runtime_error(error);
}
db_connect.push(connections[i]);
}
oss << "Set up " << max_connections << " DB connections using encoding " <<
encoding;