有一个运行良好的长时间运行过程。在我手动提交事务的地方添加 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 之外,有没有好的方法来处理这个?