Skip to content

Commit

Permalink
Ensure crash in commit/rollback does not try twice
Browse files Browse the repository at this point in the history
It is possible that if during the rollback or commit command
that there may be a disk error that will cause SQLite to return
an error.  When that happens we must not attempt to rollback again.

This was seen when running multi-threaded and a bug in some code
resulted in the SQLite file descripter being closed in the middle of
a transaction.
  • Loading branch information
Russell Smith committed Feb 18, 2019
1 parent f0aaa34 commit 96d2e7c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
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

0 comments on commit 96d2e7c

Please sign in to comment.