1
caoyue 2013-08-29 09:26:24 +08:00
可能是长时间没有操作请求导致连接被关闭了
设置 MySQL 的 interactove_timeout 值 或者设置 sqlalchemy 的 pool_recycle 值 |
3
wzxjohn 2013-08-29 10:15:21 +08:00
SAE的mysql相当不稳定,这个问题出了很久了。淡定。
|
4
hit9 2013-08-29 11:27:30 +08:00
```
try: cls.conn.ping() # ping to test if the connection is working except MySQLdb.OperationalError: cls.connect() ``` 每次连接试下ping。 详见: https://github.com/hit9/CURD.py/blob/master/CURD.py#L134 |
5
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() |