AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
TransactionTask Class Reference

#include "Transaction.h"

Inheritance diagram for TransactionTask:
SQLOperation TransactionWithResultTask

Public Member Functions

 TransactionTask (std::shared_ptr< TransactionBase > trans)
 
 ~TransactionTask () override=default
 
- Public Member Functions inherited from SQLOperation
 SQLOperation ()=default
 
virtual ~SQLOperation ()=default
 
virtual int call ()
 
virtual bool Execute ()=0
 
virtual void SetConnection (MySQLConnection *con)
 

Protected Member Functions

bool Execute () override
 
int TryExecute ()
 
void CleanupOnFailure ()
 

Protected Attributes

std::shared_ptr< TransactionBasem_trans
 

Static Protected Attributes

static std::mutex _deadlockLock
 

Friends

template<class T >
class DatabaseWorkerPool
 
class DatabaseWorker
 
class TransactionCallback
 

Additional Inherited Members

- Public Attributes inherited from SQLOperation
MySQLConnectionm_conn {nullptr}
 

Detailed Description

Low level class

Constructor & Destructor Documentation

◆ TransactionTask()

TransactionTask::TransactionTask ( std::shared_ptr< TransactionBase trans)
inline
83: m_trans(std::move(trans)) { }
std::shared_ptr< TransactionBase > m_trans
Definition: Transaction.h:91

◆ ~TransactionTask()

TransactionTask::~TransactionTask ( )
overridedefault

Member Function Documentation

◆ CleanupOnFailure()

void TransactionTask::CleanupOnFailure ( )
protected
137{
138 m_trans->Cleanup();
139}

References m_trans.

Referenced by Execute(), and TransactionWithResultTask::Execute().

◆ Execute()

bool TransactionTask::Execute ( )
overrideprotectedvirtual

Implements SQLOperation.

Reimplemented in TransactionWithResultTask.

97{
98 int errorCode = TryExecute();
99
100 if (!errorCode)
101 return true;
102
103 if (errorCode == ER_LOCK_DEADLOCK)
104 {
105 std::ostringstream threadIdStream;
106 threadIdStream << std::this_thread::get_id();
107 std::string threadId = threadIdStream.str();
108
109 {
110 // Make sure only 1 async thread retries a transaction so they don't keep dead-locking each other
111 std::lock_guard<std::mutex> lock(_deadlockLock);
112
113 for (Milliseconds loopDuration{}, startMSTime = GetTimeMS(); loopDuration <= DEADLOCK_MAX_RETRY_TIME_MS; loopDuration = GetMSTimeDiffToNow(startMSTime))
114 {
115 if (!TryExecute())
116 return true;
117
118 LOG_WARN("sql.sql", "Deadlocked SQL Transaction, retrying. Loop timer: {} ms, Thread Id: {}", loopDuration.count(), threadId);
119 }
120 }
121
122 LOG_ERROR("sql.sql", "Fatal deadlocked SQL Transaction, it will not be retried anymore. Thread Id: {}", threadId);
123 }
124
125 // Clean up now.
127
128 return false;
129}
#define LOG_ERROR(filterType__,...)
Definition: Log.h:156
#define LOG_WARN(filterType__,...)
Definition: Log.h:160
Milliseconds GetTimeMS()
Definition: Timer.h:84
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition: Timer.h:131
std::chrono::milliseconds Milliseconds
Milliseconds shorthand typedef.
Definition: Duration.h:27
constexpr Milliseconds DEADLOCK_MAX_RETRY_TIME_MS
Definition: Transaction.cpp:30
int TryExecute()
Definition: Transaction.cpp:131
static std::mutex _deadlockLock
Definition: Transaction.h:92
void CleanupOnFailure()
Definition: Transaction.cpp:136

References _deadlockLock, CleanupOnFailure(), DEADLOCK_MAX_RETRY_TIME_MS, GetMSTimeDiffToNow(), GetTimeMS(), LOG_ERROR, LOG_WARN, and TryExecute().

◆ TryExecute()

int TransactionTask::TryExecute ( )
protected
132{
134}
int ExecuteTransaction(std::shared_ptr< TransactionBase > transaction)
Definition: MySQLConnection.cpp:382
MySQLConnection * m_conn
Definition: SQLOperation.h:56

References MySQLConnection::ExecuteTransaction(), SQLOperation::m_conn, and m_trans.

Referenced by Execute(), and TransactionWithResultTask::Execute().

Friends And Related Function Documentation

◆ DatabaseWorker

friend class DatabaseWorker
friend

◆ DatabaseWorkerPool

template<class T >
friend class DatabaseWorkerPool
friend

◆ TransactionCallback

friend class TransactionCallback
friend

Member Data Documentation

◆ _deadlockLock

std::mutex TransactionTask::_deadlockLock
staticprotected

◆ m_trans

std::shared_ptr<TransactionBase> TransactionTask::m_trans
protected

Referenced by CleanupOnFailure(), and TryExecute().