coding utf-8 from django conf import settings from django core import

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# -*- coding: utf-8 -*-
from django.conf import settings
from django.core import signals
from django.db.models import sql
from django.db.models.query import QuerySet
from lib import _threading_local
def get_db_wrapper():
if not hasattr(_threading_local, 'DB_POOL'):
_threading_local.DB_POOL = {}
db = getattr(_threading_local, 'DATABASE', 'default')
database = settings.DATABASES[db]
if db in _threading_local.DB_POOL and \
_threading_local.DB_POOL[db]._valid_connection():
return _threading_local.DB_POOL[db]
backend = __import__('django.db.backends.' + database['DATABASE_ENGINE']
+ ".base", {}, {}, ['base'])
backup = {}
for key, value in database.iteritems():
backup[key] = getattr(settings, key)
setattr(settings, key, value)
wrapper = backend.DatabaseWrapper()
wrapper._cursor(settings)
_threading_local.DB_POOL[db] = wrapper
for key, value in backup.iteritems():
setattr(settings, key, value)
return wrapper
def close_connection_pool(**kwargs):
if hasattr(_threading_local, 'DB_POOL'):
for db_name in _threading_local.DB_POOL:
_threading_local.DB_POOL[db_name].close()
signals.request_finished.connect(close_connection_pool)
def __init__(self, model=None, query=None):
self.model = model
self.query = query or sql.Query(self.model, get_db_wrapper())
self._result_cache = None
self._iter = None
self._sticky_filter = False
QuerySet.__init__ = __init__