2

有一个运行良好的长时间运行过程。在我手动提交事务的地方添加 transaction.commit() 后开始出现 (2006 'MySQL server has gone away') 错误。

之前(效果很好):

DBObject.objects.get(id = 1)

之后:(在晚上闲置 8 小时后得到错误)

注意:我需要像这样刷新它以避免获取过时的数据。

flush_transaction()
DBObject.objects.get(id = 1)

在哪里

@transaction.commit_manually
def flush_transaction():
    """
    Flush the current transaction so we don't read stale data

    Use in long running processes to make sure fresh data is read from
    the database.  This is a problem with MySQL and the default
    transaction mode.  You can fix it by setting
    "transaction-isolation = READ-COMMITTED" in my.cnf or by calling
    this function at the appropriate moment
    """
    transaction.commit()

据我了解,我正在切换到 commit_manually 但似乎我也失去了 django 的自动重新连接。

除了在 mysql 端增加 wait_timeout 之外,有没有好的方法来处理这个?

4

1 回答 1

1

问题在于切换到 commit_manual 模式。似乎没有正确关闭连接。阅读更多关于django 和数据库连接的信息。

解决问题的是手动关闭数据库连接并再次尝试查询。

手动关闭我使用的连接

from django import db
db.close_connection()

希望这有帮助

于 2013-09-04T00:22:02.417 回答