From: Mike Bayer Date: Thu, 16 Mar 2006 19:09:53 +0000 (+0000) Subject: reorganized SingletonThreadPool to return distinct connections in the same thread... X-Git-Tag: rel_0_1_5~73 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c858ba70b16d05a1b05e17c49656f4d0e225219f;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git reorganized SingletonThreadPool to return distinct connections in the same thread; use_threadlocal behavior is now switchable --- diff --git a/lib/sqlalchemy/pool.py b/lib/sqlalchemy/pool.py index ad65f7462a..5ab8ba5788 100644 --- a/lib/sqlalchemy/pool.py +++ b/lib/sqlalchemy/pool.py @@ -120,7 +120,7 @@ class Pool(object): raise NotImplementedError() def log(self, msg): - self.logger.write(msg) + self._logger.write(msg) class ConnectionFairy(object): def __init__(self, pool, connection=None): @@ -155,19 +155,17 @@ class SingletonThreadPool(Pool): """Maintains one connection per each thread, never moving to another thread. this is used for SQLite and other databases with a similar restriction.""" def __init__(self, creator, **params): - params['use_threadlocal'] = False Pool.__init__(self, **params) self._conns = {} self._creator = creator def status(self): - return "SingletonThreadPool size: %d" % len(self._conns) - - def unique_connection(self): - return ConnectionFairy(self, self._creator()) + return "SingletonThreadPool thread:%d size: %d" % (thread.get_ident(), len(self._conns)) def do_return_conn(self, conn): - pass + if self._conns.get(thread.get_ident(), None) is None: + self._conns[thread.get_ident()] = conn + def do_return_invalid(self): try: del self._conns[thread.get_ident()] @@ -176,9 +174,13 @@ class SingletonThreadPool(Pool): def do_get(self): try: - return self._conns[thread.get_ident()] + c = self._conns[thread.get_ident()] + if c is None: + return self._creator() except KeyError: - return self._conns.setdefault(thread.get_ident(), self._creator()) + c = self._creator() + self._conns[thread.get_ident()] = None + return c class QueuePool(Pool): """uses Queue.Queue to maintain a fixed-size list of connections.""" diff --git a/test/pool.py b/test/pool.py index 811bda51ae..2737a33b1c 100644 --- a/test/pool.py +++ b/test/pool.py @@ -69,7 +69,22 @@ class PoolTest(PersistTest): self.assert_(status(p) == (3, 1, 0, 2)) c2 = None self.assert_(status(p) == (3, 2, 0, 1)) - + + def testthreadlocal(self): + for p in ( + pool.QueuePool(creator = lambda: sqlite.connect('foo.db'), pool_size = 3, max_overflow = -1, use_threadlocal = True, echo = False), + pool.SingletonThreadPool(creator = lambda: sqlite.connect('foo.db'), use_threadlocal = True) + ): + c1 = p.connect() + c2 = p.connect() + self.assert_(c1 is c2) + c3 = p.unique_connection() + self.assert_(c3 is not c1) + c2 = None + c2 = p.connect() + self.assert_(c1 is c2) + self.assert_(c3 is not c1) + def tearDown(self): pool.clear_managers() for file in ('foo.db', 'bar.db'):