diff options
author | Shauren <shauren.trinity@gmail.com> | 2020-04-14 16:23:44 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2020-04-14 16:23:44 +0200 |
commit | 0f0ca3a9194d76afa0227943e86469ad8368c5e2 (patch) | |
tree | ca267417fdb6be1eb9c78dfd05b7449376a0eaf9 /src/server/database/Database/Transaction.cpp | |
parent | 34b393ab64d1c53e7e5828e735b4c31e61eaf950 (diff) |
Core/DBLayer: Implement async transaction completion callbacks
Diffstat (limited to 'src/server/database/Database/Transaction.cpp')
-rw-r--r-- | src/server/database/Database/Transaction.cpp | 56 |
1 files changed, 54 insertions, 2 deletions
diff --git a/src/server/database/Database/Transaction.cpp b/src/server/database/Database/Transaction.cpp index ecf0ba25a7b..27aa2a4808c 100644 --- a/src/server/database/Database/Transaction.cpp +++ b/src/server/database/Database/Transaction.cpp @@ -65,7 +65,7 @@ void TransactionBase::Cleanup() bool TransactionTask::Execute() { - int errorCode = m_conn->ExecuteTransaction(m_trans); + int errorCode = TryExecute(); if (!errorCode) return true; @@ -75,12 +75,64 @@ bool TransactionTask::Execute() 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 (!m_conn->ExecuteTransaction(m_trans)) + if (!TryExecute()) return true; } // 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; + } return false; } |