[odb-users] Exception handling and rollback()

Panayiotis Georgiou ps.georgiou at gmail.com
Wed Dec 13 10:49:30 EST 2017


Hello

According to ODB's manual, as well as this post [1], ODB is smart enough to
rollback any non-finalised transactions.

Is there a situation where ODB may throw an exception in which we need to
manually call rollback ()?

Below I have included a simple example of how I catch exceptions in my code
(same approach is followed for calls to update(), load(), delete()).

Is this a reasonable implementation and if we had to call rollback after an
exception was thrown, what is the best way to do that?

Thanks,
Panayiotis.


//==========
unsigned long createRecord(T& newRecord) const
{
    odb::database* db;
    odb::transaction transaction;
    unsigned long id;

    for (unsigned short retryCount(0); ; retryCount++)
    {
        try
        {
            db = dbManager.getDatabase();
            transaction.reset(db->begin());
            id = db->persist(newRecord);
            transaction.commit();
        }
        catch (const odb::recoverable& e)
        {
            if (retryCount > maxRetries)
                throw kException("Retry limit exceeded: " +  e.what());
            else
                continue;
        }
        catch (const odb::exception& e)
        {
            throw kException("ODB critical exception: " +  e.what());
        }
    }

    return id;
}
//==========

[1] https://www.codesynthesis.com/pipermail/odb-users/2013-May/001296.html


More information about the odb-users mailing list