Created
September 22, 2014 14:40
-
-
Save young001/cc4a6edd6196a9ec13f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import transaction, load_backend | |
from testdjango import settings | |
from sqlalchemy import pool | |
pool_initialized = False | |
class hashabledict(dict): | |
def __hash__(self): | |
return hash(tuple(sorted(self.items()))) | |
class hashablelist(list): | |
def __hash__(self): | |
return hash(tuple(sorted(self))) | |
class ManagerProxy(object): | |
def __init__(self, manager): | |
self.manager = manager | |
def __getattr__(self, key): | |
return getattr(self.manager, key) | |
def connect(self, *args, **kwargs): | |
if 'conv' in kwargs: | |
conv = kwargs['conv'] | |
if isinstance(conv, dict): | |
items = [] | |
for k, v in conv.items(): | |
if isinstance(v, list): | |
v = hashablelist(v) | |
items.append((k, v)) | |
kwargs['conv'] = hashabledict(items) | |
return self.manager.connect(*args, **kwargs) | |
POOL_SETTINGS = {'max_overflow': 10, 'pool_size': 5} | |
def init_pool(): | |
if not globals().get('pool_initialized', False): | |
global pool_initialized | |
pool_initialized = True | |
try: | |
backendname = settings.DATABASES['default']['ENGINE'] | |
backend = load_backend(backendname) | |
backend.Database = ManagerProxy(pool.manage(backend.Database, **POOL_SETTINGS)) | |
# backend.Database = pool.manage(backend.Database) | |
backend.DatabaseError = backend.Database.DatabaseError | |
backend.IntegrityError = backend.Database.IntegrityError | |
logging.info("Connection Pool initialized") | |
except: | |
logging.exception("Connection Pool initialization error") | |
init_pool() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment