diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2005-11-06 20:17:13 +0000 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2005-11-06 20:17:13 +0000 |
commit | ac101149089e573add3dc65ac783547b711f20ed (patch) | |
tree | b5a8d361a6978458252f6c0fa69cebf9ee040886 | |
parent | 415ee39db578a17cefc1e4ccd8526ec5612be2e0 (diff) | |
download | sqlalchemy-ac101149089e573add3dc65ac783547b711f20ed.tar.gz |
sqlite cant share among threads ! hence a new pool class.
-rw-r--r-- | lib/sqlalchemy/databases/sqlite.py | 1 | ||||
-rw-r--r-- | lib/sqlalchemy/pool.py | 18 |
2 files changed, 19 insertions, 0 deletions
diff --git a/lib/sqlalchemy/databases/sqlite.py b/lib/sqlalchemy/databases/sqlite.py index da993d9a5..53c5b771f 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 53e861393..fc1e3c502 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 |