Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot catch MySQLdb.OperationalError on App Engine

I have a Django app running on Google App Engine. From time to time the DB raises OperationalError which is normal, however my code, although using try..except, is not catching the exception (I need this for retry purposes).

Here is my code:

from MySQLdb import OperationalError, DatabaseError

DB_RETRY_EXCEPTIONS = (
    OperationalError,
    DatabaseError,
)

class MyClassView(rest_framework.views.APIView):
    @retry(DB_RETRY_EXCEPTIONS, tries=5, delay=5, logger=logger)
    def dispatch(self, *args, **kwargs):
        try:
            return super(MyClassView, self).dispatch(*args, **kwargs)
        except DB_RETRY_EXCEPTIONS as exp:
            logger.warning("Got %s", exp)
            raise

In the exception stack trace, I can see that the flow is going through this piece of code, but no warnings.

Here is the last lines of the stack trace:

File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/MySQLdb-1.2.4b4/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/MySQLdb-1.2.4b4/MySQLdb/connections.py", line 190, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2013, "Lost connection to MySQL server at 'reading initial communication packet', system error: 38")

Any help is appreciated.

like image 790
Tzach Avatar asked Dec 21 '14 07:12

Tzach


1 Answers

It turned out that there is another OperationalError at django.db.utils.OperationalError and that's was the one that was raised. The solution was to catch also this exception.

like image 172
Tzach Avatar answered Oct 07 '22 04:10

Tzach