aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/server/shared/Database/DatabaseWorkerPool.h95
-rw-r--r--src/server/shared/Database/MySQLConnection.cpp8
-rw-r--r--src/server/worldserver/Main.cpp22
3 files changed, 81 insertions, 44 deletions
diff --git a/src/server/shared/Database/DatabaseWorkerPool.h b/src/server/shared/Database/DatabaseWorkerPool.h
index 39f1a8da3c2..e95dfc1e484 100644
--- a/src/server/shared/Database/DatabaseWorkerPool.h
+++ b/src/server/shared/Database/DatabaseWorkerPool.h
@@ -45,6 +45,14 @@ class PingOperation : public SQLOperation
template <class T>
class DatabaseWorkerPool
{
+ private:
+ enum InternalIndex
+ {
+ IDX_ASYNC,
+ IDX_SYNCH,
+ IDX_SIZE
+ };
+
public:
/* Activity state */
DatabaseWorkerPool() : _connectionInfo(NULL)
@@ -74,34 +82,17 @@ class DatabaseWorkerPool
TC_LOG_INFO("sql.driver", "Opening DatabasePool '%s'. Asynchronous connections: %u, synchronous connections: %u.",
GetDatabaseName(), async_threads, synch_threads);
- //! Open asynchronous connections (delayed operations)
- _connections[IDX_ASYNC].resize(async_threads);
- for (uint8 i = 0; i < async_threads; ++i)
- {
- T* t = new T(_queue, *_connectionInfo);
- res &= t->Open();
- if (res) // only check mysql version if connection is valid
- WPFatal(mysql_get_server_version(t->GetHandle()) >= MIN_MYSQL_SERVER_VERSION, "TrinityCore does not support MySQL versions below 5.1");
- _connections[IDX_ASYNC][i] = t;
- ++_connectionCount[IDX_ASYNC];
- }
+ res = OpenConnections(IDX_ASYNC, async_threads);
- //! Open synchronous connections (direct, blocking operations)
- _connections[IDX_SYNCH].resize(synch_threads);
- for (uint8 i = 0; i < synch_threads; ++i)
- {
- T* t = new T(*_connectionInfo);
- res &= t->Open();
- _connections[IDX_SYNCH][i] = t;
- ++_connectionCount[IDX_SYNCH];
- }
+ if (!res)
+ return res;
+
+ res = OpenConnections(IDX_SYNCH, synch_threads);
if (res)
TC_LOG_INFO("sql.driver", "DatabasePool '%s' opened successfully. %u total connections running.", GetDatabaseName(),
(_connectionCount[IDX_SYNCH] + _connectionCount[IDX_ASYNC]));
- else
- TC_LOG_ERROR("sql.driver", "DatabasePool %s NOT opened. There were errors opening the MySQL connections. Check your SQLDriverLogFile "
- "for specific errors. Read wiki at http://collab.kpsn.org/display/tc/TrinityCore+Home", GetDatabaseName());
+
return res;
}
@@ -112,8 +103,6 @@ class DatabaseWorkerPool
for (uint8 i = 0; i < _connectionCount[IDX_ASYNC]; ++i)
{
T* t = _connections[IDX_ASYNC][i];
- DatabaseWorker* worker = t->m_worker;
- delete worker;
t->Close(); //! Closes the actualy MySQL connection.
}
@@ -442,7 +431,7 @@ class DatabaseWorkerPool
if (str.empty())
return;
- char* buf = new char[str.size()*2+1];
+ char* buf = new char[str.size() * 2 + 1];
EscapeString(buf, str.c_str(), str.size());
str = buf;
delete[] buf;
@@ -470,6 +459,52 @@ class DatabaseWorkerPool
}
private:
+ bool OpenConnections(InternalIndex type, uint8 numConnections)
+ {
+ _connections[type].resize(numConnections);
+ for (uint8 i = 0; i < numConnections; ++i)
+ {
+ T* t;
+
+ if (type == IDX_ASYNC)
+ t = new T(_queue, *_connectionInfo);
+ else if (type == IDX_SYNCH)
+ t = new T(*_connectionInfo);
+
+ _connections[type][i] = t;
+ ++_connectionCount[type];
+
+ bool res = t->Open();
+
+ if (res)
+ {
+ if (mysql_get_server_version(t->GetHandle()) < MIN_MYSQL_SERVER_VERSION)
+ {
+ TC_LOG_ERROR("sql.driver", "TrinityCore does not support MySQL versions below 5.1");
+ res = false;
+ }
+ }
+
+ // Failed to open a connection or invalid version, abort and cleanup
+ if (!res)
+ {
+ TC_LOG_ERROR("sql.driver", "DatabasePool %s NOT opened. There were errors opening the MySQL connections. Check your SQLDriverLogFile "
+ "for specific errors. Read wiki at http://collab.kpsn.org/display/tc/TrinityCore+Home", GetDatabaseName());
+
+ while (_connectionCount[type] != 0)
+ {
+ T* t = _connections[type][i--];
+ delete t;
+ --_connectionCount[type];
+ }
+
+ return false;
+ }
+ }
+
+ return true;
+ }
+
unsigned long EscapeString(char *to, const char *from, unsigned long length)
{
if (!to || !from || !length)
@@ -507,14 +542,6 @@ class DatabaseWorkerPool
return _connectionInfo->database.c_str();
}
- private:
- enum _internalIndex
- {
- IDX_ASYNC,
- IDX_SYNCH,
- IDX_SIZE
- };
-
ProducerConsumerQueue<SQLOperation*>* _queue; //! Queue shared by async worker threads.
std::vector< std::vector<T*> > _connections;
uint32 _connectionCount[2]; //! Counter of MySQL connections;
diff --git a/src/server/shared/Database/MySQLConnection.cpp b/src/server/shared/Database/MySQLConnection.cpp
index e9fc20aef82..4e46ff0e3a1 100644
--- a/src/server/shared/Database/MySQLConnection.cpp
+++ b/src/server/shared/Database/MySQLConnection.cpp
@@ -57,12 +57,14 @@ m_connectionFlags(CONNECTION_ASYNC)
MySQLConnection::~MySQLConnection()
{
- ASSERT (m_Mysql); /// MySQL context must be present at this point
-
for (size_t i = 0; i < m_stmts.size(); ++i)
delete m_stmts[i];
- mysql_close(m_Mysql);
+ if (m_Mysql)
+ mysql_close(m_Mysql);
+
+ if (m_worker)
+ delete m_worker;
}
void MySQLConnection::Close()
diff --git a/src/server/worldserver/Main.cpp b/src/server/worldserver/Main.cpp
index a879dca78b9..2c393215f7d 100644
--- a/src/server/worldserver/Main.cpp
+++ b/src/server/worldserver/Main.cpp
@@ -87,6 +87,7 @@ bool StartDB();
void StopDB();
void WorldUpdateLoop();
void ClearOnlineAccounts();
+void ShutdownThreadPool(std::vector<std::thread>& threadPool);
variables_map GetConsoleArguments(int argc, char** argv, std::string& cfg_file, std::string& cfg_service);
/// Launch the Trinity server
@@ -179,7 +180,10 @@ extern int main(int argc, char** argv)
// Start the databases
if (!StartDB())
+ {
+ ShutdownThreadPool(threadPool);
return 1;
+ }
// Set server offline (not connectable)
LoginDatabase.DirectPExecute("UPDATE realmlist SET flag = (flag & ~%u) | %u WHERE id = '%d'", REALM_FLAG_OFFLINE, REALM_FLAG_INVALID, realmID);
@@ -236,13 +240,7 @@ extern int main(int argc, char** argv)
WorldUpdateLoop();
// Shutdown starts here
-
- _ioService.stop();
-
- for (auto& thread : threadPool)
- {
- thread.join();
- }
+ ShutdownThreadPool(threadPool);
sScriptMgr->OnShutdown();
@@ -296,6 +294,16 @@ extern int main(int argc, char** argv)
return World::GetExitCode();
}
+void ShutdownThreadPool(std::vector<std::thread>& threadPool)
+{
+ _ioService.stop();
+
+ for (auto& thread : threadPool)
+ {
+ thread.join();
+ }
+}
+
void WorldUpdateLoop()
{
uint32 realCurrTime = 0;