Core/Database: Refactor MySQLConnection constructors and async thread creation

This commit is contained in:
Shauren
2023-12-14 17:00:58 +01:00
parent 685ead8642
commit 5c59e2a248
11 changed files with 25 additions and 50 deletions

View File

@@ -382,9 +382,13 @@ uint32 DatabaseWorkerPool<T>::OpenConnections(InternalIndex type, uint8 numConne
switch (type)
{
case IDX_ASYNC:
return std::make_unique<T>(_queue.get(), *_connectionInfo);
{
auto c = std::make_unique<T>(*_connectionInfo, CONNECTION_ASYNC);
c->StartDatabaseWorkerThread(_queue.get());
return c;
}
case IDX_SYNCH:
return std::make_unique<T>(*_connectionInfo);
return std::make_unique<T>(*_connectionInfo, CONNECTION_SYNCH);
default:
ABORT();
}

View File

@@ -790,11 +790,7 @@ void CharacterDatabaseConnection::DoPrepareStatements()
PrepareStatement(CHAR_INS_INSTANCE, "INSERT INTO instance (instanceId, data, completedEncountersMask, entranceWorldSafeLocId) VALUES (?, ?, ?, ?)", CONNECTION_ASYNC);
}
CharacterDatabaseConnection::CharacterDatabaseConnection(MySQLConnectionInfo& connInfo) : MySQLConnection(connInfo)
{
}
CharacterDatabaseConnection::CharacterDatabaseConnection(ProducerConsumerQueue<SQLOperation*>* q, MySQLConnectionInfo& connInfo) : MySQLConnection(q, connInfo)
CharacterDatabaseConnection::CharacterDatabaseConnection(MySQLConnectionInfo& connInfo, ConnectionFlags connectionFlags) : MySQLConnection(connInfo, connectionFlags)
{
}

View File

@@ -653,9 +653,7 @@ class TC_DATABASE_API CharacterDatabaseConnection : public MySQLConnection
public:
typedef CharacterDatabaseStatements Statements;
//- Constructors for sync and async connections
CharacterDatabaseConnection(MySQLConnectionInfo& connInfo);
CharacterDatabaseConnection(ProducerConsumerQueue<SQLOperation*>* q, MySQLConnectionInfo& connInfo);
CharacterDatabaseConnection(MySQLConnectionInfo& connInfo, ConnectionFlags connectionFlags);
~CharacterDatabaseConnection();
//- Loads database type specific prepared statements

View File

@@ -1954,11 +1954,7 @@ void HotfixDatabaseConnection::DoPrepareStatements()
PREPARE_MAX_ID_STMT(HOTFIX_SEL_WORLD_STATE_EXPRESSION, "SELECT MAX(ID) + 1 FROM world_state_expression", CONNECTION_SYNCH);
}
HotfixDatabaseConnection::HotfixDatabaseConnection(MySQLConnectionInfo& connInfo) : MySQLConnection(connInfo)
{
}
HotfixDatabaseConnection::HotfixDatabaseConnection(ProducerConsumerQueue<SQLOperation*>* q, MySQLConnectionInfo& connInfo) : MySQLConnection(q, connInfo)
HotfixDatabaseConnection::HotfixDatabaseConnection(MySQLConnectionInfo& connInfo, ConnectionFlags connectionFlags) : MySQLConnection(connInfo, connectionFlags)
{
}

View File

@@ -1138,9 +1138,7 @@ class TC_DATABASE_API HotfixDatabaseConnection : public MySQLConnection
public:
typedef HotfixDatabaseStatements Statements;
//- Constructors for sync and async connections
HotfixDatabaseConnection(MySQLConnectionInfo& connInfo);
HotfixDatabaseConnection(ProducerConsumerQueue<SQLOperation*>* q, MySQLConnectionInfo& connInfo);
HotfixDatabaseConnection(MySQLConnectionInfo& connInfo, ConnectionFlags connectionFlags);
~HotfixDatabaseConnection();
//- Loads database type specific prepared statements

View File

@@ -192,11 +192,7 @@ void LoginDatabaseConnection::DoPrepareStatements()
"ON DUPLICATE KEY UPDATE illusionMask = illusionMask | VALUES(illusionMask)", CONNECTION_ASYNC);
}
LoginDatabaseConnection::LoginDatabaseConnection(MySQLConnectionInfo& connInfo) : MySQLConnection(connInfo)
{
}
LoginDatabaseConnection::LoginDatabaseConnection(ProducerConsumerQueue<SQLOperation*>* q, MySQLConnectionInfo& connInfo) : MySQLConnection(q, connInfo)
LoginDatabaseConnection::LoginDatabaseConnection(MySQLConnectionInfo& connInfo, ConnectionFlags connectionFlags) : MySQLConnection(connInfo, connectionFlags)
{
}

View File

