Modify the TransactionImpl class

This commit is contained in:
antao 2019-02-21 20:36:18 +08:00
parent 52f1888a14
commit 90e63c1a0f
2 changed files with 18 additions and 7 deletions

View File

@ -82,6 +82,7 @@ void TransactionImpl::execSql(std::string &&sql,
if (!thisPtr->_isWorking) if (!thisPtr->_isWorking)
{ {
thisPtr->_isWorking = true; thisPtr->_isWorking = true;
thisPtr->_thisPtr = thisPtr;
thisPtr->_connectionPtr->execSql(std::move(sql), thisPtr->_connectionPtr->execSql(std::move(sql),
paraNum, paraNum,
std::move(parameters), std::move(parameters),
@ -152,6 +153,7 @@ void TransactionImpl::rollback()
return; return;
} }
thisPtr->_isWorking = true; thisPtr->_isWorking = true;
thisPtr->_thisPtr = thisPtr;
thisPtr->_connectionPtr->execSql("rollback", thisPtr->_connectionPtr->execSql("rollback",
0, 0,
std::vector<const char *>(), std::vector<const char *>(),
@ -173,13 +175,14 @@ void TransactionImpl::rollback()
void TransactionImpl::execNewTask() void TransactionImpl::execNewTask()
{ {
_loop->assertInLoopThread(); _loop->assertInLoopThread();
_thisPtr.reset();
assert(_isWorking); assert(_isWorking);
if (!_isCommitedOrRolledback) if (!_isCommitedOrRolledback)
{ {
auto thisPtr = shared_from_this(); auto thisPtr = shared_from_this();
if (!_sqlCmdBuffer.empty()) if (!_sqlCmdBuffer.empty())
{ {
auto cmd = _sqlCmdBuffer.front(); auto cmd = std::move(_sqlCmdBuffer.front());
_sqlCmdBuffer.pop_front(); _sqlCmdBuffer.pop_front();
auto conn = _connectionPtr; auto conn = _connectionPtr;
conn->execSql(std::move(cmd._sql), conn->execSql(std::move(cmd._sql),
@ -187,10 +190,21 @@ void TransactionImpl::execNewTask()
std::move(cmd._parameters), std::move(cmd._parameters),
std::move(cmd._length), std::move(cmd._length),
std::move(cmd._format), 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) { [cmd, thisPtr](const std::exception_ptr &ePtr) {
if (!cmd._isRollbackCmd) if (!cmd._isRollbackCmd)
thisPtr->rollback(); thisPtr->rollback();
else
{
thisPtr->_isCommitedOrRolledback = true;
}
if (cmd._exceptCb) if (cmd._exceptCb)
cmd._exceptCb(ePtr); cmd._exceptCb(ePtr);
}); });
@ -239,6 +253,7 @@ void TransactionImpl::doBegin()
assert(!thisPtr->_isWorking); assert(!thisPtr->_isWorking);
assert(!thisPtr->_isCommitedOrRolledback); assert(!thisPtr->_isCommitedOrRolledback);
thisPtr->_isWorking = true; thisPtr->_isWorking = true;
thisPtr->_thisPtr = thisPtr;
thisPtr->_connectionPtr->execSql("begin", thisPtr->_connectionPtr->execSql("begin",
0, 0,
std::vector<const char *>(), std::vector<const char *>(),
@ -249,11 +264,6 @@ void TransactionImpl::doBegin()
}, },
[thisPtr](const std::exception_ptr &ePtr) { [thisPtr](const std::exception_ptr &ePtr) {
thisPtr->_isCommitedOrRolledback = true; thisPtr->_isCommitedOrRolledback = true;
if (thisPtr->_usedUpCallback)
{
thisPtr->_usedUpCallback();
}
}); });
}); });
} }

View File

@ -66,6 +66,7 @@ class TransactionImpl : public Transaction, public std::enable_shared_from_this<
void doBegin(); void doBegin();
trantor::EventLoop *_loop; trantor::EventLoop *_loop;
std::function<void(bool)> _commitCallback; std::function<void(bool)> _commitCallback;
std::shared_ptr<TransactionImpl> _thisPtr;
}; };
} // namespace orm } // namespace orm
} // namespace drogon } // namespace drogon