Skip to content

Instantly share code, notes, and snippets.

@andymckay
Created January 20, 2012 20:38
Show Gist options
  • Save andymckay/1649432 to your computer and use it in GitHub Desktop.
Save andymckay/1649432 to your computer and use it in GitHub Desktop.
diff --git a/mysql_pool/base.py b/mysql_pool/base.py
index b8be24f..5548602 100644
--- a/mysql_pool/base.py
+++ b/mysql_pool/base.py
@@ -27,7 +27,10 @@ Database = manage(Database, **getattr(settings, 'DATABASE_POOL_ARGS', {}))
def serialize(**kwargs):
# We need to figure out what database connection goes where
# so we'll hash the args.
- return hashlib.md5(str(kwargs)).hexdigest()
+ keys = sorted(kwargs.keys())
+ out = [repr(k) + repr(kwargs[k])
+ for k in keys if isinstance(kwargs[k], (str, int, bool))]
+ return hashlib.md5(''.join(out)).hexdigest()
class DatabaseWrapper(DatabaseWrapper):
@@ -61,15 +64,12 @@ class DatabaseWrapper(DatabaseWrapper):
# SQL Alchemy can't serialize the dict that's in OPTIONS, so
# we'll do some serialization ourselves. You can avoid this
# step specifying sa_pool_key in the DB settings.
- if 'sa_pool_key' not in kwargs:
- kwargs['sa_pool_key'] = serialize(**kwargs)
- self._settings_dict = kwargs
+ kwargs['sa_pool_key'] = serialize(**kwargs)
+ return kwargs
def _cursor(self):
- if not getattr(self, '_settings_dict', None):
- self._set_settings()
-
- self.connection = Database.connect(**self._settings_dict)
+ settings = self._set_settings()
+ self.connection = Database.connect(**settings)
self.connection.encoders[SafeUnicode] = self.connection.encoders[unicode]
self.connection.encoders[SafeString] = self.connection.encoders[str]
connection_created.send(sender=self.__class__, connection=self)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment