Skip to content

Commit

Permalink
Implement autocommit isolation level for pysqlite
Browse files Browse the repository at this point in the history
Fixes: #5164
Change-Id: I190b9de552dfed9f2a33babf82e42465ef09c82a
(cherry picked from commit 64e8303)
  • Loading branch information
gordthompson authored and zzzeek committed Mar 24, 2020
1 parent 1dcc807 commit 9ebbf86
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ test/test_schema.db
*test_schema.db
.idea
/Pipfile*
/querytest.db
5 changes: 5 additions & 0 deletions doc/build/changelog/unreleased_13/5164.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.. change::
:tags: sqlite, usecase
:tickets: 5164

Implemented AUTOCOMMIT isolation level for SQLite when using pysqlite.
13 changes: 11 additions & 2 deletions lib/sqlalchemy/dialects/sqlite/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ def bi_c(element, compiler, **kw):
.. _sqlite_isolation_level:
Transaction Isolation Level
----------------------------
Transaction Isolation Level / Autocommit
----------------------------------------
SQLite supports "transaction isolation" in a non-standard way, along two
axes. One is that of the
Expand All @@ -185,6 +185,15 @@ def bi_c(element, compiler, **kw):
SQLite defaults to ``SERIALIZABLE``, however its behavior is impacted by
the pysqlite driver's default behavior.
When using the pysqlite driver, the ``"AUTOCOMMIT"`` isolation level is also
available, which will alter the pysqlite connection using the ``.isolation_level``
attribute on the DBAPI connection and set it to None for the duration
of the setting.
.. versionadded:: 1.3.16 added support for SQLite AUTOCOMMIT isolation level
when using the pysqlite / sqlite3 SQLite driver.
The other axis along which SQLite's transactional locking is impacted is
via the nature of the ``BEGIN`` statement used. The three varieties
are "deferred", "immediate", and "exclusive", as described at
Expand Down
20 changes: 20 additions & 0 deletions lib/sqlalchemy/dialects/sqlite/pysqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,12 @@ def do_begin(conn):
# emit our own BEGIN
conn.execute("BEGIN")
.. warning:: When using the above recipe, it is advised to not use the
:paramref:`.execution_options.isolation_level` setting on
:class:`.Connection` and :func:`.create_engine` with the SQLite driver,
as this function necessarily will also alter the ".isolation_level" setting.
Above, we intercept a new pysqlite connection and disable any transactional
integration. Then, at the point at which SQLAlchemy knows that transaction
scope is to begin, we emit ``"BEGIN"`` ourselves.
Expand Down Expand Up @@ -437,6 +443,20 @@ def get_pool_class(cls, url):
def _get_server_version_info(self, connection):
return self.dbapi.sqlite_version_info

def set_isolation_level(self, connection, level):
if hasattr(connection, "connection"):
dbapi_connection = connection.connection
else:
dbapi_connection = connection

if level == "AUTOCOMMIT":
dbapi_connection.isolation_level = None
else:
dbapi_connection.isolation_level = ""
return super(SQLiteDialect_pysqlite, self).set_isolation_level(
connection, level
)

def create_connect_args(self, url):
if url.username or url.password or url.host or url.port:
raise exc.ArgumentError(
Expand Down
1 change: 1 addition & 0 deletions test/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ def get_isolation_levels(self, config):

if against(config, "sqlite"):
default = "SERIALIZABLE"
levels.add("AUTOCOMMIT")
elif against(config, "postgresql"):
default = "READ COMMITTED"
levels.add("AUTOCOMMIT")
Expand Down

0 comments on commit 9ebbf86

Please sign in to comment.