Core/DBLayer: Pass connection info down to lower level class MySQLConnection and use relevant data in logging messages in its methods

--HG--
branch : trunk
This commit is contained in:
Machiavelli
2010-09-29 15:29:57 +02:00
parent d3b30e979a
commit fcec787e9c
9 changed files with 58 additions and 49 deletions

View File

@@ -69,22 +69,22 @@ class DatabaseWorkerPool
~DatabaseWorkerPool()
{
sLog.outSQLDriver("~DatabaseWorkerPool for '%s'.", m_connectionDetails.database.c_str());
sLog.outSQLDriver("~DatabaseWorkerPool for '%s'.", m_connectionInfo.database.c_str());
mysql_library_end();
}
bool Open(const std::string& infoString, uint8 async_threads, uint8 synch_threads)
{
m_connectionDetails = MySQLConnectionInfo(infoString);
m_connectionInfo = MySQLConnectionInfo(infoString);
sLog.outSQLDriver("Opening databasepool '%s'. Async threads: %u, synch threads: %u", m_connectionDetails.database.c_str(), async_threads, synch_threads);
sLog.outSQLDriver("Opening databasepool '%s'. Async threads: %u, synch threads: %u", m_connectionInfo.database.c_str(), async_threads, synch_threads);
/// Open asynchronous connections (delayed operations)
m_connections[IDX_ASYNC].resize(async_threads);
for (uint8 i = 0; i < async_threads; ++i)
{
T* t = new T(m_queue);
t->Open(m_connectionDetails);
T* t = new T(m_queue, m_connectionInfo);
t->Open();
m_connections[IDX_ASYNC][i] = t;
++m_connectionCount;
}
@@ -93,8 +93,8 @@ class DatabaseWorkerPool
m_connections[IDX_SYNCH].resize(synch_threads);
for (uint8 i = 0; i < synch_threads; ++i)
{
T* t = new T();
t->Open(m_connectionDetails);
T* t = new T(m_connectionInfo);
t->Open();
m_connections[IDX_SYNCH][i] = t;
++m_connectionCount;
}
@@ -105,7 +105,7 @@ class DatabaseWorkerPool
void Close()
{
sLog.outSQLDriver("Closing down databasepool '%s'.", m_connectionDetails.database.c_str());
sLog.outSQLDriver("Closing down databasepool '%s'.", m_connectionInfo.database.c_str());
/// Shuts down delaythreads for this connection pool.
m_queue->queue()->deactivate();
@@ -118,7 +118,7 @@ class DatabaseWorkerPool
--m_connectionCount;
}
sLog.outSQLDriver("Asynchronous connections on databasepool '%s' terminated. Proceeding with synchronous connections.", m_connectionDetails.database.c_str());
sLog.outSQLDriver("Asynchronous connections on databasepool '%s' terminated. Proceeding with synchronous connections.", m_connectionInfo.database.c_str());
/// Shut down the synchronous connections
for (uint8 i = 0; i < m_connections[IDX_SYNCH].size(); ++i)
@@ -130,7 +130,7 @@ class DatabaseWorkerPool
--m_connectionCount;
}
sLog.outSQLDriver("All connections on databasepool %s closed.", m_connectionDetails.database.c_str());
sLog.outSQLDriver("All connections on databasepool %s closed.", m_connectionInfo.database.c_str());
}
void Execute(const char* sql)
@@ -360,7 +360,7 @@ class DatabaseWorkerPool
ACE_Activation_Queue* m_queue; //! Queue shared by async worker threads.
std::vector< std::vector<T*> > m_connections;
AtomicUInt m_connectionCount; //! Counter of MySQL connections;
MySQLConnectionInfo m_connectionDetails;
MySQLConnectionInfo m_connectionInfo;
};
#endif

View File

