2

I am getting errors when trying to reset the connection to a remote database. It is not the default database. It seems that connections.close only has a affect on the default Database configuration.

Am I missing something or is there a way to reset the connection to a specific database (not default)?

Django 1.7 Python 2.7.9

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'DefaultDB',
        'USER': 'xxxxx',
        'PASSWORD': 'xxxxxx',
        'HOST': '',
        'PORT': '',
    },
    'BaSS': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'TheDB',
        'USER': 'xxxx',
        'PASSWORD': 'xxxxx',
        'HOST': '10.x.x.x',
        'PORT': 'xxxx',
    }
}

RemoteDB_models.py:

class RemoteDBRouters(models.Model):
    hostname = models.CharField(max_length=63)
    role = models.CharField(max_length=20, blank=True)
    infodate = models.DateField()
    infotime = models.TimeField()
    serialnum = models.CharField(max_length=20)
    iostype = models.CharField(max_length=50)
    iosver = models.CharField(max_length=15)
    imagefilename = models.CharField(max_length=256, blank=True)
    model = models.CharField(max_length=20)
    cfgver = models.DecimalField(max_digits=3, decimal_places=2, blank=True, null=True)
    filename = models.CharField(max_length=256)
    cfghostname = models.CharField(max_length=63, blank=True)
    medium = models.CharField(max_length=20, blank=True)
    dmtype = models.CharField(max_length=20, blank=True)
    t1size = models.IntegerField(blank=True, null=True)
    spid1 = models.CharField(max_length=20, blank=True)
    spid2 = models.CharField(max_length=20, blank=True)
    mrtglink = models.CharField(max_length=256, blank=True)
    loopbackip = models.CharField(max_length=15, blank=True)
    tunnelip = models.CharField(max_length=15, blank=True)
    managementip = models.CharField(max_length=15, blank=True)
    snmplocation = models.CharField(max_length=200, blank=True)
    uid = models.IntegerField(primary_key=True)
    tun8inet = models.CharField(max_length=31, blank=True)
    tun9inet = models.CharField(max_length=31, blank=True)
    snmpcontact = models.CharField(max_length=300, blank=True)

    class Meta:
        managed = False
        db_table = 'routers'

class RoutersLastupdate(models.Model):
    uid = models.ForeignKey(BaSSRouters)
    hostname = models.ForeignKey(BaSSRouters)
    infodate = models.DateField()
    infotime = models.TimeField()
    deconverted = models.CharField(max_length=1)

    class Meta:
        managed = False
        db_table = 'routers_lastupdate'

Ouputput of attempts

>>> from django.db import connection; connection.close() 
>>> from thunderdome.RemoteDB_models import RoutersLastupdate, RemoteDBRouters
>>> from thunderdome.models import InventoryWarehouse as Inventory_Warehouse
>>> serial = "0123456789"                                        
>>> Inventory_Warehouse.objects.filter(serial_numb=serial).first()
<InventoryWarehouse: InventoryWarehouse object>
>>>>
>>>>
>>> RemoteDBRouters.objects.using("BaSS").filter(hostname="router1").last()       
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 520, in last
    return qs[0]
  File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 177, in __getitem__
    return list(qs)[0]
  File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 141, in __iter__
    self._fetch_all()
  File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 966, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 265, in iterator
    for row in compiler.results_iter():
  File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 700, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 129, in execute
    return self.cursor.execute(query, args)
  File "/usr/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "/usr/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
OperationalError: (2006, 'MySQL server has gone away')
>>> 

Edit:

The BaSS database is a remote database that I only have read access to all tables. I do not control the timeout on the serverside for this DB.

Update

Restarting MySQl at the service level does reset this and allow connections through. Still this is not a elegant solution to restart the service every 8 hours. Looking for help still.

1 Answer 1

2

django.db.connection refers to the default connection (based on the output of your attempts code). If you want the connection for a non default database, use django.db.connections, specifying the database name as the index i.e.

from django.db import connections
connections['BaSS'].close()

That said, you shouldn't have to reset the connection manually by default (django by default closes a connection at the end of each request), though I see why this might be needed for long running admin scripts/commands with long periods of inactivity.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.