Python django transaction.commit#u手动导致';MySQL服务器已经消失了';
有一个长期运行的过程,工作良好。在添加transaction.commit()后开始出现(2006年“MySQL服务器已消失”)错误,我在其中手动提交了事务 之前(效果很好): 之后:(夜间空闲8小时无需处理后获取错误) 注意:我需要像这样刷新它以避免得到过时的数据Python django transaction.commit#u手动导致';MySQL服务器已经消失了';,python,mysql,django,python-2.7,transactions,Python,Mysql,Django,Python 2.7,Transactions,有一个长期运行的过程,工作良好。在添加transaction.commit()后开始出现(2006年“MySQL服务器已消失”)错误,我在其中手动提交了事务 之前(效果很好): 之后:(夜间空闲8小时无需处理后获取错误) 注意:我需要像这样刷新它以避免得到过时的数据 flush_transaction() DBObject.objects.get(id = 1) 在哪里 据我所知,我正在切换到手动提交,但似乎我也失去了django的自动重新连接 除了在mysql端增加等待超时之外,还有什么好办
flush_transaction()
DBObject.objects.get(id = 1)
在哪里
据我所知,我正在切换到手动提交,但似乎我也失去了django的自动重新连接
除了在mysql端增加等待超时之外,还有什么好办法来处理这个问题吗?问题在于切换到提交手动模式。似乎没有正确关闭连接。阅读更多关于 解决此问题的方法是手动关闭数据库连接并重试查询 手动关闭我使用的连接
from django import db
db.close_connection()
希望这有帮助
@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()
from django import db
db.close_connection()