尝试重置与远程数据库的连接时出错。它不是默认数据库。看来connections.close只对默认的数据库配置产生影响。
我是否遗漏了某些内容,或者是否有办法重置与特定数据库的连接(非默认)?
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'
尝试输出
>>> 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')
>>>
修改
BaSS数据库是一个远程数据库,我只对所有表具有读访问权限。我不控制此服务器端的超时。
更新
在服务级别重新启动MySQl会重置此设置并允许连接通过。这仍然不是每8小时重启服务的优雅解决方案。寻求帮助。
答案 0 :(得分:2)
django.db.connection
指的是默认连接(基于尝试代码的输出)。如果要连接非默认数据库,请使用django.db.connections,将数据库名称指定为索引,即
from django.db import connections
connections['BaSS'].close()
也就是说,你不应该默认手动重置连接(django默认关闭end of each request处的连接),虽然我知道为什么长时间运行管理脚本/命令可能需要这样做长时间不活动。