Permalink
Browse files

added a reconnecting mysql connection pool

  • Loading branch information...
1 parent a749698 commit c9376fe6add1cb47b47d3d3f31433ac9349c2935 @bmuller committed Mar 29, 2012
Showing with 20 additions and 5 deletions.
  1. +20 −5 twistar/dbconfig/mysql.py
View
@@ -1,3 +1,8 @@
+import MySQLdb
+
+from twisted.enterprise import adbapi
+from twisted.python import log
+
from twistar.dbconfig.base import InteractionBase
@@ -10,8 +15,18 @@ def insertArgsToString(self, vals):
return "VALUES ()"
-
-
-
-
-
+class ReconnectingMySQLConnectionPool(adbapi.ConnectionPool):
+ """
+ This connection pool will reconnect if the server goes away. This idea was taken from:
+ http://www.gelens.org/2009/09/13/twisted-connectionpool-revisited/
+ """
+ def _runInteraction(self, interaction, *args, **kw):
+ try:
+ return adbapi.ConnectionPool._runInteraction(self, interaction, *args, **kw)
+ except MySQLdb.OperationalError, e:
+ if e[0] not in (2006, 2013):
+ raise
+ log.err(None, "Lost connection to MySQL with error '%s', retrying operation" % e)
+ conn = self.connections.get(self.threadID())
+ self.disconnect(conn)
+ return adbapi.ConnectionPool._runInteraction(self, interaction, *args, **kw)

0 comments on commit c9376fe

Please sign in to comment.