[devtools pg handler] some fixes so we get proper error when pg host is not reachable stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 16 Jun 2011 15:15:25 +0200
branchstable
changeset 7520 b7456b454334
parent 7519 4e531c8697f8
child 7521 a5a5eea29c1b
[devtools pg handler] some fixes so we get proper error when pg host is not reachable
devtools/__init__.py
--- a/devtools/__init__.py	Thu Jun 16 11:29:31 2011 +0200
+++ b/devtools/__init__.py	Thu Jun 16 15:15:25 2011 +0200
@@ -527,11 +527,19 @@
         return get_db_helper('postgres')
 
     @property
-    @cached
     def dbcnx(self):
-        from cubicweb.server.serverctl import _db_sys_cnx
-        return  _db_sys_cnx(self.system_source, 'CREATE DATABASE and / or USER',
-                            interactive=False)
+        try:
+            return self._cnx
+        except AttributeError:
+            from cubicweb.server.serverctl import _db_sys_cnx
+            try:
+                self._cnx = _db_sys_cnx(
+                    self.system_source, 'CREATE DATABASE and / or USER',
+                    interactive=False)
+                return self._cnx
+            except Exception:
+                self._cnx = None
+                raise
 
     @property
     @cached
@@ -569,17 +577,19 @@
                 cnx.close()
             init_repository(self.config, interactive=False)
         except:
-            self.dbcnx.rollback()
+            if self.dbcnx is not None:
+                self.dbcnx.rollback()
             print >> sys.stderr, 'building', self.dbname, 'failed'
             #self._drop(self.dbname)
             raise
 
     def helper_clear_cache(self):
-        self.dbcnx.commit()
-        self.dbcnx.close()
-        clear_cache(self, 'dbcnx')
+        if self.dbcnx is not None:
+            self.dbcnx.commit()
+            self.dbcnx.close()
+            del self._cnx
+            clear_cache(self, 'cursor')
         clear_cache(self, 'helper')
-        clear_cache(self, 'cursor')
 
     def __del__(self):
         self.helper_clear_cache()