Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force django to restart a database connection from the shell?

Tags:

I have a django project on a Digital Ocean server. From my local machine, I connect to the database through ssh:

ssh -L 63333:localhost:5432 [email protected] 

And change my local settings.py as:

DATABASES = {     'default': {         'ENGINE': 'django.db.backends.postgresql_psycopg2',         'NAME': '',         'USER': '',         'PASSWORD': '',         'HOST': 'localhost',         'PORT': 63333     } } 

And on a Jupyter QtConsole on my local machine, I follow these steps for setup:

import os import django os.chdir('/path/to/my/project') os.environ['DJANGO_SETTINGS_MODULE'] = 'my_project.settings' django.setup() 

It all works fine until my ssh connection is broken by some reason. First I get this error (on a line that queries the database, say, my_model.objects.first()):

DatabaseErrorTraceback (most recent call last) /home/ayhan/anaconda3/lib/python3.6/site-packages/django/db/backends/utils.py in _execute(self, sql, params, *ignored_wrapper_args)      84             else: ---> 85                 return self.cursor.execute(sql, params)      86   DatabaseError: server closed the connection unexpectedly     This probably means the server terminated abnormally     before or while processing the request. 

When I reconnect through ssh, I keep getting the following error:

InterfaceErrorTraceback (most recent call last) /home/ayhan/anaconda3/lib/python3.6/site-packages/django/db/backends/base/base.py in _cursor(self, name)     233         with self.wrap_database_errors: --> 234             return self._prepare_cursor(self.create_cursor(name))     235   /home/ayhan/anaconda3/lib/python3.6/site-packages/django/db/backends/postgresql/base.py in create_cursor(self, name)     211         else: --> 212             cursor = self.connection.cursor()     213         cursor.tzinfo_factory = utc_tzinfo_factory if settings.USE_TZ else None  InterfaceError: connection already closed 

The error only goes away if I restart the IPython kernel. I tried executing the same steps for the setup. I also tried from importlib import reload; reload(django) and then doing the setup again but it gives the same error no matter what.

Sometimes this happens while I have variables that I don't want to lose in the memory so I want to avoid restarting the kernel. Is there a way to recreate the database connection without it? I am using Python 3.6 and django 2.0.0.

like image 752
ayhan Avatar asked Jan 18 '18 20:01

ayhan


People also ask

How does Django handle database connections?

Connection managementDjango opens a connection to the database when it first makes a database query. It keeps this connection open and reuses it in subsequent requests. Django closes the connection once it exceeds the maximum age defined by CONN_MAX_AGE or when it isn't usable any longer.


1 Answers

You can use something like below

from django.db import connections conn = connections['default'] conn.connect() 

or

from django.db import connection connection.connect() 
like image 143
Tarun Lalwani Avatar answered Oct 01 '22 01:10

Tarun Lalwani