I have been running an Django 1.5.1 app under Apache for some time without issue. Today, without any changes to the server I started seeing 500 Errors caused by an Exception in DatabaseWrapper relating to threading, I have no threading behavior in my code so this confuses me. The error affects all routes, although intermittently, roughly every 10th access or so hit this problem. It seems to trigger on first contact with the Database (Postgres 9.2 running on another machine)
Some tracebacks don't touch any of my own code. I restarted Apache and it made no difference, then I stopped Apache completely and started it again, now I am no longer seeing the error. However i'd like to understand why this happened and prevent it from happening again.
Traceback (most recent call last):
File "/home/bats/.virtualenvs/bats/lib/python2.7/site-packages/tastypie/resources.py", line 217, in wrapper
response = callback(request, *args, **kwargs)
File "/home/bats/.virtualenvs/bats/lib/python2.7/site-packages/tastypie/resources.py", line 468, in dispatch_detail
return self.dispatch('detail', request, **kwargs)
File "/home/bats/.virtualenvs/bats/lib/python2.7/site-packages/tastypie/resources.py", line 486, in dispatch
self.is_authenticated(request)
File "/home/bats/.virtualenvs/bats/lib/python2.7/site-packages/tastypie/resources.py", line 569, in is_authenticated
auth_result = self._
meta.authentication.is_authenticated(request)
File "/home/bats/.virtualenvs/bats/lib/python2.7/site-packages/tastypie/authentication.py", line 490, in is_authenticated
check =
backend.is_authenticated(request, **kwargs)
File "/home/bats/.virtualenvs/bats/lib/python2.7/site-packages/tastypie/authentication.py", line 194, in is_authenticated
user = User.objects.get(**lookup_kwargs)
File "/home/bats/.virtualenvs/bats/lib/python2.7/site-packages/django/db/models/manager.py", line 143, in get
return self.get_query_set().get(*args, **kwargs)
File "/home/bats/.virtualenvs/bats/lib/python2.7/site-packages/django/db/models/query.py", line 382, in get
num = len(clone)
File "/home/bats/.virtualenvs/bats/lib/python2.7/site-packages/django/db/models/query.py", line 90, in __len__
self._result_cache = list(self.iterator())
File "/home/bats/.virtualenvs/bats/lib/python2.7/site-packages/django/db/models/query.py", line 301, in iterator
for row in compiler.results_iter():
File "/home/bats/.virtualenvs/bats/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 775, in results_iter
for rows in self.execute_sql(MULTI):
File "/home/bats/.virtualenvs/bats/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 839, in execute_sql
cursor = self.connection.cursor()
File "/home/bats/.virtualenvs/bats/lib/python2.7/site-packages/django/db/backends/__init__.py", line 321, in cursor
self.validate_thread_sharing()
File "/home/bats/.virtualenvs/bats/lib/python2.7/site-packages/django/db/backends/__init__.py", line 154, in validate_thread_sharing
% (self.alias, self._thread_ident, thread.get_ident()))
DatabaseError: DatabaseWrapper objects created in a thread can only be used in that same thread. The object with alias 'default' was created in thread id 140625758729984 and this is thread id 140625691588352.