Transaction destructor must never throw exception

- added a try/catch arround the exec("ROLLBACK")
- documentation was not right
This commit is contained in:
Sébastien Rombauts 2012-12-10 10:52:25 +01:00
parent e7e979433d
commit 9a5b993ae9
5 changed files with 16 additions and 9 deletions

View File

@ -50,7 +50,7 @@ Column::~Column(void) throw() // nothrow
if (SQLITE_OK != ret)
{
// Never throw an exception in a destructor
//std::cout << sqlite3_errmsg(mpSQLite);
//std::cout << sqlite3_errmsg(mpSQLite) << std::endl;
}
mpStmt = NULL;

View File

@ -37,7 +37,7 @@ Database::~Database(void) throw() // nothrow
if (SQLITE_OK != ret)
{
// Never throw an exception in a destructor
//std::cout << sqlite3_errmsg(mpSQLite);
//std::cout << sqlite3_errmsg(mpSQLite) << std::endl;
}
}

View File

@ -11,7 +11,6 @@
#include "Database.h"
#include "Column.h"
#include <iostream>
namespace SQLite
{
@ -47,7 +46,7 @@ Statement::~Statement(void) throw() // nothrow
if (SQLITE_OK != ret)
{
// Never throw an exception in a destructor
//std::cout << sqlite3_errmsg(mpSQLite);
//std::cout << sqlite3_errmsg(mpSQLite) << std::endl;
}
mpStmt = NULL;

View File

@ -1,6 +1,6 @@
/**
* @file Transaction.cpp
* @brief A prepared SQLite Transaction is a compiled SQL query ready to be executed.
* @brief A Transaction is way to group multiple SQL statements into an atomic secured operation.
*
* Copyright (c) 2012 Sebastien Rombauts (sebastien.rombauts@gmail.com)
*
@ -14,7 +14,7 @@
namespace SQLite
{
// Compile and register the SQL query for the provided SQLite Database Connection
//Begins the SQLite transaction
Transaction::Transaction(Database &aDatabase) : // throw(SQLite::Exception)
mDatabase(aDatabase),
mbCommited(false)
@ -22,12 +22,20 @@ Transaction::Transaction(Database &aDatabase) : // throw(SQLite::Exception)
mDatabase.exec("BEGIN");
}
// Finalize and unregister the SQL query from the SQLite Database Connection.
// Safely rollback the transaction if it has not been committed.
Transaction::~Transaction(void) throw() // nothrow
{
if (false == mbCommited)
{
mDatabase.exec("ROLLBACK");
try
{
mDatabase.exec("ROLLBACK");
}
catch (SQLite::Exception& e)
{
// Never throw an exception in a destructor
//std::cout << e.what() << std::endl;
}
}
}

View File

@ -57,7 +57,7 @@ private:
private:
Database& mDatabase; //!< Reference to the SQLite Database Connection
bool mbCommited; //!< True when the last executeStep() had no more row to fetch
bool mbCommited; //!< True when commit has been called
};