#26282: db.connection.is_usable throws when no connection exists yet
----------------------------------------------+--------------------
Reporter: ppattard | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.9
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
Hello,
I am running a Celery long running process which periodically loses the
MySQL connection. I am therefore trying to use the is_usable() method to
ensure the database connection is usable or reset it. This method
perfectly fits my needs as it simply returns True or False.
However, the first time I call it, as this is before there has been any
connection physically opened yet (I have not made any use of any model
yet), the connection does not exists and the method throws. It seems a bit
clumsy because the django.db.connection itself exists, it is just the
backend connection that has not been established yet.
Example:
{{{
import django
django.setup()
if django.db.connection.is_usable():
print 'Usable!'
}}}
Throws the following:
{{{
Traceback (most recent call last):
File "bug.py", line 4, in <module>
if not django.db.connection.is_usable():
File "C:\Python27\lib\site-packages\django\db\backends\mysql\base.py",
line 351, in is_usable
self.connection.ping()
AttributeError: 'NoneType' object has no attribute 'ping'
}}}
May I suggest that in the MySQL backend (I have not tested with other
backends), you check that the connections is not None before trying to
invoke ping() on it ?
So something like:
{{{
def is_usable(self):
if not self.connection:
return False
try:
self.connection.ping()
except Database.Error:
return False
else:
return True
}}}
Thanks!
Pierre -- a very happy and enthusiastic Django user.
--
Ticket URL: <https://code.djangoproject.com/ticket/26282>
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 post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/051.59da77febdf351a5e44ea50eb4a068b0%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.