def get_rows_by_slave(self, sql, *parameters, **kwparameters): if self._restarting == True: return [] rows = [] try: rows = self._db_slave.query(sql, *parameters, **kwparameters) except Exception, e: Log.critical('Mysql get rows error(%s)(%s)', sql, str(e))
def get_one_by_slave(self, sql, *parameters, **kwparameters): # 目前重连是阻塞状态,所以这个判断不会生效,暂时保留 if self._restarting == True: return None row = None try: row = self._db_slave.get(sql, *parameters, **kwparameters) except Exception, e: Log.critical('Mysql get rows error(%s)(%s)', sql, str(e))
def get_rows(self, sql, *parameters, **kwparameters): if self._restarting == True: return [] rows = [] try: rows = self._db.query(sql, *parameters, **kwparameters) except Exception, e: Log.critical('Mysql get rows error(%s)(%s)', sql, str(e)) if self._db is None or str(e).find('connect to MySQL') >= 0 or str(e).find('MySQL server has gone away') >= 0: self._restart()
def start(cls, host="127.0.0.1", port=27017): cls._host = host cls._port = port #Log.info('Start connect to MongoDB server (%s:%s).', host, port) if cls._client is None: try: cls._client = pymongo.MongoClient(host,port) except Exception, e: Log.critical('Mongodb(%s:%s) connect failed. \n %s', host, port, str(e)) sys.exit() Log.critical('Mongodb(%s:%s) Ok.', host, port)
def execute(self, sql, *parameters, **kwparameters): if self._restarting == True: return [False, 0] ret = [False, 0] try: ret[1] = self._db.execute_lastrowid(sql, *parameters, **kwparameters) ret[0] = True except Exception, e: Log.critical('Mysql Error(%s)(%s)', sql, str(e)) if self._db is None or str(e).find('connect to MySQL') >=0 or str(e).find('MySQL server has gone away') >= 0: self._restart()
def start(cls, host="127.0.0.1", port=27017): cls._host = host cls._port = port #Log.info('Start connect to MongoDB server (%s:%s).', host, port) if cls._client is None: try: cls._client = pymongo.MongoClient(host, port) except Exception, e: Log.critical('Mongodb(%s:%s) connect failed. \n %s', host, port, str(e)) sys.exit() Log.critical('Mongodb(%s:%s) Ok.', host, port)
def start(cls, host=None, port=None, password=None): host = config_base.store['redis']['host'] port = config_base.store['redis']['port'] password = config_base.store['redis']['pwd'] #Log.info('Start connect to Redis server (%s:%s).', host, port) if cls._pool is None: try: cls._pool = redis.ConnectionPool(host=host, port=port, password=password) cls._db = redis.Redis(connection_pool=cls._pool) cls._db.get("test") except Exception, e: Log.critical('Redis(%s:%s) connect failed. \n %s', host, port, str(e)) sys.exit() Log.critical('Redis(%s:%s) Ok.', host, port)
def _get_slave_db(cls, slaves_conf): for db_conf in slaves_conf: try: _db = torndb.Connection( host = '%s:%s' % (db_conf['host'], db_conf['port']), user = db_conf['uname'], password = db_conf['pwd'], database = db_conf['name'], connect_timeout = db_conf['time_out'], time_zone = "+8:00" ) sql = "SET NAMES 'utf8'" _db.execute(sql) Log.critical('Mysql Slave(%s:%s) Ok', db_conf['host'], db_conf['host']) return _db except Exception as e: Log.critical('Mysql Slave Error(%s:%s)(%s)', db_conf['host'], db_conf['host'], str(e)) return cls._db
def start(cls): master_conf = config_base.store['db_master'] debug_flag = config_base.store['db_master']['debug'] slaves_conf = [] if config_base.store.has_key('db_slaves'): slaves_conf = config_base.store['db_slaves'] #host, port, user, password, database, time_out = 3, debug_flag = False if cls._db is None: cls._debug_flag = debug_flag try: cls._db = cls._get_db(master_conf) sql = "SET NAMES 'utf8'" cls._db.execute(sql) Log.critical("Mysql Master(%s:%s) OK" % (master_conf['host'], master_conf['host'])) except Exception, e: cls._db = None Log.critical('Mysql Error(%s:%s)(%s)', master_conf['host'], master_conf['host'], str(e))
def start(cls, host, port): if cls.__mc_conn is None: cls.__mc_conn_str = "%s:%s" % (host, port) try: cls.__mc_conn = memcache.Client([cls.__mc_conn_str]) mc_stats = cls.__mc_conn.get_stats() if mc_stats is None or len(mc_stats) <= 0: Log.critical('memcache(%s) connect failed', cls.__mc_conn_str) sys.exit() except Exception, e: print e Log.critical('memcache connection error(%s)(%s)', cls.__mc_conn_str, str(e)) sys.exit() Log.critical('Memcatched(%s:%s) Ok.', host, port)
sys.exit() Log.critical('Memcatched(%s:%s) Ok.', host, port) @classmethod def __reconnect(cls): if cls.__mc_conn is not None: try: cls.__mc_conn.disconnect_all() except Exception, e: Log.warning('memcache disconnect(%s)', str(e)) cls.__mc_conn = None try: cls.__mc_conn = memcache.Client([cls.__mc_conn_str]) except Exception, e: cls.__mc_conn = None Log.critical('memcache re_connection error(%s)(%s)', cls.__mc_conn_str, str(e)) @classmethod def get(cls, key): val = None try: val = cls.__mc_conn.get(key) except Exception, e: Log.warning('memcache get %s failed(%s)', key, str(e)) cls.__reconnect() try: val = cls.__mc_conn.get(key) except Exception, e1: val = None Log.warning('memcache re-get %s failed(%s)', key, str(e1)) return val