From 90e63c1a0f45316fc8a10c9532c7802956f75b6d Mon Sep 17 00:00:00 2001 From: antao Date: Thu, 21 Feb 2019 20:36:18 +0800 Subject: [PATCH] Modify the TransactionImpl class --- orm_lib/src/TransactionImpl.cc | 24 +++++++++++++++++------- orm_lib/src/TransactionImpl.h | 1 + 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/orm_lib/src/TransactionImpl.cc b/orm_lib/src/TransactionImpl.cc index 8cbec4dc..1cb03bfb 100644 --- a/orm_lib/src/TransactionImpl.cc +++ b/orm_lib/src/TransactionImpl.cc @@ -82,6 +82,7 @@ void TransactionImpl::execSql(std::string &&sql, if (!thisPtr->_isWorking) { thisPtr->_isWorking = true; + thisPtr->_thisPtr = thisPtr; thisPtr->_connectionPtr->execSql(std::move(sql), paraNum, std::move(parameters), @@ -152,6 +153,7 @@ void TransactionImpl::rollback() return; } thisPtr->_isWorking = true; + thisPtr->_thisPtr = thisPtr; thisPtr->_connectionPtr->execSql("rollback", 0, std::vector(), @@ -173,13 +175,14 @@ void TransactionImpl::rollback() void TransactionImpl::execNewTask() { _loop->assertInLoopThread(); + _thisPtr.reset(); assert(_isWorking); if (!_isCommitedOrRolledback) { auto thisPtr = shared_from_this(); if (!_sqlCmdBuffer.empty()) { - auto cmd = _sqlCmdBuffer.front(); + auto cmd = std::move(_sqlCmdBuffer.front()); _sqlCmdBuffer.pop_front(); auto conn = _connectionPtr; conn->execSql(std::move(cmd._sql), @@ -187,10 +190,21 @@ void TransactionImpl::execNewTask() std::move(cmd._parameters), std::move(cmd._length), std::move(cmd._format), - std::move(cmd._cb), + [callback = std::move(cmd._cb), cmd, thisPtr](const Result &r) { + if (cmd._isRollbackCmd) + { + thisPtr->_isCommitedOrRolledback = true; + } + if (callback) + callback(r); + }, [cmd, thisPtr](const std::exception_ptr &ePtr) { if (!cmd._isRollbackCmd) thisPtr->rollback(); + else + { + thisPtr->_isCommitedOrRolledback = true; + } if (cmd._exceptCb) cmd._exceptCb(ePtr); }); @@ -239,6 +253,7 @@ void TransactionImpl::doBegin() assert(!thisPtr->_isWorking); assert(!thisPtr->_isCommitedOrRolledback); thisPtr->_isWorking = true; + thisPtr->_thisPtr = thisPtr; thisPtr->_connectionPtr->execSql("begin", 0, std::vector(), @@ -249,11 +264,6 @@ void TransactionImpl::doBegin() }, [thisPtr](const std::exception_ptr &ePtr) { thisPtr->_isCommitedOrRolledback = true; - - if (thisPtr->_usedUpCallback) - { - thisPtr->_usedUpCallback(); - } }); }); } diff --git a/orm_lib/src/TransactionImpl.h b/orm_lib/src/TransactionImpl.h index 5fc13d52..72eb8fc1 100644 --- a/orm_lib/src/TransactionImpl.h +++ b/orm_lib/src/TransactionImpl.h @@ -66,6 +66,7 @@ class TransactionImpl : public Transaction, public std::enable_shared_from_this< void doBegin(); trantor::EventLoop *_loop; std::function _commitCallback; + std::shared_ptr _thisPtr; }; } // namespace orm } // namespace drogon