Forums

OperationalError at No exception message supplied

Site working but sometimes raise error like this;

OperationalError at /
No exception message supplied
Request Method: GET
Request URL:    http://hostdjango.pythonanywhere.com/
Django Version: 1.6.5
Exception Type: OperationalError
Exception Location: /usr/local/lib/python3.4/dist-packages/mysql/connector/connection.py in cursor, line 1328
Python Executable:  /usr/local/bin/uwsgi
Python Version: 3.4.1
Python Path:    
['/var/www',
 '.',
 '',
 '/home/hostdjango/.local/lib/python3.4/site-packages',
 '/usr/local/lib/python3.4/dist-packages/setuptools-5.4.2-py3.4.egg',
 '/usr/local/lib/python3.4/dist-packages/matplotlib-1.3.1-py3.4-linux-x86_64.egg',
 '/usr/local/lib/python3.4/dist-packages/nose-1.3.3-py3.4.egg',
 '/usr/local/lib/python3.4/dist-packages/certifi-14.05.14-py3.4.egg',
 '/var/www',
 '/usr/lib/python3.4',
 '/usr/lib/python3.4/plat-x86_64-linux-gnu',
 '/usr/lib/python3.4/lib-dynload',
 '/usr/local/lib/python3.4/dist-packages',
 '/usr/lib/python3/dist-packages',
 '/home/hostdjango/wen']
Server time:    Thu, 21 Aug 2014 19:36:42 +0400
Traceback Switch to copy-and-paste view

/usr/local/lib/python3.4/dist-packages/django/core/handlers/base.py in get_response
                    response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
 Local vars
/home/hostdjango/wen/forum/views.py in forum
        pages = current_page.page(page_number) ...
 Local vars
/usr/local/lib/python3.4/dist-packages/django/core/paginator.py in page
        number = self.validate_number(number) ...
 Local vars
/usr/local/lib/python3.4/dist-packages/django/core/paginator.py in validate_number
        if number > self.num_pages: ...
 Local vars
/usr/local/lib/python3.4/dist-packages/django/core/paginator.py in _get_num_pages
            if self.count == 0 and not self.allow_empty_first_page: ...
 Local vars
/usr/local/lib/python3.4/dist-packages/django/core/paginator.py in _get_count
                self._count = self.object_list.count() ...
 Local vars
/usr/local/lib/python3.4/dist-packages/django/db/models/query.py in count
        return self.query.get_count(using=self.db) ...
 Local vars
/usr/local/lib/python3.4/dist-packages/django/db/models/sql/query.py in get_count
        number = obj.get_aggregation(using=using)[None] ...
 Local vars
/usr/local/lib/python3.4/dist-packages/django/db/models/sql/query.py in get_aggregation
        result = query.get_compiler(using).execute_sql(SINGLE) ...
 Local vars
/usr/local/lib/python3.4/dist-packages/django/db/models/sql/compiler.py in execute_sql
        cursor = self.connection.cursor() ...
 Local vars
/usr/local/lib/python3.4/dist-packages/django/db/backends/__init__.py in cursor
            cursor = self.make_debug_cursor(self._cursor()) ...
 Local vars
/usr/local/lib/python3.4/dist-packages/mysql/connector/django/base.py in _cursor
            return super(DatabaseWrapper, self)._cursor() ...
 Local vars
/usr/local/lib/python3.4/dist-packages/django/db/backends/__init__.py in _cursor
            return self.create_cursor() ...
 Local vars
/usr/local/lib/python3.4/dist-packages/django/db/utils.py in __exit__
                six.reraise(dj_exc_type, dj_exc_value, traceback) ...
 Local vars
/usr/local/lib/python3.4/dist-packages/django/utils/six.py in reraise
            raise value.with_traceback(tb) ...
 Local vars
/usr/local/lib/python3.4/dist-packages/django/db/backends/__init__.py in _cursor
            return self.create_cursor() ...
 Local vars
/usr/local/lib/python3.4/dist-packages/mysql/connector/django/base.py in create_cursor
        cursor = self.connection.cursor() ...
 Local vars
/usr/local/lib/python3.4/dist-packages/mysql/connector/connection.py in cursor
            raise errors.OperationalError("MySQL Connection not available.") ...
 Local vars

Django 1.6 needs to be told how long it can expect a database connection to last. On PythonAnywhere we terminate unused connections after 5min, so you need to include

'CONN_MAX_AGE': 299

in your database configuration dict in settings.py. See the Django docs

glenn, thank you for helping

glenn, i included 'CONN_MAX_AGE': 299 and reload apache many time, but this error appears frequently still.

That's puzzling. How frequently are you seeing the problem?

harry, this usually occurs when updating any page of the site. I tested my site in Page Speed - Google Developers and there was also error. This may be due to this code?

if( window.location.hash ) { // just in case there is no hash
        $(document.body).animate({
            'scrollTop':   $( window.location.hash ).offset().top
        }, 2000);
        }

I don't think that code would cause errors, no. We'll see if we can find anything in our logs...

can be put another CONN_MAX_AGE? for example CONN_MAX_AGE=3600

Well, like Glenn said elsewhere, anything greater than 300 is going to cause problem, because mysql will disconnect you from its end after 5 minutes. I would recommend a setting of about 250.

I set 250, but error stil. I do not know what to do. Case in hosting or code?

I've bumped the server's global max_connections variable -- it didn't look like we were anywhere near the limit, but right now isn't our most busy time of day. Can you give it another try?

You should note that django 1.6 with python 3.4 isn't supported. Are you having deliberately the need for this version combination?

From the django 1.6 release notes:

Python 3.4 is not supported, but support will be added in Django 1.7

Either go back to python 3.3 (you probably won't see a difference) or advance to django 1.7 (in development). If the problem persists you have the additional possibility of filing a bug report.

Cheers

Hi, am getting the same error every time there is no activity on the website after a while and navigate to another page or something. Is there any way to reconnect (refresh - database connectivity) to the database after time out ?

That's what we thought CONN_MAX_AGE was supposed to fix. You should be aware that the Django and MySQL combination don't have very good support for Python 3. The standard MySQLdb library (which is the only one that Django fully supports) is not available (except as an incomplete alpha release) for Python 3.