【问题标题】:Cannot catch MySQLdb.OperationalError on App Engine无法在 App Engine 上捕获 MySQLdb.OperationalError
【发布时间】:2015-02-19 15:48:28
【问题描述】:

我有一个在 Google App Engine 上运行的 Django 应用。数据库不时引发OperationalError,这是正常的,但是我的代码虽然使用了try..except,但没有捕获异常(我需要这个来重试)。

这是我的代码:

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

在异常堆栈跟踪中,我可以看到流程正在通过这段代码,但没有警告。

这是堆栈跟踪的最后几行:

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")

感谢任何帮助。

【问题讨论】:

    标签: python mysql django google-app-engine mysql-python


    【解决方案1】:

    原来django.db.utils.OperationalError 上还有另一个OperationalError,这就是提出的那个。解决方案是也捕获此异常。

    【讨论】:

      猜你喜欢
      • 2012-02-11
      • 1970-01-01
      • 2020-01-07
      • 2018-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-14
      • 2019-09-03
      相关资源
      最近更新 更多