Browse Source

Merge branch 'master' into peercoin

peercoin
Intel 11 years ago
parent
commit
e856f5e222
  1. 34
      src/server/shared/MySQL/DatabaseConnection.cpp
  2. 9
      src/server/shared/MySQL/DatabaseConnection.h

34
src/server/shared/MySQL/DatabaseConnection.cpp

@ -26,7 +26,7 @@ namespace MySQL
bool DatabaseConnection::Open() bool DatabaseConnection::Open()
{ {
MYSQL* mysqlInit; MYSQL* mysqlInit = NULL;
mysqlInit = mysql_init(NULL); mysqlInit = mysql_init(NULL);
if (!mysqlInit) if (!mysqlInit)
throw ConnectionException(Util::FS("Could not initialize Mysql connection to database `%s`", _connectionInfo.DB.c_str())); throw ConnectionException(Util::FS("Could not initialize Mysql connection to database `%s`", _connectionInfo.DB.c_str()));
@ -50,8 +50,11 @@ namespace MySQL
else else
{ {
const char* error = mysql_error(mysqlInit); const char* error = mysql_error(mysqlInit);
_mysqlConnectError = mysql_errno(mysqlInit); // Used for DatabaseConnection::_HandleMySQLErrno
mysql_close(mysqlInit); mysql_close(mysqlInit);
throw ConnectionException(Util::FS("Could not connect to MySQL database at %s: %s", _connectionInfo.Host.c_str(), mysql_error(mysqlInit))); mysqlInit = NULL;
throw ConnectionException(Util::FS("Could not connect to MySQL database at %s: %s", _connectionInfo.Host.c_str(), error));
} }
} }
@ -285,8 +288,31 @@ namespace MySQL
return true; return true;
} }
bool DatabaseConnection::_HandleMySQLErrno(uint32_t lErrno) bool DatabaseConnection::_HandleMySQLErrno(uint32 lErrno)
{ {
return false; switch (lErrno)
{
case CR_SERVER_GONE_ERROR:
case CR_SERVER_LOST:
case CR_INVALID_CONN_HANDLE:
case CR_SERVER_LOST_EXTENDED:
{
mysql_close(_mysql);
try {
Open();
return true;
} catch(std::exception& e) {
sLog.Error(LOG_DATABASE, "Failed to connect to mysql database: %s", e.what());
// It's possible this attempted reconnect throws 2006 at us. To prevent crazy recursive calls, sleep here.
boost::this_thread::sleep(boost::posix_time::milliseconds(3000));
return _HandleMySQLErrno(_mysqlConnectError);
}
}
default:
sLog.Error(LOG_DATABASE, "Unhandled MySQL errno %u. Unexpected behaviour possible.", lErrno);
return false;
}
} }
} }

9
src/server/shared/MySQL/DatabaseConnection.h

@ -14,6 +14,14 @@
namespace MySQL namespace MySQL
{ {
enum ClientError
{
CR_SERVER_GONE_ERROR = 2006,
CR_SERVER_LOST = 2013,
CR_INVALID_CONN_HANDLE = 2048,
CR_SERVER_LOST_EXTENDED = 2055
};
enum ConnectionType enum ConnectionType
{ {
MYSQL_CONN_SYNC, MYSQL_CONN_SYNC,
@ -99,6 +107,7 @@ namespace MySQL
boost::mutex _mutex; boost::mutex _mutex;
MYSQL* _mysql; MYSQL* _mysql;
uint32 _mysqlConnectError;
DatabaseWorkQueue* _asyncQueue; DatabaseWorkQueue* _asyncQueue;
DatabaseWorker* _worker; DatabaseWorker* _worker;
std::vector<ConnectionPreparedStatement*> _stmts; std::vector<ConnectionPreparedStatement*> _stmts;

Loading…
Cancel
Save