【问题标题】:django transaction.commit_manually leads to 'MySQL server has gone away'django transaction.commit_manually 导致“MySQL 服务器已消失”
【发布时间】:2013-09-07 04:12:37
【问题描述】:

有一个运行良好的长时间运行的进程。在我手动提交事务的地方添加 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,还有什么好的处理方法吗?

【问题讨论】:

    标签: python mysql django python-2.7 transactions


    【解决方案1】:

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

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

    手动关闭我使用的连接

    from django import db
    db.close_connection()
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2017-04-02
      • 2013-11-28
      • 2010-12-19
      • 2018-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-13
      • 2012-05-25
      相关资源
      最近更新 更多