[Twisted-Python] OT - adbapi, connection timeouts, mysql - OT

Werner Thie wthie at thiengineering.ch
Tue Jul 21 15:05:04 MDT 2009


Hi Gabriel

had the same problem, solved it by having keepalive() called in a 
LoopingCall(), MySQL sitting at defaults timingwise.

DB_DRIVER = "MySQLdb"

USERDB_ARGS = {
   'host': '',
   'db': '',
   'user': '',
   'passwd': '',
   'cp_reconnect': True
}

storekeeper = StoreKeeper(DB_DRIVER, **USERDB_ARGS)

ka = task.LoopingCall(storekeeper.store.keepAlive)
ka.start(300)

class StoreKeeper(object):
   def __init__(self, dbapiName, **params):
     self.store = Store(dbapiName, **params)

   def dbdisconn(self, reason):
     print 'db disconnected for ', reason

   def keepAlive(self):
     d = self.store.runQuery('SELECT 1')
     d.addErrback(self.dbdisconn)


#with store being something like:

class Store(object):
   def __init__(self, dbapiName, **params):
     self.__pool   = adbapi.ConnectionPool(dbapiName, **params)
     print self.__pool.__getstate__()
     self.runOperation('SET autocommit = %s', 1)

   def runQuery(self, query, *args):
     d = self.__pool.runInteraction(self.mapQuery, query, args)
     return d

   def mapQuery(self, curs, query, *args):
     try:
       curs.execute(query, *args)
     except adbapi.ConnectionLost:
       print
       print '++++++++++++ rerunning query'
       print
       curs.execute(query, *args)                    #simply resend 
query, assuming cp_reconnect=True
     result = curs.fetchall()
     columns = [d[0] for d in curs.description]
     return [dict(zip(columns, r)) for r in result]

   def runOperation(self, query, *args):
     d = self.__pool.runOperation(query, args)
     return d

   def runInteraction(self, fun, queries=(), args=()):
     d = self.__pool.runInteraction(fun, queries, args)
     return d


HTH, Werner

Gabriel Rossetti wrote:
> Hello everyone,
> 
> I have been experiencing the ConnectionError with adbapi & 
> cp_reconnect=True. I know that because of the cp_reconnect=True param 
> tha is reconnects and that the query is not re-run. I have written some 
> code that should re-run the query in that case (if I get a Failure back 
> because of a ConnectionError), but it doesn't seem to work. My question 
> is if anyone knows how to make mysql's idle timeouts shorter so that I 
> can debug my code? I searched google and the mysql site with no luck.
> 
> thank you,
> Gabriel
> 
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python





More information about the Twisted-Python mailing list