Tzach
Tzach

Reputation: 13376

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.

Upvotes: 6

Views: 2113

Answers (1)

Tzach
Tzach

Reputation: 13376

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.

Upvotes: 6

Related Questions