@@ -18,9 +18,9 @@
#include "CharacterDatabase.h"
bool CharacterDatabaseConnection::Open(const MySQLConnectionInfo& connInfo)
bool CharacterDatabaseConnection::Open()
{
if (!MySQLConnection::Open(connInfo))
if (!MySQLConnection::Open())
return false;
m_stmts.resize(MAX_CHARACTERDATABASE_STATEMENTS);

View File

@@ -26,11 +26,11 @@ class CharacterDatabaseConnection : public MySQLConnection
{
public:
//- Constructors for sync and async connections
CharacterDatabaseConnection() : MySQLConnection() {}
CharacterDatabaseConnection(ACE_Activation_Queue* q) : MySQLConnection(q) {}
CharacterDatabaseConnection(const MySQLConnectionInfo& connInfo) : MySQLConnection(connInfo) {}
CharacterDatabaseConnection(ACE_Activation_Queue* q, const MySQLConnectionInfo& connInfo) : MySQLConnection(q, connInfo) {}
//- Loads databasetype specific prepared statements
bool Open(const MySQLConnectionInfo& connInfo);
bool Open();
};
typedef DatabaseWorkerPool<CharacterDatabaseConnection> CharacterDatabaseWorkerPool;

View File

@@ -18,9 +18,9 @@
#include "LoginDatabase.h"
bool LoginDatabaseConnection::Open(const MySQLConnectionInfo& connInfo)
bool LoginDatabaseConnection::Open()
{
if (!MySQLConnection::Open(connInfo))
if (!MySQLConnection::Open())
return false;
m_stmts.resize(MAX_LOGINDATABASE_STATEMENTS);

View File

@@ -26,11 +26,11 @@ class LoginDatabaseConnection : public MySQLConnection
{
public:
//- Constructors for sync and async connections
LoginDatabaseConnection() : MySQLConnection() {}
LoginDatabaseConnection(ACE_Activation_Queue* q) : MySQLConnection(q) {}
LoginDatabaseConnection(const MySQLConnectionInfo& connInfo) : MySQLConnection(connInfo) {}
LoginDatabaseConnection(ACE_Activation_Queue* q, const MySQLConnectionInfo& connInfo) : MySQLConnection(q, connInfo) {}
//- Loads databasetype specific prepared statements
bool Open(const MySQLConnectionInfo& connInfo);
bool Open();
};
typedef DatabaseWorkerPool<LoginDatabaseConnection> LoginDatabaseWorkerPool;

View File

@@ -18,9 +18,9 @@
#include "WorldDatabase.h"
bool WorldDatabaseConnection::Open(const MySQLConnectionInfo& connInfo)
bool WorldDatabaseConnection::Open()
{
if (!MySQLConnection::Open(connInfo))
if (!MySQLConnection::Open())
return false;
m_stmts.resize(MAX_WORLDDATABASE_STATEMENTS);

View File

@@ -26,11 +26,11 @@ class WorldDatabaseConnection : public MySQLConnection
{
public:
//- Constructors for sync and async connections
WorldDatabaseConnection() : MySQLConnection() {}
WorldDatabaseConnection(ACE_Activation_Queue* q) : MySQLConnection(q) {}
WorldDatabaseConnection(const MySQLConnectionInfo& connInfo) : MySQLConnection(connInfo) {}
WorldDatabaseConnection(ACE_Activation_Queue* q, const MySQLConnectionInfo& connInfo) : MySQLConnection(q, connInfo) {}
//- Loads databasetype specific prepared statements
bool Open(const MySQLConnectionInfo& connInfo);
bool Open();
};
typedef DatabaseWorkerPool<WorldDatabaseConnection> WorldDatabaseWorkerPool;

View File

@@ -32,14 +32,16 @@
#include "DatabaseWorker.h"
#include "Timer.h"
MySQLConnection::MySQLConnection() :
MySQLConnection::MySQLConnection(const MySQLConnectionInfo& connInfo) :
m_connectionInfo(connInfo),
m_queue(NULL),
m_worker(NULL),
m_Mysql(NULL)
{
}
MySQLConnection::MySQLConnection(ACE_Activation_Queue* queue) :
MySQLConnection::MySQLConnection(ACE_Activation_Queue* queue, const MySQLConnectionInfo& connInfo) :
m_connectionInfo(connInfo),
m_queue(queue),
m_Mysql(NULL)
{
@@ -64,13 +66,13 @@ void MySQLConnection::Close()
delete this;
}
bool MySQLConnection::Open(const MySQLConnectionInfo& connInfo)
bool MySQLConnection::Open()
{
MYSQL *mysqlInit;
mysqlInit = mysql_init(NULL);
if (!mysqlInit)
{
sLog.outError("Could not initialize Mysql connection");
sLog.outError("Could not initialize Mysql connection to database `%s`", m_connectionInfo.database.c_str());
return false;
}
@@ -79,7 +81,7 @@ bool MySQLConnection::Open(const MySQLConnectionInfo& connInfo)
mysql_options(mysqlInit, MYSQL_SET_CHARSET_NAME, "utf8");
#ifdef _WIN32
if (connInfo.host == ".") // named pipe use option (Windows)
if (m_connectionInfo.host == ".") // named pipe use option (Windows)
{
unsigned int opt = MYSQL_PROTOCOL_PIPE;
mysql_options(mysqlInit, MYSQL_OPT_PROTOCOL, (char const*)&opt);
@@ -88,27 +90,27 @@ bool MySQLConnection::Open(const MySQLConnectionInfo& connInfo)
}
else // generic case
{
port = atoi(connInfo.port_or_socket.c_str());
port = atoi(m_connectionInfo.port_or_socket.c_str());
unix_socket = 0;
}
#else
if (connInfo.host == ".") // socket use option (Unix/Linux)
if (m_connectionInfo.host == ".") // socket use option (Unix/Linux)
{
unsigned int opt = MYSQL_PROTOCOL_SOCKET;
mysql_options(mysqlInit, MYSQL_OPT_PROTOCOL, (char const*)&opt);
connInfo.ChangeHost("localhost");
m_connectionInfo.ChangeHost("localhost");
port = 0;
unix_socket = connInfo.port_or_socket.c_str();
unix_socket = m_connectionInfo.port_or_socket.c_str();
}
else // generic case
{
port = atoi(connInfo.port_or_socket.c_str());
port = atoi(m_connectionInfo.port_or_socket.c_str());
unix_socket = 0;
}
#endif
m_Mysql = mysql_real_connect(mysqlInit, connInfo.host.c_str(), connInfo.user.c_str(),
connInfo.password.c_str(), connInfo.database.c_str(), port, unix_socket, 0);
m_Mysql = mysql_real_connect(mysqlInit, m_connectionInfo.host.c_str(), m_connectionInfo.user.c_str(),
m_connectionInfo.password.c_str(), m_connectionInfo.database.c_str(), port, unix_socket, 0);
if (m_Mysql)
{
@@ -117,7 +119,7 @@ bool MySQLConnection::Open(const MySQLConnectionInfo& connInfo)
if (mysql_get_server_version(m_Mysql) != mysql_get_client_version())
sLog.outSQLDriver("[WARNING] MySQL client/server version mismatch; may conflict with behaviour of prepared statements.");
sLog.outDetail("Connected to MySQL database at %s", connInfo.host.c_str());
sLog.outDetail("Connected to MySQL database at %s", m_connectionInfo.host.c_str());
if (!mysql_autocommit(m_Mysql, 1))
sLog.outSQLDriver("AUTOCOMMIT SUCCESSFULLY SET TO 1");
else
@@ -141,7 +143,7 @@ bool MySQLConnection::Open(const MySQLConnectionInfo& connInfo)
}
else
{
sLog.outError("Could not connect to MySQL database at %s: %s\n", connInfo.host.c_str(), mysql_error(mysqlInit));
sLog.outError("Could not connect to MySQL database at %s: %s\n", m_connectionInfo.host.c_str(), mysql_error(mysqlInit));
mysql_close(mysqlInit);
return false;
}
@@ -195,20 +197,23 @@ bool MySQLConnection::Execute(PreparedStatement* stmt)
#endif
if (mysql_stmt_bind_param(msql_STMT, msql_BIND))
{
sLog.outSQLDriver("[ERROR]: PreparedStatement (id: %u) error binding params: %s", index, mysql_stmt_error(msql_STMT));
sLog.outSQLDriver("[ERROR]: PreparedStatement (id: %u, database: `%s`) error binding params: %s",
index, m_connectionInfo.database.c_str(), mysql_stmt_error(msql_STMT));
m_mStmt->ClearParameters();
return false;
}
if (mysql_stmt_execute(msql_STMT))
{
sLog.outSQLDriver("[ERROR]: PreparedStatement (id: %u) error executing: %s", index, mysql_stmt_error(msql_STMT));
sLog.outSQLDriver("[ERROR]: PreparedStatement (id: %u, database: `%s`) error executing: %s",
index, m_connectionInfo.database.c_str(), mysql_stmt_error(msql_STMT));
m_mStmt->ClearParameters();
return false;
}
#ifdef SQLQUERY_LOG
sLog.outSQLDriver("[%u ms] Prepared SQL: %u", getMSTimeDiff(_s, getMSTime()), index);
sLog.outSQLDriver("[%u ms] Prepared SQL: %u on database `%s`",
getMSTimeDiff(_s, getMSTime()), index, m_connectionInfo.database.c_str());
#endif
m_mStmt->ClearParameters();
return true;
@@ -237,20 +242,23 @@ bool MySQLConnection::_Query(PreparedStatement* stmt, MYSQL_RES **pResult, uint6
#endif
if (mysql_stmt_bind_param(msql_STMT, msql_BIND))
{
sLog.outSQLDriver("[ERROR]: PreparedStatement (id: %u) error binding params: %s", index, mysql_stmt_error(msql_STMT));
sLog.outSQLDriver("[ERROR]: PreparedStatement (id: %u, database: `%s`) error binding params: %s",
index, m_connectionInfo.database.c_str(), mysql_stmt_error(msql_STMT));
m_mStmt->ClearParameters();
return false;
}
if (mysql_stmt_execute(msql_STMT))
{
sLog.outSQLDriver("[ERROR]: PreparedStatement (id: %u) error executing: %s", index, mysql_stmt_error(msql_STMT));
sLog.outSQLDriver("[ERROR]: PreparedStatement (id: %u, database: `%s`) error executing: %s",
index, m_connectionInfo.database.c_str(), mysql_stmt_error(msql_STMT));
m_mStmt->ClearParameters();
return false;
}
#ifdef SQLQUERY_LOG
sLog.outSQLDriver("[%u ms] Prepared SQL: %u", getMSTimeDiff(_s, getMSTime()), index);
sLog.outSQLDriver("[%u ms] Prepared SQL: %u on database `%s`",
getMSTimeDiff(_s, getMSTime()), index, m_connectionInfo.database.c_str());
#endif
m_mStmt->ClearParameters();

View File

@@ -66,11 +66,11 @@ class MySQLConnection
friend class PingOperation;
public:
MySQLConnection(); //! Constructor for synchroneous connections.
MySQLConnection(ACE_Activation_Queue* queue); //! Constructor for asynchroneous connections.
MySQLConnection(const MySQLConnectionInfo& connInfo); //! Constructor for synchroneous connections.
MySQLConnection(ACE_Activation_Queue* queue, const MySQLConnectionInfo& connInfo); //! Constructor for asynchroneous connections.
~MySQLConnection();
virtual bool Open(const MySQLConnectionInfo& connInfo); //! Connection details.
virtual bool Open();
void Close();
public:
@@ -111,6 +111,7 @@ class MySQLConnection
ACE_Activation_Queue* m_queue; //! Queue shared with other asynchroneous connections.
DatabaseWorker* m_worker; //! Core worker task.
MYSQL * m_Mysql; //! MySQL Handle.
const MySQLConnectionInfo& m_connectionInfo; //! Connection info (used for logging)
ACE_Thread_Mutex m_Mutex;
};