def buildProtocol(self, addr): self.resetDelay() self.proto = memcache.MemCacheProtocol() if self.deferred: reactor.callLater(0, self.deferred.callback, self.proto) del self.deferred self.deferred = None singleton.store('memCacheClient', self.proto) return self.proto
def establishConnection(connID, hostname, port, username, password, database, minPoolCons, maxPoolCons, sslEnabled=False, sslCACert='', useServerCursor=False, dbProxySecHosts=None): """I establish a connection to either: - A mySQL SQL server using twisted's ADBAPI connection pool semantics. - A dbproxy server using twisted perspective broker (PB). @param connectType: One of the following: - 'direct': Establish a connection to a mySQL SQL server using twisted's ADBAPI connection pool semantics. - 'dbproxy': Establish a connection to a dbproxy server using twisted perspective broker (PB). @param connID: A key to use that uniquely identifies this connection (pool). It is stored using our singleton interface, so if you make a pool with an ID of 'dbMyPool', you can get that connection object by using "singleton.get('dbMyPool'). Let's use the convention where every db pool ID must start with 'db' to avoid clashes with other objects stored in the singleton. @type connID: str @param hostname: The hostname or IP address of the database server. @type hostname: str @param username: The username used to connect to the database. @type username: str @param port: The port used to connect to the database. @type port: int @param password: The password used to connect to the database. @type password: str @param database: The default database name that we USE once connected. @type database: str @param minPoolCons: The minimum number of connections to keep open to the database as part of the connection pool. @type minPoolCons: int @param maxPoolCons: The maximum number of connections to keep open to the database as part of the connection pool. @type maxPoolCons: int """ assert isinstance(connID, basestring) and len(connID) >= 2 and connID[0:2] == 'db' assert hostname and isinstance(hostname, basestring) assert port and isinstance(port, int) assert minPoolCons and isinstance(minPoolCons, int) assert maxPoolCons and isinstance(maxPoolCons, int) assert (sslEnabled and sslCACert) or not sslEnabled d = None assert database and isinstance(database, basestring) assert username and isinstance(username, basestring) assert password and isinstance(password, basestring) #connect directly to mysql database #for possible SSL support sslDict = { 'ca': sslCACert, 'capath': None, 'key': None, 'cert': None, 'cipher': None, } cursorClassDict = {} if useServerCursor: from MySQLdb.cursors import SSCursor cursorClassDict['cursorclass'] = SSCursor #try: connPool = adbapi.ConnectionPool("pyPgSQL.PgSQL", database=database,user=username, password=password, host=hostname, port=port) #user=username, passwd=password, #db=database, cp_min=minPoolCons, cp_max=maxPoolCons) #use_unicode=True, charset='utf8', ssl=sslEnabled and sslDict or None, #cp_noisy=False, #cp_reconnect=True, poolName=connID, #**cursorClassDict) #except: #errorutil.triggerFatalError("Could not connect to database for pool \"%s\"" % connID) d = defer.succeed(True) singleton.store(connID + 'Type', 'direct') singleton.store(connID, connPool) return d