luckyduck
2013-08-29 11:47:04 +08:00
这个问题在BAE上同样会有的,并不是不稳定,而是故意设计的这样一种机制,来防止MySQL产生大量的空闲链接。你可以在每次数据库操作前判断一下当前的空闲时间,如果超过30秒就重连。Tornado分出来的torndb就支持这种操作,可以设置max_idle_time。
def _ensure_connected(self):
# Mysql by default closes client connections that are idle for
# 8 hours, but the client library does not report this fact until
# you try to perform a query and it fails. Protect against this
# case by preemptively closing and reopening the connection
# if it has been idle for too long (7 hours by default).
if (self._db is None or
(time.time() - self._last_use_time > self.max_idle_time)):
self.reconnect()
self._last_use_time = time.time()