Skip to content
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

Ensure crash in commit/rollback does not try twice #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/sqlite/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,17 @@ namespace sqlite{
* \return \c true if transaction is still active, \c false otherwise
*/
bool isActive() const { return m_isActive; }
/** \brief Allow to check if transaction handled by this object is
* currently in the middle of a COMMIT/Rollback operation. It is used
* for the handling of exceptions during commit/rollback operations.
* \return \c true if transaction is committing/rolling back, \c false otherwise
*/
bool isEnding() const { return m_isEnding; }
private:
void exec(std::string const &);
connection & m_con;
bool m_isActive; ///< if \c true there is a transaction currently opened
bool m_isEnding; ///< if \c true there is a transaction is currently ending
};
}

Expand Down
18 changes: 17 additions & 1 deletion src/sqlite/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,18 @@ namespace sqlite{
}

transaction::~transaction(){
// when the transaction is stuck ending, the commit/rollback failed
// and we don't want to take further action, we must assume there is
// nothing we can do.
if (m_isEnding) {
return;
}
if (m_isActive) {
rollback();
try {
rollback();
} catch (...) {
// We can't recover in the destructor, hope for the best and continue.
}
}
}

Expand All @@ -61,17 +71,23 @@ namespace sqlite{
}

void transaction::end(){
m_isEnding = true;
exec("END TRANSACTION");
m_isEnding = false;
m_isActive = false;
}

void transaction::commit(){
m_isEnding = true;
exec("COMMIT TRANSACTION");
m_isEnding = false;
m_isActive = false;
}

void transaction::rollback(){
m_isEnding = true;
exec("ROLLBACK TRANSACTION");
m_isEnding = false;
m_isActive = false;
}

Expand Down