librelist archives

« back to archive

Exception Handling & MySQL Connections

Exception Handling & MySQL Connections

From:
Michael Montero
Date:
2014-04-07 @ 19:11
Hi all!  Hoping I can get some help on this issue.  Occasionally I see this
in my uWSGI log file:

    raise errors.OperationalError("MySQL Connection not available.")


I suspect this is happening because fatal errors are occurring that aren't
properly shutting down the MySQL connection - closing the open cursor and
closing the connection.

I'm using Flask-Restful and wondering how I might create a handler that is
called if ANY uncaught exception is thrown so that I can shutdown the
connection properly and re-throw the exception.

I'm using python3, mysql-connector and Flask/Flask-Restful.

Any ideas would be greatly appreciated.

Thanks,

Mike

Re: [flask] Exception Handling & MySQL Connections

From:
Kevin Houlihan
Date:
2014-04-08 @ 10:21
Hi Mike,

Unhandled exceptions should already be being dealt with by the Api class,
so one option might be to subclass that and override the error handler:

from flask.ext.restful import Api as BaseApi

class Api(BaseApi):

    def handle_error(self, e):
        response = super(Api, self).handle_error(e)
        # Do your stuff.
        return response

Regards,
Kevin


On 7 April 2014 20:11, Michael Montero <mike@resy.com> wrote:

> Hi all!  Hoping I can get some help on this issue.  Occasionally I see
> this in my uWSGI log file:
>
>      raise errors.OperationalError("MySQL Connection not available.")
>
>
> I suspect this is happening because fatal errors are occurring that aren't
> properly shutting down the MySQL connection - closing the open cursor and
> closing the connection.
>
> I'm using Flask-Restful and wondering how I might create a handler that is
> called if ANY uncaught exception is thrown so that I can shutdown the
> connection properly and re-throw the exception.
>
> I'm using python3, mysql-connector and Flask/Flask-Restful.
>
> Any ideas would be greatly appreciated.
>
> Thanks,
>
> Mike
>