title: how is Django persistent database connections thread safe?
body: <p>In django, persistent db connections are implemented by <code>threading.local</code>. This code resides in the class <code>django.db.utils.ConnectionHandler</code>. Since this class is instantiated in <code>django.db.__init.__.py</code> it gets instantiated when we do <code>import django.db</code>. And this would happen somewhere during request/response cycle. For subsequent requests in same thread or even different thread in same process <code>django.db.__init__.py</code> won't be executed since it is already loaded. </p>
  
  <p>So my question is wouldn't it share the connection for all threads in same process. Am I missing something here?</p>
  
  <p>I think something like following should work just fine instead</p>
  
  <pre><code># file db/__init__.py
  connections = threading.local()
  connections.connections = ConnectionHandler()
  </code></pre>