You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@gump.apache.org by bo...@apache.org on 2009/09/09 11:01:32 UTC

svn commit: r812843 - /gump/trunk/python/gump/actor/stats/mysql/statsdb.py

Author: bodewig
Date: Wed Sep  9 09:01:32 2009
New Revision: 812843

URL: http://svn.apache.org/viewvc?rev=812843&view=rev
Log:
This looks correct but is completely untested

Modified:
    gump/trunk/python/gump/actor/stats/mysql/statsdb.py

Modified: gump/trunk/python/gump/actor/stats/mysql/statsdb.py
URL: http://svn.apache.org/viewvc/gump/trunk/python/gump/actor/stats/mysql/statsdb.py?rev=812843&r1=812842&r2=812843&view=diff
==============================================================================
--- gump/trunk/python/gump/actor/stats/mysql/statsdb.py (original)
+++ gump/trunk/python/gump/actor/stats/mysql/statsdb.py Wed Sep  9 09:01:32 2009
@@ -65,17 +65,27 @@
         }
 
     def __init__(self, dbInfo):
-        self.conn = MySQLdb.Connect(
-            host = dbInfo.getHost(), 
-            user = dbInfo.getUser(),
-            passwd = dbInfo.getPasswd(), 
-            db = dbInfo.getDatabase(),
+        self._dbinfo = dbInfo
+        self._helper = None
+
+    def _get_helper(self):
+        """
+        Returns a cached DBHelper or creates and caches a new one
+        """
+        if self._helper:
+            return self._helper
+        conn = MySQLdb.Connect(
+            host = self._dbinfo.getHost(), 
+            user = self._dbinfo.getUser(),
+            passwd = self._dbinfo.getPasswd(), 
+            db = self._dbinfo.getDatabase(),
             compress = 1,
             cursorclass = MySQLdb.cursors.DictCursor)
 
         # print 'ThreadSafe : ' + `MySQLdb.threadsafety`
 
-        self.helper = gump.util.mysql.DbHelper(self.conn, dbInfo.getDatabase())
+        self._helper = gump.util.mysql.DbHelper(conn,
+                                                self._dbinfo.getDatabase())
  
     # Workspace
     def getWorkspaceStats(self, workspaceName):
@@ -162,8 +172,10 @@
     def _getStats(self, table_name, column_name, entity_name, stats):
 
         # Select the row settings from the database
-        settings = self.helper.select(table_name, column_name, entity_name,
-                                      StatisticsDB.ATTR_COLUMN_MAP.values())
+        settings = self._with_reconnect_on_error(\
+            lambda helper:
+                helper.select(table_name, column_name, entity_name,
+                              StatisticsDB.ATTR_COLUMN_MAP.values()))
 
         # Extract columns
         self._getBaseStats(stats, settings)
@@ -178,12 +190,16 @@
                 settings[key] = value
 
         # Perform the update (we've ensured a row exists).
-        self.helper.set(table_name, column_name, stats.name, settings)
+        self._with_reconnect_on_error(\
+            lambda helper:
+                helper.set(table_name, column_name, stats.name, settings))
 
     def _delStats(self, table_name, column_name, stats):
         """ Perform an SQL DELETE """
         # Perform the delete
-        self.helper.delete(table_name, column_name, stats.name)
+        self._with_reconnect_on_error(\
+            lambda helper:
+                helper.delete(table_name, column_name, stats.name))
 
     def _getBaseStats(self, stats, settings):
         """
@@ -237,6 +253,18 @@
     def sync(self):
         pass
 
+    def _with_reconnect_on_error(self, command):
+        """
+        Executes command (passing in a DbHelper instance) and retries
+        it with a new instance if an OperationalError occurs
+        """
+        try:
+            return command(self._get_helper())
+        except MySQLdb.OperationalError:
+            log.info("Caught an exception from DB, reconnecting")
+            self._helper = None
+            return command(self._get_helper())
+
 if __name__ == '__main__':
     import logging