#24810: Reopen database connection automatically when no transaction is active
-------------------------------------+-------------------------------------
Reporter: Aymeric Augustin | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Daniel Hahler):
Hugo,
you should be able to reproduce this when restarting PostgreSQL after
getting the cursor, i.e.:
{{{
from django.db import connection
cursor = connection.cursor()
__import__("time").sleep(5) # restart postgresql
cursor.execute("select 1")
}}}
This then fails with the following for me on Django 3.2.8:
{{{
Traceback (most recent call last):
File "…/django/db/backends/utils.py", line 82, in _execute
return self.cursor.execute(sql)
psycopg2.errors.AdminShutdown: terminating connection due to administrator
command
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "…/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False,
executor=self._execute)
File "…/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "…/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "…/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "…/django/db/backends/utils.py", line 82, in _execute
return self.cursor.execute(sql)
django.db.utils.OperationalError: terminating connection due to
administrator command
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
}}}
(Note that in this case the first exception is of type
{{{AdminShutdown}}}, whereas on Sentry I see {{{psycopg2.OperationalError:
server closed the connection unexpectedly}}} there.)
Just for reference: there exists https://github.com/jdelic/django-dbconn-
retry, but it has its (open) issues also.
--
Ticket URL: <https://code.djangoproject.com/ticket/24810#comment:13>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/067.63cf16f67d89fd6f0f00c257f06f2d7e%40djangoproject.com.