我有一个与MySQL数据库对话的旋风式web服务。当有一段时间没有任何活动(我猜超过8个小时)时,我会得到以下错误,即重新启动web服务:
_mysql_exceptions.OperationalError:(2006年,“MySQL服务器已经消失”)
我见过重接,并在创建连接池时实现了这一点:
pool = adbapi.ConnectionPool("MySQLdb", host=self.host, user=self.user,
passwd=self.password, db=self.database, cursorclass=MySQLdb.cursors.DictCursor,
cp_reconnect=True)我本以为这会修复它,而且它似乎有一段时间了,但是现在我看到"MySQL服务器已经消失了“错误,在服务器上有一段时间没有活动之后,再次出现错误。
我读过超时,我想我可以用这种方式修复它,但是为什么cp_reconnect特性不适合我呢?我将adbapi文档解释为如果您指定了cp_reconnect参数,那么adbadpi将处理MySQL发送的错误并为您重新尝试查询。因此,基本上您不必在代码中直接处理错误。我是不是误会了?
发布于 2012-10-05 16:54:47
在对扭曲邮件列表进行了一些讨论并查看了扭曲的bug跟踪器之后,发现这是一个已知的bug,尚未修复。
下面是指向邮件列表讨论的链接。
下面是指向扭曲错误跟踪器问题的链接。
可能的解决办法:
其中,我可能会选择#1。
发布于 2012-10-02 08:00:55
几年前我一直在寻找解决这个问题的方法。我在网上找到的解决方案是子类ConnectionPool和重写_runInteraction,您可以在其中强制在特定的错误代码上重新连接。一个快速的谷歌搜索引导我到这个网站:http://www.gelens.org/2009/09/13/twisted-connectionpool-revisited/包含一个代码。我再也没想过要停止MySQL连接了。
发布于 2016-02-03 13:46:19
链接http://www.gelens.org/2009/09/13/twisted-connectionpool-revisited/不再工作了,甚至working版本也无法工作。这里是Twistar 1.3的摘录,它引用了相同的解决方案:
https://pypi.python.org/pypi/twistar
class ReconnectingMySQLConnectionPool(adbapi.ConnectionPool):
"""
This connection pool will reconnect if the server goes away. This idea was taken from:
http://www.gelens.org/2009/09/13/twisted-connectionpool-revisited/
"""
def _runInteraction(self, interaction, *args, **kw):
try:
return adbapi.ConnectionPool._runInteraction(self, interaction, *args, **kw)
except MySQLdb.OperationalError, e:
if e[0] not in (2006, 2013):
raise
log.err("Lost connection to MySQL, retrying operation. If no errors follow, retry was successful.")
conn = self.connections.get(self.threadID())
self.disconnect(conn)
return adbapi.ConnectionPool._runInteraction(self, interaction, *args, **kw)https://stackoverflow.com/questions/12677246
复制相似问题