From 8220d9cdbe4ae97692a7676e4552572f1baa5131 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 27 Dec 2008 18:45:41 +0000 Subject: [PATCH] - Added a mutex for the initial pool creation when using pool.manage(dbapi). This prevents a minor case of "dogpile" behavior which would otherwise occur upon a heavy load startup. [ticket:799] --- CHANGES | 20 ++++++++++++++------ lib/sqlalchemy/pool.py | 18 +++++++++++++----- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/CHANGES b/CHANGES index d0c8ff1586..c919e9eddd 100644 --- a/CHANGES +++ b/CHANGES @@ -145,9 +145,6 @@ CHANGES mapper since it's not needed. - sql - - NullPool supports reconnect on failure behavior. - [ticket:1094] - - Columns can again contain percent signs within their names. [ticket:1256] @@ -156,9 +153,6 @@ CHANGES __eq__(). If the object does not implement __eq__() and mutable=True, a deprecation warning is raised. - - - Connection.invalidate() checks for closed status - to avoid attribute errors. [ticket:1246] - Fixed the import weirdness in sqlalchemy.sql to not export __names__ [ticket:1215]. @@ -198,6 +192,20 @@ CHANGES LIMIT/OFFSET), you should also call self_group() on it to apply parenthesis. + +- engine/pool + + - Connection.invalidate() checks for closed status + to avoid attribute errors. [ticket:1246] + + - NullPool supports reconnect on failure behavior. + [ticket:1094] + + - Added a mutex for the initial pool creation when + using pool.manage(dbapi). This prevents a minor + case of "dogpile" behavior which would otherwise + occur upon a heavy load startup. [ticket:799] + - _execute_clauseelement() goes back to being a private method. Subclassing Connection is not needed now that ConnectionProxy diff --git a/lib/sqlalchemy/pool.py b/lib/sqlalchemy/pool.py index ee4fa01908..1467a4f6b9 100644 --- a/lib/sqlalchemy/pool.py +++ b/lib/sqlalchemy/pool.py @@ -865,7 +865,8 @@ class _DBProxy(object): self.params = params self.poolclass = poolclass self.pools = {} - + self._create_pool_mutex = threading.Lock() + def close(self): for key in self.pools.keys(): del self.pools[key] @@ -881,10 +882,17 @@ class _DBProxy(object): try: return self.pools[key] except KeyError: - pool = self.poolclass(lambda: self.module.connect(*args, **params), **self.params) - self.pools[key] = pool - return pool - + self._create_pool_mutex.acquire() + try: + if key not in self.pools: + pool = self.poolclass(lambda: self.module.connect(*args, **params), **self.params) + self.pools[key] = pool + return pool + else: + return self.pools[key] + finally: + self._create_pool_mutex.release() + def connect(self, *args, **params): """Activate a connection to the database. -- 2.47.3