Fix a rollback bug in transactions

This commit is contained in:
antao 2018-11-15 16:22:24 +08:00
parent 7611573153
commit 701105fb64
2 changed files with 27 additions and 4 deletions

View File

@ -24,6 +24,7 @@ PgTransactionImpl::PgTransactionImpl(const PgConnectionPtr &connPtr,
}
PgTransactionImpl::~PgTransactionImpl()
{
assert(!_isWorking);
if (!_isCommitedOrRolledback)
{
auto cb = _usedUpCallback;
@ -72,8 +73,9 @@ void PgTransactionImpl::execSql(const std::string &sql,
format,
rcb,
[exceptCallback, thisPtr](const std::exception_ptr &ePtr) {
exceptCallback(ePtr);
thisPtr->rollback();
if(exceptCallback)
exceptCallback(ePtr);
},
[thisPtr]() {
thisPtr->execNewTask();
@ -115,7 +117,7 @@ void PgTransactionImpl::rollback()
{
auto thisPtr = shared_from_this();
_loop->queueInLoop([thisPtr]() {
_loop->runInLoop([thisPtr]() {
if (thisPtr->_isCommitedOrRolledback)
return;
auto clearupCb = [thisPtr]() {
@ -139,7 +141,8 @@ void PgTransactionImpl::rollback()
cmd._exceptCb = [clearupCb](const std::exception_ptr &ePtr) {
clearupCb();
};
thisPtr->_sqlCmdBuffer.push_back(std::move(cmd));
//Rollback cmd should be executed firstly, so we push it in front of the list
thisPtr->_sqlCmdBuffer.push_front(std::move(cmd));
return;
}
thisPtr->_isWorking = true;

View File

@ -35,7 +35,27 @@ int main()
LOG_DEBUG << "start!";
{
auto trans = client->newTransaction();
//trans->rollback();
*trans << "delete from users where user_uuid=201" >>
[](const Result &r) {
std::cout << "delete " << r.affectedRows() << "user!!!!!" << std::endl;
} >>
[](const DrogonDbException &e) {
std::cout << e.base().what() << std::endl;
};
*trans << "dlelf from users" >>
[](const Result &r) {
std::cout << "delete " << r.affectedRows() << "user!!!!!" << std::endl;
} >>
[](const DrogonDbException &e) {
std::cout << e.base().what() << std::endl;
};
*trans << "dlelf111 from users" >>
[](const Result &r) {
std::cout << "delete " << r.affectedRows() << "user!!!!!" << std::endl;
} >>
[](const DrogonDbException &e) {
std::cout << e.base().what() << std::endl;
};
}
Mapper<User> mapper(client);
auto U = mapper.findByPrimaryKey(2);