mirror of
https://github.com/TrinityCore/TrinityCore.git
synced 2026-01-23 18:36:31 +01:00
Implemented separate connection for SqlDelayThread
This should result in less locking when accessing database, and improved performance. * Changed SqlDelay queue type to monitored queue, and removed 10ms check period. thanks to Derex, mknjc, Jeniczek and Christyan for ideas and tests. --HG-- branch : trunk
This commit is contained in:
@@ -50,7 +50,7 @@ class TRINITY_DLL_SPEC Database
|
||||
virtual ~Database();
|
||||
|
||||
virtual bool Initialize(const char *infoString);
|
||||
virtual void InitDelayThread() = 0;
|
||||
virtual void InitDelayThread(const char *infoString) = 0;
|
||||
virtual void HaltDelayThread() = 0;
|
||||
|
||||
virtual QueryResult* Query(const char *sql) = 0;
|
||||
|
||||
@@ -70,7 +70,7 @@ DatabaseMysql::~DatabaseMysql()
|
||||
mysql_library_end();
|
||||
}
|
||||
|
||||
bool DatabaseMysql::Initialize(const char *infoString)
|
||||
bool DatabaseMysql::Initialize(const char *infoString, bool initDelayThread)
|
||||
{
|
||||
|
||||
if(!Database::Initialize(infoString))
|
||||
@@ -84,8 +84,8 @@ bool DatabaseMysql::Initialize(const char *infoString)
|
||||
sLog.outError( "Could not initialize Mysql connection" );
|
||||
return false;
|
||||
}
|
||||
|
||||
InitDelayThread();
|
||||
if(initDelayThread)
|
||||
InitDelayThread(infoString);
|
||||
|
||||
Tokens tokens = StrSplit(infoString, ";");
|
||||
|
||||
@@ -403,12 +403,12 @@ unsigned long DatabaseMysql::escape_string(char *to, const char *from, unsigned
|
||||
return(mysql_real_escape_string(mMysql, to, from, length));
|
||||
}
|
||||
|
||||
void DatabaseMysql::InitDelayThread()
|
||||
void DatabaseMysql::InitDelayThread(const char* infoString)
|
||||
{
|
||||
assert(!m_delayThread);
|
||||
|
||||
//New delay thread for delay execute
|
||||
m_delayThread = new ZThread::Thread(m_threadBody = new MySQLDelayThread(this));
|
||||
m_delayThread = new ZThread::Thread(m_threadBody = new MySQLDelayThread(this,infoString));
|
||||
}
|
||||
|
||||
void DatabaseMysql::HaltDelayThread()
|
||||
|
||||
@@ -45,8 +45,8 @@ class TRINITY_DLL_SPEC DatabaseMysql : public Database
|
||||
|
||||
//! Initializes Mysql and connects to a server.
|
||||
/*! infoString should be formated like hostname;username;password;database. */
|
||||
bool Initialize(const char *infoString);
|
||||
void InitDelayThread();
|
||||
bool Initialize(const char *infoString, bool initDelayThread = true);
|
||||
void InitDelayThread(const char* infoString);
|
||||
void HaltDelayThread();
|
||||
QueryResult* Query(const char *sql);
|
||||
bool Execute(const char *sql);
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
class MySQLDelayThread : public SqlDelayThread
|
||||
{
|
||||
public:
|
||||
MySQLDelayThread(Database* db) : SqlDelayThread(db) {}
|
||||
MySQLDelayThread(Database* db,const char* infoString) : SqlDelayThread(db, infoString) {}
|
||||
void Stop() { SqlDelayThread::Stop(); }
|
||||
};
|
||||
#endif //__MYSQLDELAYTHREAD_H
|
||||
|
||||
@@ -22,8 +22,12 @@
|
||||
#include "Database/SqlOperations.h"
|
||||
#include "DatabaseEnv.h"
|
||||
|
||||
SqlDelayThread::SqlDelayThread(Database* db) : m_dbEngine(db), m_running(true)
|
||||
SqlDelayThread::SqlDelayThread(Database* db, const char* infoString) :m_running(true)
|
||||
{
|
||||
|
||||
m_dbEngine = new DatabaseType;
|
||||
((DatabaseType*)m_dbEngine)->Initialize(infoString, false);
|
||||
|
||||
}
|
||||
|
||||
void SqlDelayThread::run()
|
||||
@@ -35,15 +39,17 @@ void SqlDelayThread::run()
|
||||
|
||||
while (m_running)
|
||||
{
|
||||
// if the running state gets turned off while sleeping
|
||||
// empty the queue before exiting
|
||||
ZThread::Thread::sleep(10);
|
||||
while (!m_sqlQueue.empty())
|
||||
{
|
||||
s = m_sqlQueue.next();
|
||||
s->Execute(m_dbEngine);
|
||||
delete s;
|
||||
}
|
||||
try
|
||||
{
|
||||
s = m_sqlQueue.next();
|
||||
}
|
||||
catch(...)
|
||||
{continue;}
|
||||
if(!s)
|
||||
continue;
|
||||
s->Execute(m_dbEngine);
|
||||
delete s;
|
||||
|
||||
}
|
||||
|
||||
#ifndef DO_POSTGRESQL
|
||||
@@ -54,5 +60,6 @@ void SqlDelayThread::run()
|
||||
void SqlDelayThread::Stop()
|
||||
{
|
||||
m_running = false;
|
||||
m_sqlQueue.cancel();
|
||||
}
|
||||
|
||||
|
||||
@@ -24,14 +24,16 @@
|
||||
#include "zthread/Thread.h"
|
||||
#include "zthread/Runnable.h"
|
||||
#include "zthread/FastMutex.h"
|
||||
#include "zthread/LockedQueue.h"
|
||||
#include "zthread/MonitoredQueue.h"
|
||||
|
||||
class Database;
|
||||
class SqlOperation;
|
||||
|
||||
|
||||
|
||||
class SqlDelayThread : public ZThread::Runnable
|
||||
{
|
||||
typedef ZThread::LockedQueue<SqlOperation*, ZThread::FastMutex> SqlQueue;
|
||||
typedef ZThread::MonitoredQueue<SqlOperation*, ZThread::FastMutex> SqlQueue;
|
||||
private:
|
||||
SqlQueue m_sqlQueue; ///< Queue of SQL statements
|
||||
Database* m_dbEngine; ///< Pointer to used Database engine
|
||||
@@ -39,7 +41,7 @@ class SqlDelayThread : public ZThread::Runnable
|
||||
|
||||
SqlDelayThread();
|
||||
public:
|
||||
SqlDelayThread(Database* db);
|
||||
SqlDelayThread(Database* db,const char* infoString);
|
||||
|
||||
///< Put sql statement to delay queue
|
||||
inline bool Delay(SqlOperation* sql) { m_sqlQueue.add(sql); return true; }
|
||||
|
||||
Reference in New Issue
Block a user