From ac101149089e573add3dc65ac783547b711f20ed Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 6 Nov 2005 20:17:13 +0000 Subject: [PATCH] sqlite cant share among threads ! hence a new pool class. --- lib/sqlalchemy/databases/sqlite.py | 1 + lib/sqlalchemy/pool.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/sqlalchemy/databases/sqlite.py b/lib/sqlalchemy/databases/sqlite.py index da993d9a52..53c5b771f5 100644 --- a/lib/sqlalchemy/databases/sqlite.py +++ b/lib/sqlalchemy/databases/sqlite.py @@ -87,6 +87,7 @@ class SQLiteSQLEngine(ansisql.ANSISQLEngine): def __init__(self, opts, **params): self.filename = opts.pop('filename') self.opts = opts or {} + params['poolclass'] = sqlalchemy.pool.SingletonThreadPool ansisql.ANSISQLEngine.__init__(self, **params) def post_exec(self, connection, cursor, statement, parameters, echo = None, compiled = None, **kwargs): diff --git a/lib/sqlalchemy/pool.py b/lib/sqlalchemy/pool.py index 53e861393b..fc1e3c502a 100644 --- a/lib/sqlalchemy/pool.py +++ b/lib/sqlalchemy/pool.py @@ -111,8 +111,26 @@ class CursorFairy(object): self.cursor = cursor def __getattr__(self, key): return getattr(self.cursor, key) + +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 return_conn(self, conn): + pass + def get(self): + try: + return self._conns[thread.get_ident()] + except KeyError: + return self._conns.setdefault(thread.get_ident(), self._creator()) + class QueuePool(Pool): + """uses Queue.Queue to maintain a fixed-size list of connections.""" def __init__(self, creator, pool_size = 5, max_overflow = 10, **params): Pool.__init__(self, **params) self._creator = creator -- 2.47.2