From: Mike Bayer Date: Fri, 18 Dec 2020 14:28:06 +0000 (-0500) Subject: Gracefully degrade on v$transaction not readable X-Git-Tag: rel_1_3_22~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d6842bcb722f07ef9fe62e0888ee8ef3c7fd963c;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Gracefully degrade on v$transaction not readable Fixed regression which occured due to [ticket:5755] which implemented isolation level support for Oracle. It has been reported that many Oracle accounts don't actually have permission to query the ``v$transaction`` view so this feature has been altered to gracefully fallback when it fails upon database connect, where the dialect will assume "READ COMMITTED" is the default isolation level as was the case prior to SQLAlchemy 1.3.21. However, explicit use of the :meth:`_engine.Connection.get_isolation_level` method must now necessarily raise an exception, as Oracle databases with this restriction explicitly disallow the user from reading the current isolation level. Fixes: #5784 Change-Id: Iefc82928744f3c944c18ae8000eb3c9e52e523bc (cherry picked from commit 566f1b520e05659ba4d4928c740a9aa1b0fed614) --- diff --git a/doc/build/changelog/unreleased_13/5784.rst b/doc/build/changelog/unreleased_13/5784.rst new file mode 100644 index 0000000000..87ee080b5c --- /dev/null +++ b/doc/build/changelog/unreleased_13/5784.rst @@ -0,0 +1,15 @@ +.. change:: + :tags: bug, oracle + :tickets: 5784 + :versions: 1.4.0b2 + + Fixed regression which occured due to :ticket:`5755` which implemented + isolation level support for Oracle. It has been reported that many Oracle + accounts don't actually have permission to query the ``v$transaction`` + view so this feature has been altered to gracefully fallback when it fails + upon database connect, where the dialect will assume "READ COMMITTED" is + the default isolation level as was the case prior to SQLAlchemy 1.3.21. + However, explicit use of the :meth:`_engine.Connection.get_isolation_level` + method must now necessarily raise an exception, as Oracle databases with + this restriction explicitly disallow the user from reading the current + isolation level. \ No newline at end of file diff --git a/lib/sqlalchemy/dialects/oracle/base.py b/lib/sqlalchemy/dialects/oracle/base.py index dadc1335e5..3028704a4f 100644 --- a/lib/sqlalchemy/dialects/oracle/base.py +++ b/lib/sqlalchemy/dialects/oracle/base.py @@ -66,10 +66,28 @@ Valid values for ``isolation_level`` include: * ``AUTOCOMMIT`` * ``SERIALIZABLE`` -.. note:: The implementation :meth:`_engine.Connection.get_isolation_level` - implemented by the Oracle dialect necessarily forces the start of - a transaction using the Oracle LOCAL_TRANSACTION_ID function; otherwise - no level is normally readable. +.. note:: The implementation for the + :meth:`_engine.Connection.get_isolation_level` method as implemented by the + Oracle dialect necessarily forces the start of a transaction using the + Oracle LOCAL_TRANSACTION_ID function; otherwise no level is normally + readable. + + Additionally, the :meth:`_engine.Connection.get_isolation_level` method will + raise an exception if the ``v$transaction`` view is not available due to + permissions or other reasons, which is a common occurrence in Oracle + installations. + + The cx_Oracle dialect attempts to call the + :meth:`_engine.Connection.get_isolation_level` method when the dialect makes + its first connection to the database in order to acquire the + "default"isolation level. This default level is necessary so that the level + can be reset on a connection after it has been temporarily modified using + :meth:`_engine.Connection.execution_options` method. In the common event + that the :meth:`_engine.Connection.get_isolation_level` method raises an + exception due to ``v$transaction`` not being readable as well as any other + database-related failure, the level is assumed to be "READ COMMITTED". No + warning is emitted for this initial first-connect condition as it is + expected to be a common restriction on Oracle databases. .. versionadded:: 1.3.16 added support for AUTOCOMMIT to the cx_oracle dialect as well as the notion of a default isolation level @@ -77,6 +95,11 @@ Valid values for ``isolation_level`` include: .. versionadded:: 1.3.21 Added support for SERIALIZABLE as well as live reading of the isolation level. +.. versionchanged:: 1.3.22 In the event that the default isolation + level cannot be read due to permissions on the v$transaction view as + is common in Oracle installations, the default isolation level is hardcoded + to "READ COMMITTED" which was the behavior prior to 1.3.21. + .. seealso:: :ref:`dbapi_autocommit` @@ -1408,6 +1431,14 @@ class OracleDialect(default.DefaultDialect): def get_isolation_level(self, connection): raise NotImplementedError("implemented by cx_Oracle dialect") + def get_default_isolation_level(self, dbapi_conn): + try: + return self.get_isolation_level(dbapi_conn) + except NotImplementedError: + raise + except: + return "READ COMMITTED" + def set_isolation_level(self, connection, level): raise NotImplementedError("implemented by cx_Oracle dialect") diff --git a/lib/sqlalchemy/engine/default.py b/lib/sqlalchemy/engine/default.py index 2584924713..6ab10eec70 100644 --- a/lib/sqlalchemy/engine/default.py +++ b/lib/sqlalchemy/engine/default.py @@ -322,7 +322,7 @@ class DefaultDialect(interfaces.Dialect): self.default_schema_name = None try: - self.default_isolation_level = self.get_isolation_level( + self.default_isolation_level = self.get_default_isolation_level( connection.connection ) except NotImplementedError: @@ -367,6 +367,22 @@ class DefaultDialect(interfaces.Dialect): """ return None + def get_default_isolation_level(self, dbapi_conn): + """Given a DBAPI connection, return its isolation level, or + a default isolation level if one cannot be retrieved. + + May be overridden by subclasses in order to provide a + "fallback" isolation level for databases that cannot reliably + retrieve the actual isolation level. + + By default, calls the :meth:`_engine.Interfaces.get_isolation_level` + method, propagating any exceptions raised. + + .. versionadded:: 1.3.22 + + """ + return self.get_isolation_level(dbapi_conn) + def _check_unicode_returns(self, connection, additional_tests=None): if util.py2k and not self.supports_unicode_statements: cast_to = util.binary_type diff --git a/lib/sqlalchemy/engine/interfaces.py b/lib/sqlalchemy/engine/interfaces.py index d9dac0600c..170b9852aa 100644 --- a/lib/sqlalchemy/engine/interfaces.py +++ b/lib/sqlalchemy/engine/interfaces.py @@ -872,6 +872,26 @@ class Dialect(object): raise NotImplementedError() + def get_default_isolation_level(self, dbapi_conn): + """Given a DBAPI connection, return its isolation level, or + a default isolation level if one cannot be retrieved. + + This method may only raise NotImplementedError and + **must not raise any other exception**, as it is used implicitly upon + first connect. + + The method **must return a value** for a dialect that supports + isolation level settings, as this level is what will be reverted + towards when a per-connection isolation level change is made. + + The method defaults to using the :meth:`.Dialect.get_isolation_level` + method unless overridden by a dialect. + + .. versionadded:: 1.3.22 + + """ + raise NotImplementedError() + @classmethod def get_dialect_cls(cls, url): """Given a URL, return the :class:`.Dialect` that will be used. diff --git a/test/dialect/oracle/test_dialect.py b/test/dialect/oracle/test_dialect.py index 006d358899..c28218a3d3 100644 --- a/test/dialect/oracle/test_dialect.py +++ b/test/dialect/oracle/test_dialect.py @@ -24,6 +24,7 @@ from sqlalchemy.testing import assert_raises from sqlalchemy.testing import assert_raises_message from sqlalchemy.testing import AssertsCompiledSQL from sqlalchemy.testing import AssertsExecutionResults +from sqlalchemy.testing import engines from sqlalchemy.testing import eq_ from sqlalchemy.testing import fixtures from sqlalchemy.testing import mock @@ -65,6 +66,73 @@ class DialectTest(fixtures.TestBase): cx_oracle.OracleDialect_cx_oracle(dbapi=Mock()) +class DialectWBackendTest(fixtures.TestBase): + __backend__ = True + __only_on__ = "oracle" + + def test_hypothetical_not_implemented_isolation_level(self): + engine = engines.testing_engine() + + def get_isolation_level(connection): + raise NotImplementedError + + with mock.patch.object( + engine.dialect, "get_isolation_level", get_isolation_level + ): + conn = engine.connect() + + # for NotImplementedError we get back None. But the + # cx_Oracle dialect does not raise this. + eq_(conn.dialect.default_isolation_level, None) + + dbapi_conn = conn.connection.connection + + eq_( + testing.db.dialect.get_isolation_level(dbapi_conn), + "READ COMMITTED", + ) + + def test_graceful_failure_isolation_level_not_available(self): + engine = engines.testing_engine() + + def get_isolation_level(connection): + raise exc.DBAPIError( + "get isolation level", + {}, + engine.dialect.dbapi.Error("isolation level failed"), + ) + + with mock.patch.object( + engine.dialect, "get_isolation_level", get_isolation_level + ): + conn = engine.connect() + eq_(conn.dialect.default_isolation_level, "READ COMMITTED") + + # test that we can use isolation level setting and that it + # reverts for "real" back to READ COMMITTED even though we + # can't read it + dbapi_conn = conn.connection.connection + + conn = conn.execution_options(isolation_level="SERIALIZABLE") + eq_( + testing.db.dialect.get_isolation_level(dbapi_conn), + "SERIALIZABLE", + ) + + conn.close() + eq_( + testing.db.dialect.get_isolation_level(dbapi_conn), + "READ COMMITTED", + ) + + with engine.connect() as conn: + assert_raises_message( + exc.DBAPIError, + r".*isolation level failed.*", + conn.get_isolation_level, + ) + + class EncodingErrorsTest(fixtures.TestBase): """mock test for encoding_errors.