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

#include "Transaction.h"

Inheritance diagram for TransactionWithResultTask:
TransactionTask SQLOperation

Public Member Functions

 TransactionWithResultTask (std::shared_ptr< TransactionBase > trans)
 
TransactionFuture GetFuture ()
 
- Public Member Functions inherited from TransactionTask
 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
 
- Protected Member Functions inherited from TransactionTask
bool Execute () override
 
int TryExecute ()
 
void CleanupOnFailure ()
 

Protected Attributes

TransactionPromise m_result
 
- Protected Attributes inherited from TransactionTask
std::shared_ptr< TransactionBasem_trans
 

Additional Inherited Members

- Public Attributes inherited from SQLOperation
MySQLConnectionm_conn {nullptr}
 
- Static Protected Attributes inherited from TransactionTask
static std::mutex _deadlockLock
 

Detailed Description

Constructor & Destructor Documentation

◆ TransactionWithResultTask()

TransactionWithResultTask::TransactionWithResultTask ( std::shared_ptr< TransactionBase trans)
inline
98: TransactionTask(trans) { }
Definition: Transaction.h:75

Member Function Documentation

◆ Execute()

bool TransactionWithResultTask::Execute ( )
overrideprotectedvirtual

Reimplemented from TransactionTask.

142{
143 int errorCode = TryExecute();
144 if (!errorCode)
145 {
146 m_result.set_value(true);
147 return true;
148 }
149
150 if (errorCode == ER_LOCK_DEADLOCK)
151 {
152 std::ostringstream threadIdStream;
153 threadIdStream << std::this_thread::get_id();
154 std::string threadId = threadIdStream.str();
155
156 {
157 // Make sure only 1 async thread retries a transaction so they don't keep dead-locking each other
158 std::lock_guard<std::mutex> lock(_deadlockLock);
159
160 for (Milliseconds loopDuration{}, startMSTime = GetTimeMS(); loopDuration <= DEADLOCK_MAX_RETRY_TIME_MS; loopDuration = GetMSTimeDiffToNow(startMSTime))
161 {
162 if (!TryExecute())
163 {
164 m_result.set_value(true);
165 return true;
166 }
167
168 LOG_WARN("sql.sql", "Deadlocked SQL Transaction, retrying. Loop timer: {} ms, Thread Id: {}", loopDuration.count(), threadId);
169 }
170 }
171
172 LOG_ERROR("sql.sql", "Fatal deadlocked SQL Transaction, it will not be retried anymore. Thread Id: {}", threadId);
173 }
174
175 // Clean up now.
177 m_result.set_value(false);
178
179 return false;
180}
#define LOG_ERROR(filterType__,...)
Definition: Log.h:157
#define LOG_WARN(filterType__,...)
Definition: Log.h:161
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
TransactionPromise m_result
Definition: Transaction.h:105

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

◆ GetFuture()

TransactionFuture TransactionWithResultTask::GetFuture ( )
inline
100{ return m_result.get_future(); }

Referenced by DatabaseWorkerPool< T >::AsyncCommitTransaction().

Member Data Documentation

◆ m_result

TransactionPromise TransactionWithResultTask::m_result
protected

Referenced by Execute().