-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add setAutoCommit method to the Transaction class #1459
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,25 +41,26 @@ TransactionImpl::~TransactionImpl() | |
auto loop = connectionPtr_->loop(); | ||
loop->queueInLoop([conn = connectionPtr_, | ||
ucb = std::move(usedUpCallback_), | ||
commitCb = std::move(commitCallback_)]() { | ||
commitCb = std::move(commitCallback_), | ||
autoCommit = autoCommit_]() { | ||
conn->setIdleCallback([ucb = std::move(ucb)]() { | ||
if (ucb) | ||
ucb(); | ||
}); | ||
conn->execSql( | ||
"commit", | ||
autoCommit ? "commit" : "rollback", | ||
0, | ||
{}, | ||
{}, | ||
{}, | ||
[commitCb](const Result &) { | ||
[commitCb, autoCommit](const Result &) { | ||
LOG_TRACE << "Transaction committed!"; | ||
if (commitCb) | ||
if (autoCommit && commitCb) | ||
{ | ||
commitCb(true); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be same in execption callback I prefer not using this commitCb. It's confusing. |
||
}, | ||
[commitCb](const std::exception_ptr &ePtr) { | ||
[commitCb, autoCommit](const std::exception_ptr &ePtr) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is the last time we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are the arguments evaluated in left-to-right order? If not, we can not use move. |
||
try | ||
{ | ||
std::rethrow_exception(ePtr); | ||
|
@@ -68,7 +69,7 @@ TransactionImpl::~TransactionImpl() | |
{ | ||
LOG_ERROR << "Transaction submission failed:" | ||
<< e.base().what(); | ||
if (commitCb) | ||
if (autoCommit && commitCb) | ||
{ | ||
commitCb(false); | ||
} | ||
|
@@ -150,6 +151,69 @@ void TransactionImpl::execSqlInLoop( | |
} | ||
} | ||
|
||
void TransactionImpl::commit(std::function<void(bool)> callback) | ||
{ | ||
auto thisPtr = shared_from_this(); | ||
if (callback) | ||
{ | ||
commitCallback_ = std::move(callback); | ||
} | ||
loop_->runInLoop([thisPtr]() { | ||
if (thisPtr->isCommitedOrRolledback_) | ||
return; | ||
if (thisPtr->isWorking_) | ||
{ | ||
// push sql cmd to buffer; | ||
auto cmdPtr = std::make_shared<SqlCmd>(); | ||
cmdPtr->sql_ = "commit"; | ||
cmdPtr->parametersNumber_ = 0; | ||
cmdPtr->callback_ = [thisPtr](const Result &) { | ||
LOG_DEBUG << "Transaction commit!"; | ||
thisPtr->isCommitedOrRolledback_ = true; | ||
if (thisPtr->commitCallback_) | ||
{ | ||
thisPtr->commitCallback_(true); | ||
} | ||
}; | ||
cmdPtr->exceptionCallback_ = [thisPtr](const std::exception_ptr &) { | ||
thisPtr->isCommitedOrRolledback_ = true; | ||
if (thisPtr->commitCallback_) | ||
{ | ||
thisPtr->commitCallback_(false); | ||
} | ||
LOG_ERROR << "Transaction commit error"; | ||
}; | ||
cmdPtr->isRollbackOrCommitCmd_ = true; | ||
thisPtr->sqlCmdBuffer_.push_back(std::move(cmdPtr)); | ||
return; | ||
} | ||
thisPtr->isWorking_ = true; | ||
thisPtr->thisPtr_ = thisPtr; | ||
thisPtr->connectionPtr_->execSql( | ||
"commit", | ||
0, | ||
{}, | ||
{}, | ||
{}, | ||
[thisPtr](const Result &) { | ||
LOG_TRACE << "Transaction commit!"; | ||
thisPtr->isCommitedOrRolledback_ = true; | ||
if (thisPtr->commitCallback_) | ||
{ | ||
thisPtr->commitCallback_(true); | ||
} | ||
}, | ||
[thisPtr](const std::exception_ptr &) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is the last place we used |
||
LOG_ERROR << "Transaction commit error"; | ||
thisPtr->isCommitedOrRolledback_ = true; | ||
if (thisPtr->commitCallback_) | ||
{ | ||
thisPtr->commitCallback_(false); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
void TransactionImpl::rollback() | ||
{ | ||
auto thisPtr = shared_from_this(); | ||
|
@@ -172,7 +236,7 @@ void TransactionImpl::rollback() | |
thisPtr->isCommitedOrRolledback_ = true; | ||
LOG_ERROR << "Transaction roll back error"; | ||
}; | ||
cmdPtr->isRollbackCmd_ = true; | ||
cmdPtr->isRollbackOrCommitCmd_ = true; | ||
// Rollback cmd should be executed firstly, so we push it in front | ||
// of the list | ||
thisPtr->sqlCmdBuffer_.push_front(std::move(cmdPtr)); | ||
|
@@ -189,10 +253,8 @@ void TransactionImpl::rollback() | |
[thisPtr](const Result &) { | ||
LOG_TRACE << "Transaction roll back!"; | ||
thisPtr->isCommitedOrRolledback_ = true; | ||
// clearupCb(); | ||
}, | ||
[thisPtr](const std::exception_ptr &) { | ||
// clearupCb(); | ||
LOG_ERROR << "Transaction roll back error"; | ||
thisPtr->isCommitedOrRolledback_ = true; | ||
}); | ||
|
@@ -220,15 +282,15 @@ void TransactionImpl::execNewTask() | |
std::move(cmd->formats_), | ||
[callback = std::move(cmd->callback_), cmd, thisPtr]( | ||
const Result &r) { | ||
if (cmd->isRollbackCmd_) | ||
if (cmd->isRollbackOrCommitCmd_) | ||
{ | ||
thisPtr->isCommitedOrRolledback_ = true; | ||
} | ||
if (callback) | ||
callback(r); | ||
}, | ||
[cmd, thisPtr](const std::exception_ptr &ePtr) { | ||
if (!cmd->isRollbackCmd_) | ||
if (!cmd->isRollbackOrCommitCmd_) | ||
thisPtr->rollback(); | ||
else | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UB if the user manually commits then auto commit happens. Could the following happen?