Ejemplo n.º 1
0
    def getMongoConn(self, *args, **kwargs):
        conf = self.conf.get(kwargs.get("link_type", 'default'))
        city = kwargs.get("city")
        host = conf.get("host")
        port = conf.get("port")
        user = conf.get("username", "")
        passwd = conf.get("password", "")
        db_name = kwargs.get("db_name")

        self.db_name = db_name if db_name else get_db(type=self.conf_name,
                                                      city=city)

        if user and passwd:
            uri = "mongodb://{username}:{password}@{host}:{port}".\
                format(username=user, password=passwd, host=host, port= port)
        else:
            uri = "mongodb://{host}:{port}".format(host=host, port=port)
        conn = MongoClient(uri)
        try:
            client = conn.get_database(self.db_name)
            yield client
        except Exception as e:
            logging.error('ERROR', e)
        finally:
            # conn.close()
            pass
Ejemplo n.º 2
0
    def get_conn(self, *args, **kwargs):

        conn = MysqlDB.__pool.get(self.conf_name)
        if conn:
            conn.ping()
        else:
            #获取主从连接
            city = kwargs.get("city")
            conf_name = getConfigName(city=city, type=self.conf_name)
            conf1 = mysql_conf.get(conf_name)
            conf = conf1.get(kwargs.get("link_type", 'default'))
            host = conf.get("host")
            port = conf.get("port")
            user = conf.get("user")
            passwd = conf.get("passwd")
            db_name = get_db(type=conf_name, city=city)
            conn = pymysql.connect(host=host, port=port, user=user, password=passwd, db=db_name, charset="utf8")

        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
        try:
            yield cursor
            conn.commit()
        except Exception as e:
            conn.rollback()
            logging.error(e)
        finally:
            cursor.close()
            conn.close()
Ejemplo n.º 3
0
 def get_conn(self, *args, **kwargs):
     city = kwargs.get("city")
     self.conf_name = getConfigName(city=city, type=self.conf_name)
     conf = self.conf.get(kwargs.get("link_type", 'default'))
     # 获取主从连接
     host = conf.get("host")
     port = conf.get("port")
     user = conf.get("user")
     passwd = conf.get("passwd")
     db_name = get_db(type=self.conf_name, city=city)
     conn = pymysql.connect(host=host,
                            port=port,
                            user=user,
                            password=passwd,
                            db=db_name,
                            charset="utf8")
     cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
     try:
         yield cursor
         logging.info(cursor._last_executed)
     except Exception as e:
         logging.error(e)
     finally:
         conn.commit()
         cursor.close()
         conn.close()
Ejemplo n.º 4
0
    def get_connection(self, *args, **kwargs):
        # 获取主从连接
        conf = self.conf.get(kwargs.get("link_type", 'default'))
        city = kwargs.get("city")
        host = conf.get("host")
        port = conf.get("port")
        user = conf.get("user")
        passwd = conf.get("passwd")

        db_name = get_db(type=self.conf_name, city=city)
        conn = pymysql.connect(host=host, port=port, user=user, password=passwd, db=db_name, charset="utf8")
        return conn