@@ -184,9 +184,7 @@ class TC_DATABASE_API LoginDatabaseConnection : public MySQLConnection
public:
typedef LoginDatabaseStatements Statements;
//- Constructors for sync and async connections
LoginDatabaseConnection(MySQLConnectionInfo& connInfo);
LoginDatabaseConnection(ProducerConsumerQueue<SQLOperation*>* q, MySQLConnectionInfo& connInfo);
LoginDatabaseConnection(MySQLConnectionInfo& connInfo, ConnectionFlags connectionFlags);
~LoginDatabaseConnection();
//- Loads database type specific prepared statements

View File

@@ -80,11 +80,7 @@ void WorldDatabaseConnection::DoPrepareStatements()
PrepareStatement(WORLD_INS_CONDITION, "INSERT INTO conditions (SourceTypeOrReferenceId, SourceGroup, SourceEntry, SourceId, ElseGroup, ConditionTypeOrReference, ConditionTarget, ConditionValue1, ConditionValue2, ConditionValue3, NegativeCondition, ErrorType, ErrorTextId, ScriptName, Comment) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", CONNECTION_ASYNC);
}
WorldDatabaseConnection::WorldDatabaseConnection(MySQLConnectionInfo& connInfo) : MySQLConnection(connInfo)
{
}
WorldDatabaseConnection::WorldDatabaseConnection(ProducerConsumerQueue<SQLOperation*>* q, MySQLConnectionInfo& connInfo) : MySQLConnection(q, connInfo)
WorldDatabaseConnection::WorldDatabaseConnection(MySQLConnectionInfo& connInfo, ConnectionFlags connectionFlags) : MySQLConnection(connInfo, connectionFlags)
{
}

View File

@@ -98,9 +98,7 @@ class TC_DATABASE_API WorldDatabaseConnection : public MySQLConnection
public:
typedef WorldDatabaseStatements Statements;
//- Constructors for sync and async connections
WorldDatabaseConnection(MySQLConnectionInfo& connInfo);
WorldDatabaseConnection(ProducerConsumerQueue<SQLOperation*>* q, MySQLConnectionInfo& connInfo);
WorldDatabaseConnection(MySQLConnectionInfo& connInfo, ConnectionFlags connectionFlags);
~WorldDatabaseConnection();
//- Loads database type specific prepared statements

View File

@@ -47,23 +47,13 @@ MySQLConnectionInfo::MySQLConnectionInfo(std::string const& infoString)
ssl.assign(tokens[5]);
}
MySQLConnection::MySQLConnection(MySQLConnectionInfo& connInfo) :
MySQLConnection::MySQLConnection(MySQLConnectionInfo& connInfo, ConnectionFlags connectionFlags) :
m_reconnecting(false),
m_prepareError(false),
m_queue(nullptr),
m_Mysql(nullptr),
m_connectionInfo(connInfo),
m_connectionFlags(CONNECTION_SYNCH) { }
MySQLConnection::MySQLConnection(ProducerConsumerQueue<SQLOperation*>* queue, MySQLConnectionInfo& connInfo) :
m_reconnecting(false),
m_prepareError(false),
m_queue(queue),
m_Mysql(nullptr),
m_connectionInfo(connInfo),
m_connectionFlags(CONNECTION_ASYNC)
m_connectionFlags(connectionFlags)
{
m_worker = std::make_unique<DatabaseWorker>(m_queue, this);
}
MySQLConnection::~MySQLConnection()
@@ -452,6 +442,11 @@ uint32 MySQLConnection::GetLastError()
return mysql_errno(m_Mysql);
}
void MySQLConnection::StartDatabaseWorkerThread(ProducerConsumerQueue<SQLOperation*>* queue)
{
m_worker = std::make_unique<DatabaseWorker>(queue, this);
}
bool MySQLConnection::LockIfReady()
{
return m_Mutex.try_lock();

View File

@@ -57,11 +57,10 @@ class TC_DATABASE_API MySQLConnection
friend class PingOperation;
public:
MySQLConnection(MySQLConnectionInfo& connInfo); //!< Constructor for synchronous connections.
MySQLConnection(ProducerConsumerQueue<SQLOperation*>* queue, MySQLConnectionInfo& connInfo); //!< Constructor for asynchronous connections.
MySQLConnection(MySQLConnectionInfo& connInfo, ConnectionFlags connectionFlags);
virtual ~MySQLConnection();
virtual uint32 Open();
uint32 Open();
void Close();
bool PrepareStatements();
@@ -82,6 +81,8 @@ class TC_DATABASE_API MySQLConnection
uint32 GetLastError();
void StartDatabaseWorkerThread(ProducerConsumerQueue<SQLOperation*>* queue);
protected:
/// Tries to acquire lock. If lock is acquired by another thread
/// the calling parent will just try another connection
@@ -105,7 +106,6 @@ class TC_DATABASE_API MySQLConnection
private:
bool _HandleMySQLErrno(uint32 errNo, uint8 attempts = 5);
ProducerConsumerQueue<SQLOperation*>* m_queue; //!< Queue shared with other asynchronous connections.
std::unique_ptr<DatabaseWorker> m_worker; //!< Core worker task.
MySQLHandle* m_Mysql; //!< MySQL Handle.
MySQLConnectionInfo& m_connectionInfo; //!< Connection info (used for logging)