]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- adjust _revalidate_connection() again such that we pass a _wrap=False
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 6 Dec 2014 00:08:47 +0000 (19:08 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 6 Dec 2014 00:08:47 +0000 (19:08 -0500)
to it, so that we say we will do the wrapping just once right here
in _execute_context() / _execute_default().  An adjustment is made
to _handle_dbapi_error() to not assume self.__connection in case
we are already in an invalidated state

further adjustment to
0639c199a547343d62134d2f233225fd2862ec4541e7253dee168b8c26c49, #3266

lib/sqlalchemy/engine/base.py
lib/sqlalchemy/engine/threadlocal.py
test/engine/test_parseconnect.py
test/engine/test_reconnect.py

index 235e1bf438dc9777e85e700ed318763317720142..23348469d8ee8b6563e0c0ca271be07b959bf1c7 100644 (file)
@@ -265,18 +265,18 @@ class Connection(Connectable):
         try:
             return self.__connection
         except AttributeError:
-            return self._revalidate_connection()
+            return self._revalidate_connection(_wrap=True)
 
-    def _revalidate_connection(self):
+    def _revalidate_connection(self, _wrap):
         if self.__branch_from:
-            return self.__branch_from._revalidate_connection()
-
+            return self.__branch_from._revalidate_connection(_wrap=_wrap)
         if self.__can_reconnect and self.__invalid:
             if self.__transaction is not None:
                 raise exc.InvalidRequestError(
                     "Can't reconnect until invalid "
                     "transaction is rolled back")
-            self.__connection = self.engine.raw_connection(self)
+            self.__connection = self.engine.raw_connection(
+                _connection=self, _wrap=_wrap)
             self.__invalid = False
             return self.__connection
         raise exc.ResourceClosedError("This Connection is closed")
@@ -814,11 +814,11 @@ class Connection(Connectable):
                     fn(self, default, multiparams, params)
 
         try:
-            conn = self.__connection
-        except AttributeError:
-            conn = self._revalidate_connection()
+            try:
+                conn = self.__connection
+            except AttributeError:
+                conn = self._revalidate_connection(_wrap=False)
 
-        try:
             dialect = self.dialect
             ctx = dialect.execution_ctx_cls._init_default(
                 dialect, self, conn)
@@ -952,16 +952,17 @@ class Connection(Connectable):
         a :class:`.ResultProxy`."""
 
         try:
-            conn = self.__connection
-        except AttributeError:
-            conn = self._revalidate_connection()
+            try:
+                conn = self.__connection
+            except AttributeError:
+                conn = self._revalidate_connection(_wrap=False)
 
-        try:
             context = constructor(dialect, self, conn, *args)
         except Exception as e:
-            self._handle_dbapi_exception(e,
-                                         util.text_type(statement), parameters,
-                                         None, None)
+            self._handle_dbapi_exception(
+                e,
+                util.text_type(statement), parameters,
+                None, None)
 
         if context.compiled:
             context.pre_exec()
@@ -1149,7 +1150,10 @@ class Connection(Connectable):
             self._is_disconnect =  \
                 isinstance(e, self.dialect.dbapi.Error) and \
                 not self.closed and \
-                self.dialect.is_disconnect(e, self.__connection, cursor)
+                self.dialect.is_disconnect(
+                    e,
+                    self.__connection if not self.invalidated else None,
+                    cursor)
             if context:
                 context.is_disconnect = self._is_disconnect
 
@@ -1953,7 +1957,9 @@ class Engine(Connectable, log.Identified):
         """
         return self.run_callable(self.dialect.has_table, table_name, schema)
 
-    def _wrap_pool_connect(self, fn, connection=None):
+    def _wrap_pool_connect(self, fn, connection, wrap=True):
+        if not wrap:
+            return fn()
         dialect = self.dialect
         try:
             return fn()
@@ -1961,7 +1967,7 @@ class Engine(Connectable, log.Identified):
             Connection._handle_dbapi_exception_noconnection(
                 e, dialect, self, connection)
 
-    def raw_connection(self, _connection=None):
+    def raw_connection(self, _connection=None, _wrap=True):
         """Return a "raw" DBAPI connection from the connection pool.
 
         The returned object is a proxied version of the DBAPI
@@ -1978,7 +1984,7 @@ class Engine(Connectable, log.Identified):
 
         """
         return self._wrap_pool_connect(
-            self.pool.unique_connection, _connection)
+            self.pool.unique_connection, _connection, _wrap)
 
 
 class OptionEngine(Engine):
index 71caac62659744523c81ae8adad4fbca44789b61..824b68fdfa914511ce4cd07bfe37380fe8af96bd 100644 (file)
@@ -59,7 +59,10 @@ class TLEngine(base.Engine):
             # guards against pool-level reapers, if desired.
             # or not connection.connection.is_valid:
             connection = self._tl_connection_cls(
-                self, self._wrap_pool_connect(self.pool.connect), **kw)
+                self,
+                self._wrap_pool_connect(
+                    self.pool.connect, connection, wrap=True),
+                **kw)
             self._connections.conn = weakref.ref(connection)
 
         return connection._increment_connect()
index b6d08cebaaf292a40598fde1a1f570c1b406e5c3..4a3da7d1ce61bcad72e02abf56525bdee649f0e4 100644 (file)
@@ -307,7 +307,7 @@ class CreateEngineTest(fixtures.TestBase):
 
         assert_raises(
             MySpecialException,
-            conn._revalidate_connection
+            getattr, conn, 'connection'
         )
 
     @testing.requires.sqlite
index 4500ada6a02b0d13740fa2781f3e20e9d09404e7..0efce87ce467b3665c9dd408811840f92a3a61b8 100644 (file)
@@ -517,7 +517,7 @@ class RealReconnectTest(fixtures.TestBase):
             assert c1.invalidated
             assert c1_branch.invalidated
 
-            c1_branch._revalidate_connection()
+            c1_branch._revalidate_connection(_wrap=True)
             assert not c1.invalidated
             assert not c1_branch.invalidated
 
@@ -535,7 +535,7 @@ class RealReconnectTest(fixtures.TestBase):
         assert c1.invalidated
         assert c1_branch.invalidated
 
-        c1._revalidate_connection()
+        c1._revalidate_connection(_wrap=True)
         assert not c1.invalidated
         assert not c1_branch.invalidated