V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
liangmishi
V2EX  ›  Python

不在同一服务器上的数据库连接起来久了会断开,大佬们有遇到过吗?

  •  
  •   liangmishi · 2017-05-07 23:40:49 +08:00 · 5084 次点击
    这是一个创建于 2517 天前的主题,其中的信息可能已经有所发展或是发生改变。

    服务器 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 条附言  ·  2017-05-08 22:16:02 +08:00
    最后解决办法就是:每次使用完后就显式地关闭数据库。
    关闭后,连接就会放回进程池,等到要用的时候 peewee 会自动去取。
    8 条回复    2017-05-08 22:14:10 +08:00
    firefox12
        1
    firefox12  
       2017-05-07 23:43:36 +08:00 via iPad   ❤️ 1
    本来就是这样,所以有 mysql_ping 这样的设计
    SlipStupig
        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 来保持连接
    msg7086
        3
    msg7086  
       2017-05-08 02:47:06 +08:00   ❤️ 1
    数据库本身也有闲置断线设定,没记错的话 MySQL 是 3 小时,需要改配置延长时间。
    woshixiaohao1982
        4
    woshixiaohao1982  
       2017-05-08 07:58:26 +08:00   ❤️ 1
    难倒不使用线程池来维护连接对象?
    liangmishi
        5
    liangmishi  
    OP
       2017-05-08 08:48:54 +08:00 via Android
    @firefox12 原来如此,谢谢

    @SlipStupig 增加连接时常的配置有点治标不治本的意思,我先谷歌看看 NOTIFY,谢谢

    @msg7086 想说有没有一种断了再重连的方法,或者使用前尝试重连就可以解决了

    @woshixiaohao1982 好主意,我去试试,谢谢
    QQ2171775959
        6
    QQ2171775959  
       2017-05-08 09:06:07 +08:00   ❤️ 1
    这个要看你怎么搭建了,服务器之间有没有组内网,设置的闲置时间有多长,有没有设置定时发送一条指令来激活这个闲置的连接。
    liangmishi
        8
    liangmishi  
    OP
       2017-05-08 22:14:10 +08:00
    @QQ2171775959 感谢,学习了

    @lostsquirrelX 感谢,学习了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1711 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 16:41 · PVG 00:41 · LAX 09:41 · JFK 12:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.