Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/Django 1.5 DatabaseWrapper thread error

Throwing the following DatabaseError in Django 1.5.1 (and 1.5.0) and mysql when I runserver and attempt to load a local version of the web app:

DatabaseError at /

DatabaseWrapper objects created in a thread can only be used in that same thread. The object with alias 'default' was created in thread id 4365488128 and this is thread id 140735183980896.

The exception location is as follows:

/Users/USERNAME/.envs/PROJECT_NAME/lib/python2.7/site-packages/django/db/backends/init.py in validate_thread_sharing, line 154

This happens only on the first page load. When I hit refresh the the web app loads as expected. The error returns when I alter Django/python code and the local server needs to re-validate. When that happens the first load once again throws the DatabaseError and subsequent loads once again do not.

This issue is only happening on my Django 1.5 projects. I've uninstalled mysql and cleared the mysql binary files located in /usr/local/var/mysql. A fresh install of mysql has not solved this issue.

A similar DatabaseError is discussed here but I'm not able to follow their solution:

https://code.djangoproject.com/ticket/17998

I'm at a loss here, any ideas or expertise would be appreciated. Thank you.

like image 676
daroo Avatar asked Apr 25 '13 17:04

daroo


2 Answers

I solved the issue by monkey patching before imports of django.*:

import eventlet
eventlet.monkey_patch()

You understood than I use threads here.

like image 89
swann Avatar answered Oct 19 '22 14:10

swann


This is for 1.4, but the cause of the problem may be the same as yours:

https://bitbucket.org/akoha/django-digest/issue/10/conflict-with-global-databasewrapper

Two possible solutions in that post, though both appear to be workarounds:

"I have been able to fix the problem by commenting out close_connection in db.py"

"One fix that will definitely work is to replace MultiDb/get_default_db()/self.db by django.db.connection and django.db.tranasction as documented at: https://docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-sql-directly"

like image 24
trpt4him Avatar answered Oct 19 '22 13:10

trpt4him