]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Dialect.initialize() is not called a second time if an :class:`.Engine`
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 11 Jul 2013 19:15:09 +0000 (15:15 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 11 Jul 2013 19:16:34 +0000 (15:16 -0400)
is recreated, due to a disconnect error.   This fixes a particular
issue in the Oracle 8 dialect, but in general the dialect.initialize()
phase should only be once per dialect.  [ticket:2776]

doc/build/changelog/changelog_08.rst
lib/sqlalchemy/engine/strategies.py
lib/sqlalchemy/util/__init__.py
lib/sqlalchemy/util/langhelpers.py
test/engine/test_reconnect.py

index d6a0b696218d8e9ffd422122be393b9caab2fd4a..145eb14971b007f3b17abf83749525c71234b0d8 100644 (file)
@@ -6,6 +6,13 @@
 .. changelog::
     :version: 0.8.3
 
+        :tickets: 2776
+
+        Dialect.initialize() is not called a second time if an :class:`.Engine`
+        is recreated, due to a disconnect error.   This fixes a particular
+        issue in the Oracle 8 dialect, but in general the dialect.initialize()
+        phase should only be once per dialect.
+
     .. change::
         :tags: feature, sql
         :tickets: 722
index 4c81df8f06e4522b6fcf79ca894105c5e6e1b725..8f30f1a2c7eaeea8f3b4811bde379cd793788886 100644 (file)
@@ -155,6 +155,7 @@ class DefaultEngineStrategy(EngineStrategy):
                 event.listen(pool, 'first_connect', on_connect)
                 event.listen(pool, 'connect', on_connect)
 
+            @util.only_once
             def first_connect(dbapi_connection, connection_record):
                 c = base.Connection(engine, connection=dbapi_connection)
 
index c2c5bddb85143e414a9048c8225808eccdccd4e0..8e36ea611d77da769a82d14da901f2d536730fe5 100644 (file)
@@ -28,7 +28,8 @@ from .langhelpers import iterate_attributes, class_hierarchy, \
     duck_type_collection, assert_arg_type, symbol, dictlike_iteritems,\
     classproperty, set_creation_order, warn_exception, warn, NoneType,\
     constructor_copy, methods_equivalent, chop_traceback, asint,\
-    generic_repr, counter, PluginLoader, hybridmethod, safe_reraise
+    generic_repr, counter, PluginLoader, hybridmethod, safe_reraise,\
+    only_once
 
 from .deprecations import warn_deprecated, warn_pending_deprecation, \
     deprecated, pending_deprecation
index d82aefdeae7d67f27cec863804540b9b442863d4..82822b086bda83631a8b286210d0b300e969d982 100644 (file)
@@ -1042,6 +1042,20 @@ _SQLA_RE = re.compile(r'sqlalchemy/([a-z_]+/){0,2}[a-z_]+\.py')
 _UNITTEST_RE = re.compile(r'unit(?:2|test2?/)')
 
 
+def only_once(fn):
+    """Decorate the given function to be a no-op after it is called exactly
+    once."""
+
+    once = [fn]
+    def go(*arg, **kw):
+        if once:
+            once_fn = once.pop()
+            return once_fn(*arg, **kw)
+
+    return update_wrapper(go, fn)
+
+
+
 def chop_traceback(tb, exclude_prefix=_UNITTEST_RE, exclude_suffix=_SQLA_RE):
     """Chop extraneous lines off beginning and end of a traceback.
 
index dee2fd2169fdb259b43b6983323fe907ccf044f3..1936cec081b628ba30ad0fba8d51920ad142af4e 100644 (file)
@@ -348,6 +348,28 @@ class MockReconnectTest(fixtures.TestBase):
             list, result
         )
 
+    def test_dialect_initialize_once(self):
+        from sqlalchemy.engine.base import Engine
+        from sqlalchemy.engine.url import URL
+        from sqlalchemy.engine.default import DefaultDialect
+        from sqlalchemy.pool import QueuePool
+        dbapi = self.dbapi
+
+        mock_dialect = Mock()
+        class MyURL(URL):
+            def get_dialect(self):
+                return Dialect
+        class Dialect(DefaultDialect):
+            initialize = Mock()
+
+        engine = create_engine(MyURL("foo://"), module=dbapi)
+        c1 = engine.connect()
+        engine.dispose()
+        c2 = engine.connect()
+        eq_(Dialect.initialize.call_count, 1)
+
+
+
 class CursorErrTest(fixtures.TestBase):
 
     def setup(self):
@@ -493,9 +515,15 @@ class RealReconnectTest(fixtures.TestBase):
         # raises a DBAPIError, not an AttributeError
         assert_raises(exc.DBAPIError, engine.connect)
 
-        # dispose connections so we get a new one on
-        # next go
-        engine.dispose()
+    @testing.skip_if(
+        [lambda: util.py3k, "oracle+cx_oracle"],
+        "Crashes on py3k+cx_oracle")
+    def test_explode_in_initializer_disconnect(self):
+        engine = engines.testing_engine()
+        def broken_initialize(connection):
+            connection.execute("select fake_stuff from _fake_table")
+
+        engine.dialect.initialize = broken_initialize
 
         p1 = engine.pool