[odb-users] Understanding odb::recoverable exceptions

Javier Gutierrez javier.gutierrez at web.de
Fri May 8 13:19:56 EDT 2020


Hi Boris,

I am implementing the piece of code you propose in the section of the ODB
manual: Error Handling and Recovery
(https://www.codesynthesis.com/products/odb/doc/manual.xhtml#3.7)

I did my tests with MySQL.
It seems that when the database is not available, opening a new transaction
does not throw a odb::recoverable but rather a "Can't connect to MySQL
server on ..." (odb::exception?).
If the database connection is lost after opening the transaction, then a
odb::recoverable is thrown. In this case your piece of code attempts a retry
by running the transaction again. But as said, this does not throw a
odb::recoverable so it goes immediately out of the loop never reaching the
max_retries.

Am I missing something ?

I modified slightly the code as below which works for me, but I still wonder
if I am missing something...

const unsigned short max_retries = 5;
bool is_recoverable = false;

for (unsigned short retry_count (0); ; retry_count++)
{
  try
  {
    transaction t (db.begin ());
    is_recoverable = false;
    ...

    t.commit ();
    break;
  }
  catch (const odb::recoverable& e)
  {
    Is_recoverable = true;
    continue;
  }
  catch (const odb::exception& e)
  {
    If (is_recoverable)
    {
      if (retry_count > max_retries)
        throw;
      else
        continue;
    }
    else
      throw;
  }}

Thanks a lot.

Best regards,
Javier




More information about the odb-users mailing list