jeudi 27 août 2015

SQLITE Exception Handling: Why must I reset the statement twice after foreign key violation?

I'm using SQLITE 3.8.7 and am trying to create a custom exception handling class whenever a statement fails a constraint such as a foreign key constraint violation or if a sqlite3_step statement returns SQLITE_MISUSE.

In my destructor, I perform 1) a rollback; 2) cleanup; 3) log the exception.

~SQLException()
{
    _db->Execute("ROLLBACK");
    try
    {
       _db->ResetStatement();
    }
    catch (sql_exception anotherException)
    {
       _db->ResetStatement();
    }

    LOG_ERROR("sql_exception: %d %s\n", _e.code, _e.message.c_str());
}

Execute() is just a wrapper around sqlite3_exec while ResetStatement() is a wrapper around sqlite3_reset().

I wrote a unit test that triggered a foreign key constraint violation (which triggers this cleanup code), and then try and perform additional inserts like nothing happened. The rollback works fine but the first reset throws the same foreign key constrain violation exception. It is only when I call ResetStatement() a second time do things get cleaned up and I'm able to continue with additional inserts, etc.

Why does the reset statement need to be called twice?

Thanks.

Aucun commentaire:

Enregistrer un commentaire