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

Bug #408: reconnect to MySQL server when connection fails

This commit is contained in:
Carlos Martín 2011-05-06 17:06:57 +02:00
parent b218c52a17
commit 27977b913d
3 changed files with 52 additions and 22 deletions

View File

@ -43,11 +43,11 @@ class MySqlDB : public SqlDB
{
public:
MySqlDB(const string& server,
int port,
const string& user,
const string& password,
const char * database);
MySqlDB(const string& _server,
int _port,
const string& _user,
const string& _password,
const string& _database);
~MySqlDB();
@ -81,6 +81,19 @@ private:
*/
MYSQL * db;
/**
* MySQL Connection parameters
*/
string server;
int port;
string user;
string password;
string database;
/**
* Fine-grain mutex for DB access
*/

View File

@ -208,7 +208,7 @@ void Nebula::start()
{
ostringstream oss;
db = new MySqlDB(server,port,user,passwd,0);
db = new MySqlDB(server,port,user,passwd,db_name);
oss << "CREATE DATABASE IF NOT EXISTS " << db_name;
rc = db->exec(oss);
@ -217,14 +217,6 @@ void Nebula::start()
{
throw runtime_error("Could not create database.");
}
oss.str("");
oss << "USE " << db_name;
rc = db->exec(oss);
if ( rc != 0 )
{
throw runtime_error("Could not open database.");
}
}
NebulaLog::log("ONE",Log::INFO,"Bootstraping OpenNebula database.");

View File

@ -15,6 +15,7 @@
/* -------------------------------------------------------------------------- */
#include "MySqlDB.h"
#include <mysql/errmsg.h>
/*********
* Doc: http://dev.mysql.com/doc/refman/5.5/en/c-api-function-overview.html
@ -23,12 +24,17 @@
/* -------------------------------------------------------------------------- */
MySqlDB::MySqlDB(
const string& server,
int port,
const string& user,
const string& password,
const char * database)
const string& _server,
int _port,
const string& _user,
const string& _password,
const string& _database)
{
server = _server;
port = _port;
user = _user;
password = _password;
database = _database;
// Initialize the MySQL library
mysql_library_init(0, NULL, NULL);
@ -38,7 +44,7 @@ MySqlDB::MySqlDB(
// Connect to the server
if (!mysql_real_connect(db, server.c_str(), user.c_str(),
password.c_str(), database, port, NULL, 0))
password.c_str(), database.c_str(), port, NULL, 0))
{
throw runtime_error("Could not open database.");
}
@ -81,8 +87,27 @@ int MySqlDB::exec(ostringstream& cmd, Callbackable* obj)
const char * err_msg = mysql_error(db);
int err_num = mysql_errno(db);
oss << "SQL command was: " << c_str;
oss << ", error " << err_num << " : " << err_msg;
if( err_num == CR_SERVER_GONE_ERROR || err_num == CR_SERVER_LOST )
{
oss << "MySQL connection error " << err_num << " : " << err_msg;
// Try to re-connect
if (mysql_real_connect(db, server.c_str(), user.c_str(),
password.c_str(), database.c_str(),
port, NULL, 0))
{
oss << "... Reconnected.";
}
else
{
oss << "... Reconnection attempt failed.";
}
}
else
{
oss << "SQL command was: " << c_str;
oss << ", error " << err_num << " : " << err_msg;
}
NebulaLog::log("ONE",Log::ERROR,oss);