服务器 A 是 tornado,去连服务器 B 上的 PostgreSQL 数据库,刚开始没啥问题。
可是时间久了服务器 A 和服务器 B 的数据库连接就断了。
大佬们有遇到过这种情况吗?
附个错误提示:
Traceback (most recent call last):
File "/home/xxx/.local/lib/python3.5/site-packages/peewee.py", line 3756, in execute_sql
cursor = self.get_cursor()
File "/home/xxx/.local/lib/python3.5/site-packages/peewee.py", line 3704, in get_cursor
return self.get_conn().cursor()
psycopg2.InterfaceError: connection already closed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/xxx/.local/lib/python3.5/site-packages/tornado/web.py", line 1488, in _execute
result = self.prepare()
File "/home/xxx/cc_backend/view/__init__.py", line 217, in prepare
if not self.current_user():
File "/home/xxx/cc_backend/view/__init__.py", line 192, in current_user
return User.get_by_key(key)
File "/home/xxx/cc_backend/model/user.py", line 102, in get_by_key
return cls.get(cls.key == key)
File "/home/xxx/.local/lib/python3.5/site-packages/peewee.py", line 4916, in get
return sq.get()
File "/home/xxx/.local/lib/python3.5/site-packages/peewee.py", line 3171, in get
return next(clone.execute())
File "/home/xxx/.local/lib/python3.5/site-packages/peewee.py", line 3223, in execute
self._qr = ResultWrapper(model_class, self._execute(), query_meta)
File "/home/xxx/.local/lib/python3.5/site-packages/peewee.py", line 2902, in _execute
return self.database.execute_sql(sql, params, self.require_commit)
File "/home/xxx/.local/lib/python3.5/site-packages/peewee.py", line 3765, in execute_sql
self.commit()
File "/home/xxx/.local/lib/python3.5/site-packages/peewee.py", line 3588, in __exit__
reraise(new_type, new_type(*exc_args), traceback)
File "/home/xxx/.local/lib/python3.5/site-packages/peewee.py", line 135, in reraise
raise value.with_traceback(tb)
File "/home/xxx/.local/lib/python3.5/site-packages/peewee.py", line 3756, in execute_sql
cursor = self.get_cursor()
File "/home/xxx/.local/lib/python3.5/site-packages/peewee.py", line 3704, in get_cursor
return self.get_conn().cursor()
peewee.InterfaceError: connection already closed
1
firefox12 2017-05-07 23:43:36 +08:00 via iPad 1
本来就是这样,所以有 mysql_ping 这样的设计
|
2
SlipStupig 2017-05-07 23:50:01 +08:00 1
太正常了,数据库会把一些闲置的连接主动杀掉(多长时间没有活动),可以通过 Keepalive 来连接,linux 可以通过一下参数来进行配置:
net.ipv4.tcp_keepalive_time=90 net.ipv4.tcp_keepalive_intvl=15 net.ipv4.tcp_keepalive_probes=2 但是依然会断开,postgresql 可以定时发送 NOTIFY 来保持连接 |
3
msg7086 2017-05-08 02:47:06 +08:00 1
数据库本身也有闲置断线设定,没记错的话 MySQL 是 3 小时,需要改配置延长时间。
|
4
woshixiaohao1982 2017-05-08 07:58:26 +08:00 1
难倒不使用线程池来维护连接对象?
|
5
liangmishi OP @firefox12 原来如此,谢谢
@SlipStupig 增加连接时常的配置有点治标不治本的意思,我先谷歌看看 NOTIFY,谢谢 @msg7086 想说有没有一种断了再重连的方法,或者使用前尝试重连就可以解决了 @woshixiaohao1982 好主意,我去试试,谢谢 |
6
QQ2171775959 2017-05-08 09:06:07 +08:00 1
这个要看你怎么搭建了,服务器之间有没有组内网,设置的闲置时间有多长,有没有设置定时发送一条指令来激活这个闲置的连接。
|
7
lostsquirrelX 2017-05-08 09:21:57 +08:00 1
|
8
liangmishi OP |