Work around stale database connections (#4) in the DNS server
authorEric Price <ecprice@mit.edu>
Sat, 29 Mar 2008 02:19:05 +0000 (22:19 -0400)
committerEric Price <ecprice@mit.edu>
Sat, 29 Mar 2008 02:19:05 +0000 (22:19 -0400)
svn path=/trunk/packages/sipb-xen-dns/; revision=302

code/dnsserver.py

index b6a5ac3..0cad87a 100755 (executable)
@@ -7,6 +7,9 @@ from twisted.internet import defer
 from twisted.python import failure
 
 import sipb_xen_database
+import psycopg2
+import sqlalchemy
+import time
 
 class DatabaseAuthority(common.ResolverBase):
     """An Authority that is loaded from a file."""
@@ -23,6 +26,19 @@ class DatabaseAuthority(common.ResolverBase):
                                   serial=1, refresh=3600, retry=900,
                                   expire=3600000, minimum=21600, ttl=3600)
     def _lookup(self, name, cls, type, timeout = None):
+        for i in range(3):
+            try:
+                value = self._lookup_unsafe(name, cls, type, timeout = None)
+            except (psycopg2.OperationalError, sqlalchemy.exceptions.SQLError):
+                if i == 2:
+                    raise
+                print "Reloading database"
+                time.sleep(0.5)
+                continue
+            else:
+                return value
+
+    def _lookup_unsafe(self, name, cls, type, timeout):
         sipb_xen_database.clear_cache()
         if not (name.lower() == self.domain or 
                 name.lower().endswith('.'+self.domain)):