Files
TCPlayerbotCore/src/server/database/Database/Transaction.cpp
T

139 lines
3.6 KiB
C++
Raw Normal View History

/*
2020-01-02 06:44:10 +01:00
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
2010-08-21 03:19:25 +02:00
*/
#include "Transaction.h"
2017-05-12 18:49:51 +02:00
#include "MySQLConnection.h"
#include "PreparedStatement.h"
#include <mysqld_error.h>
2010-08-21 03:19:25 +02:00
std::mutex TransactionTask::_deadlockLock;
2010-09-04 16:49:23 +02:00
//- Append a raw ad-hoc query to the transaction
void TransactionBase::Append(char const* sql)
2010-08-21 03:19:25 +02:00
{
2010-09-19 12:16:29 +02:00
SQLElementData data;
data.type = SQL_ELEMENT_RAW;
2010-09-04 16:49:23 +02:00
data.element.query = strdup(sql);
m_queries.push_back(data);
2010-08-21 03:19:25 +02:00
}
2010-09-04 16:49:23 +02:00
//- Append a prepared statement to the transaction
void TransactionBase::AppendPreparedStatement(PreparedStatementBase* stmt)
2010-09-04 16:49:23 +02:00
{
2010-09-19 12:16:29 +02:00
SQLElementData data;
data.type = SQL_ELEMENT_PREPARED;
2010-09-04 16:49:23 +02:00
data.element.stmt = stmt;
m_queries.push_back(data);
2010-09-04 16:49:23 +02:00
}
void TransactionBase::Cleanup()
2010-08-21 03:19:25 +02:00
{
// This might be called by explicit calls to Cleanup or by the auto-destructor
2011-06-18 19:51:28 +02:00
if (_cleanedUp)
return;
for (SQLElementData const& data : m_queries)
2010-08-21 03:19:25 +02:00
{
2010-09-04 16:49:23 +02:00
switch (data.type)
{
2010-09-19 12:16:29 +02:00
case SQL_ELEMENT_PREPARED:
2010-09-04 16:49:23 +02:00
delete data.element.stmt;
break;
2010-09-19 12:16:29 +02:00
case SQL_ELEMENT_RAW:
2010-09-04 16:49:23 +02:00
free((void*)(data.element.query));
break;
}
2010-08-21 03:19:25 +02:00
}
2017-05-12 18:49:51 +02:00
m_queries.clear();
_cleanedUp = true;
2010-08-21 03:19:25 +02:00
}
bool TransactionTask::Execute()
{
int errorCode = TryExecute();
if (!errorCode)
2011-01-13 20:07:09 +01:00
return true;
2010-08-21 03:19:25 +02:00
if (errorCode == ER_LOCK_DEADLOCK)
2010-08-21 03:19:25 +02:00
{
// Make sure only 1 async thread retries a transaction so they don't keep dead-locking each other
std::lock_guard<std::mutex> lock(_deadlockLock);
2011-01-13 20:07:09 +01:00
uint8 loopBreaker = 5; // Handle MySQL Errno 1213 without extending deadlock to the core itself
for (uint8 i = 0; i < loopBreaker; ++i)
if (!TryExecute())
2011-01-13 20:07:09 +01:00
return true;
2010-08-21 03:19:25 +02:00
}
2011-01-13 20:07:09 +01:00
// Clean up now.
CleanupOnFailure();
return false;
}
int TransactionTask::TryExecute()
{
return m_conn->ExecuteTransaction(m_trans);
}
void TransactionTask::CleanupOnFailure()
{
m_trans->Cleanup();
}
bool TransactionWithResultTask::Execute()
{
int errorCode = TryExecute();
if (!errorCode)
{
m_result.set_value(true);
return true;
}
if (errorCode == ER_LOCK_DEADLOCK)
{
// Make sure only 1 async thread retries a transaction so they don't keep dead-locking each other
std::lock_guard<std::mutex> lock(_deadlockLock);
uint8 loopBreaker = 5; // Handle MySQL Errno 1213 without extending deadlock to the core itself
for (uint8 i = 0; i < loopBreaker; ++i)
{
if (!TryExecute())
{
m_result.set_value(true);
return true;
}
}
}
// Clean up now.
CleanupOnFailure();
m_result.set_value(false);
return false;
}
bool TransactionCallback::InvokeIfReady()
{
if (m_future.valid() && m_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready)
{
m_callback(m_future.get());
return true;
}
2011-01-13 20:07:09 +01:00
return false;
2010-08-21 03:19:25 +02:00
}