]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- simplify the "noconnection" error handling, setting
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 8 Dec 2014 19:05:20 +0000 (14:05 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 8 Dec 2014 19:05:20 +0000 (14:05 -0500)
_handle_dbapi_exception_noconnection() to only invoke in the case
of raw_connection() in the constructor of Connection.   in all other
cases the Connection proceeds with _handle_dbapi_exception() including
revalidate.

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

index 23348469d8ee8b6563e0c0ca271be07b959bf1c7..dd8ea275cfe6b96ef2193557bd7f37039ccd84ac 100644 (file)
@@ -265,18 +265,20 @@ class Connection(Connectable):
         try:
             return self.__connection
         except AttributeError:
-            return self._revalidate_connection(_wrap=True)
+            try:
+                return self._revalidate_connection()
+            except Exception as e:
+                self._handle_dbapi_exception(e, None, None, None, None)
 
-    def _revalidate_connection(self, _wrap):
+    def _revalidate_connection(self):
         if self.__branch_from:
-            return self.__branch_from._revalidate_connection(_wrap=_wrap)
+            return self.__branch_from._revalidate_connection()
         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(
-                _connection=self, _wrap=_wrap)
+            self.__connection = self.engine.raw_connection(_connection=self)
             self.__invalid = False
             return self.__connection
         raise exc.ResourceClosedError("This Connection is closed")
@@ -817,7 +819,7 @@ class Connection(Connectable):
             try:
                 conn = self.__connection
             except AttributeError:
-                conn = self._revalidate_connection(_wrap=False)
+                conn = self._revalidate_connection()
 
             dialect = self.dialect
             ctx = dialect.execution_ctx_cls._init_default(
@@ -955,7 +957,7 @@ class Connection(Connectable):
             try:
                 conn = self.__connection
             except AttributeError:
-                conn = self._revalidate_connection(_wrap=False)
+                conn = self._revalidate_connection()
 
             context = constructor(dialect, self, conn, *args)
         except Exception as e:
@@ -1248,8 +1250,7 @@ class Connection(Connectable):
                 self.close()
 
     @classmethod
-    def _handle_dbapi_exception_noconnection(
-            cls, e, dialect, engine, connection):
+    def _handle_dbapi_exception_noconnection(cls, e, dialect, engine):
 
         exc_info = sys.exc_info()
 
@@ -1271,7 +1272,7 @@ class Connection(Connectable):
 
         if engine._has_events:
             ctx = ExceptionContextImpl(
-                e, sqlalchemy_exception, engine, connection, None, None,
+                e, sqlalchemy_exception, engine, None, None, None,
                 None, None, is_disconnect)
             for fn in engine.dispatch.handle_error:
                 try:
@@ -1957,17 +1958,18 @@ class Engine(Connectable, log.Identified):
         """
         return self.run_callable(self.dialect.has_table, table_name, schema)
 
-    def _wrap_pool_connect(self, fn, connection, wrap=True):
-        if not wrap:
-            return fn()
+    def _wrap_pool_connect(self, fn, connection):
         dialect = self.dialect
         try:
             return fn()
         except dialect.dbapi.Error as e:
-            Connection._handle_dbapi_exception_noconnection(
-                e, dialect, self, connection)
+            if connection is None:
+                Connection._handle_dbapi_exception_noconnection(
+                    e, dialect, self)
+            else:
+                util.reraise(*sys.exc_info())
 
-    def raw_connection(self, _connection=None, _wrap=True):
+    def raw_connection(self, _connection=None):
         """Return a "raw" DBAPI connection from the connection pool.
 
         The returned object is a proxied version of the DBAPI
@@ -1984,7 +1986,7 @@ class Engine(Connectable, log.Identified):
 
         """
         return self._wrap_pool_connect(
-            self.pool.unique_connection, _connection, _wrap)
+            self.pool.unique_connection, _connection)
 
 
 class OptionEngine(Engine):
index 824b68fdfa914511ce4cd07bfe37380fe8af96bd..e64ab09f4c19a821c880cdebd2160d8f9cb3b0cd 100644 (file)
@@ -61,7 +61,7 @@ class TLEngine(base.Engine):
             connection = self._tl_connection_cls(
                 self,
                 self._wrap_pool_connect(
-                    self.pool.connect, connection, wrap=True),
+                    self.pool.connect, connection),
                 **kw)
             self._connections.conn = weakref.ref(connection)
 
index 0efce87ce467b3665c9dd408811840f92a3a61b8..4500ada6a02b0d13740fa2781f3e20e9d09404e7 100644 (file)
@@ -517,7 +517,7 @@ class RealReconnectTest(fixtures.TestBase):
             assert c1.invalidated
             assert c1_branch.invalidated
 
-            c1_branch._revalidate_connection(_wrap=True)
+            c1_branch._revalidate_connection()
             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(_wrap=True)
+        c1._revalidate_connection()
         assert not c1.invalidated
         assert not c1_branch.invalidated