Django ORM 和 PostgreSQL 连接限制
我正在 Postgresql 8.1.21 上运行 Django 项目(使用 Django 1.1.1、Python2.5、psycopg2、Apache2 和 mod_wsgi 3.2)。我们最近遇到了这个可爱的错误:
OperationalError:FATAL:超出非超级用户的连接限制
我不是第一个遇到此问题的人。关于这个错误有很多讨论,特别是与 psycopg 相关的讨论,但其中大部分都集中在旧版本的 Django 上和/或提供涉及编辑 Django 本身代码的解决方案。我还没有找到关于如何解决 Django ORM(或 psycopg,在本例中真正负责的那个)留下开放的 Postgre 连接的问题的简洁解释。
只需在每个视图末尾添加 connection.close()
就能解决这个问题吗?更好的是,有人最终解决了这个问题并解决了这个错误吗?
编辑:我们后来将 Postgresql 的连接限制提高到 500 个;这防止了错误的出现,但取而代之的是过多的内存使用。
I'm running a Django project on Postgresql 8.1.21 (using Django 1.1.1, Python2.5, psycopg2, Apache2 with mod_wsgi 3.2). We've recently encountered this lovely error:
OperationalError: FATAL: connection limit exceeded for non-superusers
I'm not the first person to run up against this. There's a lot of discussion about this error, specifically with psycopg, but much of it centers on older versions of Django and/or offer solutions involving edits to code in Django itself. I've yet to find a succinct explanation of how to solve the problem of the Django ORM (or psycopg, whichever is really responsible, in this case) leaving open Postgre connections.
Will simply adding connection.close()
at the end of every view solve this problem? Better yet, has anyone conclusively solved this problem and kicked this error's ass?
Edit: we later upped Postgresql's limit to 500 connections; this prevented the error from cropping up, but replaced it with excessive memory usage.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是由其他原因引起的。例如,配置 Apache/mod_wsgi 的方式理论上它可以接受比数据库本身可以同时接受的并发请求更多的并发请求。您是否检查过您的 Apache/mod_wsgi 配置并将最大客户端限制与 PostgreSQL 的限制进行比较,以确保没有发生类似的事情。显然,这假设您已经设法在 Apache 中达到该限制,并且还取决于数据库连接池的设置方式。
This could be caused by other things. For example, configuring Apache/mod_wsgi in a way that theoretically it could accept more concurrent requests than what the database itself may be able to accept at the same time. Have you reviewed your Apache/mod_wsgi configuration and compared limit on maximum clients to that of PostgreSQL to make sure something like that hasn't been done. Obviously this presumes though that you have managed to reach that limit in Apache some how and also depends on how any database connection pooling is set up.