From 1ee6332ba261cc1b6bbc8f427d17f81614b07d9b Mon Sep 17 00:00:00 2001 From: YongJieYongJie Date: Wed, 28 Apr 2021 13:30:09 -0400 Subject: [PATCH] Use SingletonThreadPool for in-memory SQLite database created using filename uri Default to using ``SingletonThreadPool`` for in-memory SQLite databases created using URI filenames. Previously the default pool used was the ``NullPool`` that precented sharing the same database between multiple engines. Fixes: #6379 Closes: #6380 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/6380 Pull-request-sha: 3b8024417a3a54c8269d43e40801aa4e58593540 Change-Id: Ice09622796455e796ede7711c98f3ceec13aa949 --- doc/build/changelog/unreleased_14/6379.rst | 8 ++++++++ lib/sqlalchemy/dialects/sqlite/pysqlite.py | 4 +++- test/dialect/test_sqlite.py | 5 +++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 doc/build/changelog/unreleased_14/6379.rst diff --git a/doc/build/changelog/unreleased_14/6379.rst b/doc/build/changelog/unreleased_14/6379.rst new file mode 100644 index 0000000000..9ea82bcea2 --- /dev/null +++ b/doc/build/changelog/unreleased_14/6379.rst @@ -0,0 +1,8 @@ +.. change:: + :tags: usecase, sqlite + :tickets: 6379 + + Default to using ``SingletonThreadPool`` for in-memory SQLite databases + created using URI filenames. Previously the default pool used was the + ``NullPool`` that precented sharing the same database between multiple + engines. diff --git a/lib/sqlalchemy/dialects/sqlite/pysqlite.py b/lib/sqlalchemy/dialects/sqlite/pysqlite.py index 0b091e73b6..20a4bb7acb 100644 --- a/lib/sqlalchemy/dialects/sqlite/pysqlite.py +++ b/lib/sqlalchemy/dialects/sqlite/pysqlite.py @@ -474,7 +474,9 @@ class SQLiteDialect_pysqlite(SQLiteDialect): @classmethod def _is_url_file_db(cls, url): - if url.database and url.database != ":memory:": + if (url.database and url.database != ":memory:") and ( + url.query.get("mode", None) != "memory" + ): return True else: return False diff --git a/test/dialect/test_sqlite.py b/test/dialect/test_sqlite.py index 7fd7ac333c..bae97bb9a2 100644 --- a/test/dialect/test_sqlite.py +++ b/test/dialect/test_sqlite.py @@ -710,6 +710,11 @@ class DialectTest( e = create_engine("sqlite+pysqlite:///:memory:") assert e.pool.__class__ is pool.SingletonThreadPool + e = create_engine( + "sqlite+pysqlite:///file:foo.db?mode=memory&uri=true" + ) + assert e.pool.__class__ is pool.SingletonThreadPool + e = create_engine("sqlite+pysqlite:///foo.db") assert e.pool.__class__ is pool.NullPool -- 2.47.3