Reputation: 336
For each request, django is creating a new DB connection even after setting CONN_MAX_AGE variable in my Django DB configuration
My configurations -
python3.5/Django2.05/mysql-5.6
This is my DB configurations -
{'default': {
'CONN_MAX_AGE': 500,
'ENGINE': 'django.db.backends.mysql',
'HOST': 'localhost',
'NAME': DB_NAME,
'OPTIONS': {},
'PASSWORD': 'root',
'PORT': '3306',
'TEST': {'CHARSET': None, 'COLLATION': None, 'MIRROR': None,
'NAME': None},
'TIME_ZONE': None,
'USER': 'root'}}
According to the doc: Django Documentation, it should not create a new connection till the CONN_MAX_AGE expires.
I am checking by hitting a get request to custom view and checking connection object by adding this statement -
from django.db import connections
print (connections.all())
Log shows a new connection object, like this -
[INFO 2018-12-04 23:00:08,472 basehttp] "GET /health_check/ HTTP/1.1" 200 96
[<django.db.backends.mysql.base.DatabaseWrapper object at 0x7f2c5fb1a0b8>]
[INFO 2018-12-04 23:00:09,017 basehttp] "GET /health_check/ HTTP/1.1" 200 96
[<django.db.backends.mysql.base.DatabaseWrapper object at 0x7f2c5fa88d30>]
Another observation is -
By logging inside -
local/lib/python3.5/site-packages/django/db/backends/mysql/base.py
get_new_connection() is being called for every request, which according to me should not happen.
If this interpretation of logging/debugging by object name is wrong, then suggest how should I ensure that the DB conncetion doesn't get created on each request.
PS, I am new to django
Upvotes: 1
Views: 746
Reputation: 485
I totally agree with Kevin's answer. If you run the Django Development Server with nothreading argument, then you'll see the same connection object on connections.all()
.
python manage.py runserver --nothreading
Upvotes: 0