[odb-users] Exception handling and rollback()

Boris Kolpackov boris at codesynthesis.com
Thu Dec 14 08:40:29 EST 2017


Panayiotis Georgiou <ps.georgiou at gmail.com> writes:

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

If you catch it before the transaction's destructor is called (due to
stack unwinding).


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

I would move this ...

>     unsigned long id;
> 
>     for (unsigned short retryCount(0); ; retryCount++)
>     {
>         try
>         {
>             db = dbManager.getDatabase();
>             transaction.reset(db->begin());

... here:
              odb::transaction transaction (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;
> }



More information about the odb-users mailing list