[英]Django - postgreSQL - new connection is made for every page
settings.py settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'tutorial',
'USER': 'postgres',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
'CONN_MAX_AGE': 10,
}
}
As I navigate to links in my website, a new connection is created. 当我导航到网站中的链接时,将创建一个新的连接。 For example, upon entering <
domain.com
>, a new connection is created: 例如,输入<
domain.com
>后,将创建一个新连接:
2018-08-10 06:22:15.301 CDT [3380] LOG: connection received: host=::1 port=56368
2018-08-10 06:22:15.306 CDT [3380] LOG: connection authorized: user=postgres database=tutorial
And upon navigating to one of the links, like < domain.com/some_page/
>, another new connection is created. 导航到其中一个链接(例如<
domain.com/some_page/
>)后,将创建另一个新连接。
2018-08-10 06:20:10.095 CDT [22932] LOG: connection received: host=::1 port=56181
2018-08-10 06:20:10.098 CDT [22932] LOG: connection authorized: user=postgres database=tutorial
When I have visited 10 different link within my domain and visit my 11th link, it finally reaches the maximum available connections, and disconnects the very first connection I established by visiting < domain.com
>: 当我访问了域中的10个不同链接并访问了第11个链接时,它最终达到了最大可用连接数,并断开了我通过访问<
domain.com
>建立的第一个连接:
2018-08-10 06:30:17.715 CDT [6884] LOG: disconnection: session time: 0:00:09.011 user=postgres database=tutorial host=::1 port=56368
Please note that port=56368
was the exact same port I opened upon visiting my < domain.com
> the first time. 请注意,
port=56368
与我第一次访问< domain.com
>时打开的端口完全相同。 Apparently a new connection is established every time I visit a new link, and if the current number of connections exceeds the max connections, the very first connection is disconnected in order to accept a new connection. 显然,每次我访问新链接时都会建立一个新连接,并且如果当前连接数超过了最大连接数,则将断开第一个连接以接受新连接。
But this is not I want. 但这不是我想要的。 I don't want a new connection to be created every time a user visits a link inside my domain.
我不希望每次用户访问我的域内的链接时都创建新的连接。 I want to use only one connection throughout my entire domain per user.
我想在每个用户的整个域中仅使用一个连接。 If a user opens a new tab, then I might want to establish a new connection, but not sure if this is the right way to do it.
如果用户打开一个新选项卡,那么我可能想建立一个新的连接,但是不确定这是否是正确的方法。
How can I achieve this? 我该如何实现? Ideally I would want to use connection pooling, but I don't really know how to implement it inside Django.
理想情况下,我想使用连接池,但是我真的不知道如何在Django中实现它。 I know how to establish connection pooling system using a pure
psycopg2
, but not sure if I want to do it that way, since Django might support such functionality. 我知道如何使用纯
psycopg2
建立连接池系统,但不确定是否要那样做,因为Django可能支持这种功能。 Or should I just write separate psycopg2
codes for connection pooling? 还是应该只为连接池编写单独的
psycopg2
代码?
Edit 编辑
I'm aware that CONN_MAX_AGE
specifies number of seconds that a request can be alive. 我知道
CONN_MAX_AGE
指定了请求可以存在的秒数。 But, the connection I used to load a page does not close after 10 seconds it was loaded. 但是,我用来加载页面的连接在加载10秒后没有关闭。 But instead, its behaving as if
CONN_MAX_AGE
is setting the maximum number of connections, and I do not know why. 但是,相反,它的行为就像
CONN_MAX_AGE
设置了最大连接数一样,我也不知道为什么。 The log file outputs disconnection:
message only if the number of connections exceeds CONN_MAX_AGE
. 仅当连接数超过
CONN_MAX_AGE
,日志文件才会输出disconnection:
消息。 IT DOES NOT outputs disconnection
message after 10 seconds a page is loaded. 加载页面10秒后,它不会输出
disconnection
消息。
I'm using Django 2.0.6
, psycopg2 2.7.5
, postgreSQL 10
我正在使用
Django 2.0.6
, psycopg2 2.7.5
, postgreSQL 10
'CONN_MAX_AGE' is the number of seconds your connection keeps alive. “ CONN_MAX_AGE”是您的连接保持活动的秒数。 Try removing that parameter from your db settings.
尝试从数据库设置中删除该参数。
Django inherently does not support connection pooling. Django本质上不支持连接池。 You can try take a connection from the pool and close a connection.
您可以尝试从池中建立连接并关闭连接。 But this is not actual connection pooling.
但这不是实际的连接池。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.