﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
26282	db.connection.is_usable throws when no connection exists yet	Pierre Pattard	nobody	"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."	Bug	closed	Database layer (models, ORM)	1.9	Normal	wontfix			Unreviewed	0	0	0	0	0	0
