Ejemplo n.º 1
0
class SQLManager(object):
    def __init__(self):
        self.connect_to_database()

    def connect_to_database(self):
        DB_NAME = settings.get_database()

        self.connpool = MySQLConnectionPool(user=settings.DatabaseConfig.USERNAME,
                                            password=settings.DatabaseConfig.PASSWORD,
                                            host=settings.DatabaseConfig.HOST,
                                            pool_name="order_manager",
                                            pool_size=5,
                                            autocommit=True,
                                            database=DB_NAME
                                            )
        self.cnx1 = self.connpool.get_connection()
        self.cur1 = self.cnx1.cursor()
        self.cnx2 = self.connpool.get_connection()
        self.cur2 = self.cnx2.cursor()
        self.cnx3 = self.connpool.get_connection()
        self.cur3 = self.cnx3.cursor()
        self.cnx4 = self.connpool.get_connection()
        self.cur4 = self.cnx4.cursor()
        self.cnx5 = self.connpool.get_connection()
        self.cur5 = self.cnx5.cursor()
 def __init__(self):
     if self.INSTANCE is not None:
         raise ValueError("An instantiation already exists!")
     else:
         db_config = read_db_config(filename='Config.ini', section='mysql')
         print(type(db_config), db_config)
         self.__cnxPool = MySQLConnectionPool(pool_name='myPool', pool_size=5, **db_config)
Ejemplo n.º 3
0
 def __init__(self):
     if self.INSTANCE is not None:
         raise ValueError("An instantiation already exists!")
     else:
         self.__cnxPool = MySQLConnectionPool(pool_name='myPool',
                                              pool_size=5,
                                              option_files='db_config.conf')
Ejemplo n.º 4
0
 def __init__(self):
     """Mysqldriver Constructor."""
     self.config = {
         "host":
         "db-espresso-dev" if environ.get("APP_ENV") == "development" else
         environ.get("MYSQL_HOST"),
         "database":
         environ.get("MYSQL_DATABASE", "espresso_db"),
         "user":
         environ.get("MYSQL_USER", "root"),
         "password":
         environ.get("MYSQL_ROOT_PASSWORD", "strawberry"),
         "port":
         int(environ.get("MYSQL_PORT", 3306)),
         "pool_name":
         "mysql_pool",
         "pool_size":
         5,
         "pool_reset_session":
         False,  # MySQL version 5.7.2 and earlier does not support COM_RESET_CONNECTION.
     }
     try:
         self.cnxpool = MySQLConnectionPool(**self.config)
     except errors.Error as err:
         print(err)
Ejemplo n.º 5
0
 def _init_pool(self):
     logger.info("Connecting pool to DB")
     self.pool_mutex.acquire()
     self.pool = MySQLConnectionPool(
         pool_name="db_wrapper_pool",
         pool_size=self.application_args.db_poolsize,
         **self.dbconfig)
     self.pool_mutex.release()
Ejemplo n.º 6
0
 def __init__(self):
     if self.INSTANCE is not None:
         raise ValueError("An instantiation already exists!")
     else:
         db_config = read_db_config()
         self.__cnxPool = MySQLConnectionPool(pool_name="myPool",
                                              pool_size=5,
                                              **db_config)
Ejemplo n.º 7
0
 def __init__(self, filename='../resources/db_properties.ini'):
     if self.INSTANCE is not None:
         raise ValueError("An instantiation already exists!")
     else:
         db_config = read_db_config(filename)
         self.__cnxPool = MySQLConnectionPool(pool_name="myPool",
                                              pool_size=10,
                                              **db_config)
Ejemplo n.º 8
0
    def __init__(self, broker_cloud, mode, db_config, time_inactive_platform, time_update_conf, time_check_platform_active):
        self.time_update_conf = time_update_conf
        self.time_check_platform_active = time_check_platform_active
        self.time_inactive_platform = time_inactive_platform
        self.cnxpool = MySQLConnectionPool(pool_name="mypool", pool_size=32, **db_config)
        self.mode = mode

        self.producer_connection = Connection(broker_cloud)
        self.consumer_connection = Connection(broker_cloud)

        self.exchange = Exchange("IoT", type="direct")
Ejemplo n.º 9
0
    def __init__(self, config):
        """Constructor to initialize the connection pool.
            
        Args:
            config (dict): Connection configuration for database.

        """
        self._config = config
        self._db_pool = MySQLConnectionPool(pool_name="disc_pool",
                                            pool_size=_POOL_SIZE,
                                            **config)
Ejemplo n.º 10
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     # self._conn_params['use_pure'] = True
     self._conn_params['pool_name'] = 'test_pool'
     self._conn_params['pool_size'] = 10
     try:
         self._pool = MySQLConnectionPool(**self._conn_params)
         self._closed = False
     except InterfaceError as e:
         log.error('MysqlPooledDataSource connect error')
         log.error(formatErrorMsg(e))
         raise e
Ejemplo n.º 11
0
 def _init_pool(self):
     logger.info("Connecting to DB")
     dbconfig = {
         "host": self.host,
         "port": self.port,
         "user": self.user,
         "password": self.password,
         "database": self.database
     }
     with self._pool_mutex:
         self._pool = MySQLConnectionPool(pool_name="db_wrapper_pool",
                                          pool_size=self._poolsize,
                                          **dbconfig)
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self._conn_params['use_pure'] = True
     self._conn_params['pool_name'] = 'test_pool'
     self._conn_params['pool_size'] = 10
     # self._pool = MySQLConnectionPool(**{'user':self._user_name,'password':self._pass_word,\
     #                                     'host':self._host,'port':self._port,'database':self._data_base,\
     #                                     'use_pure':True,'pool_name':"test_pool",'pool_size':10})
     try:
         self._traceback = None
         self._pool = MySQLConnectionPool(**self._conn_params)
     except InterfaceError as e:
         print('MysqlPooledDataSource connect error')
         e.with_traceback(self._traceback)
         raise e
Ejemplo n.º 13
0
 def _init_pool(self):
     logger.info("Connecting to DB")
     dbconfig = {
         "host": self.host,
         "port": self.port,
         "user": self.user,
         "password": self.password,
         "database": self.database,
         "time_zone": "America/Los_Angeles"
     }
     self._pool_mutex.acquire()
     self._pool = MySQLConnectionPool(pool_name="db_wrapper_pool",
                                      pool_size=self._poolsize,
                                      **dbconfig)
     self._pool_mutex.release()
Ejemplo n.º 14
0
class DatabaseConnectionPool(object):
    INSTANCE = None

    def __init__(self, filename='../resources/db_properties.ini'):
        if self.INSTANCE is not None:
            raise ValueError("An instantiation already exists!")
        else:
            db_config = read_db_config(filename)
            self.__cnxPool = MySQLConnectionPool(pool_name="myPool",
                                                 pool_size=10,
                                                 **db_config)

    @classmethod
    def get_instance(
        cls,
        filename='../resources/db_properties.ini',
    ):
        print("get_instance() ------------", filename)
        if cls.INSTANCE is None:
            cls.INSTANCE = DatabaseConnectionPool(filename)
        return cls.INSTANCE

    def get_connection(self):
        return self.__cnxPool.get_connection()

    @classmethod
    def pool_close(cls):
        cls.INSTANCE = None
Ejemplo n.º 15
0
class DataSource:
    def __init__(self,
                 host='127.0.0.1',
                 port='3306',
                 user='******',
                 password='',
                 database='jground',
                 _pool_name='remote_query',
                 _pool_size=10):

        self.pool = MySQLConnectionPool(pool_size=_pool_size,
                                        host=host,
                                        port=port,
                                        user=user,
                                        password=password,
                                        database=database,
                                        pool_reset_session=True,
                                        autocommit=True,
                                        charset='utf8mb4')

    def getConnection(self):
        return self.pool.get_connection()

    def returnConnection(self, con):
        con.close()
Ejemplo n.º 16
0
class MySQLPooledConnection(MySQLConnectionABC):
    """ MySQL Connection from connection-pool """
    def __init__(self,
                 *args,
                 db_schema: DBSchema,
                 dictionary: bool = False,
                 named_tuple: bool = False,
                 pool_size: int = 16,
                 **kwargs):
        print('MySQLPooledConnection: Init with pool_size=%d' % pool_size)
        self.cnx_pool = MySQLConnectionPool(pool_size=pool_size,
                                            *args,
                                            **kwargs)
        super().__init__(*args,
                         db_schema=db_schema,
                         dictionary=dictionary,
                         named_tuple=named_tuple,
                         **kwargs)

    def new_cnx(self, *args, **kwargs):
        return self.cnx_pool.get_connection()

    def create_cursor(self, *args, **options) -> 'MySQLCursor':
        self.close()
        self.cnx = self.new_cnx()
        return MySQLCursor(
            self, self.cnx.cursor(*args, **self._cursor_options(**options)))
Ejemplo n.º 17
0
 def __init__(self,
              *args,
              db_schema: DBSchema,
              dictionary: bool = False,
              named_tuple: bool = False,
              pool_size: int = 16,
              **kwargs):
     print('MySQLPooledConnection: Init with pool_size=%d' % pool_size)
     self.cnx_pool = MySQLConnectionPool(pool_size=pool_size,
                                         *args,
                                         **kwargs)
     super().__init__(*args,
                      db_schema=db_schema,
                      dictionary=dictionary,
                      named_tuple=named_tuple,
                      **kwargs)
Ejemplo n.º 18
0
class MySQL(object):
    def __init__(self):
        self._cnxpool = MySQLConnectionPool(pool_name="mysqlPool",
                                            pool_size=3,
                                            **Config.dbconfig)

    def _getConnection(self):
        return self._cnxpool.get_connection()

    def query(self, query, queryArgsTuple=None):
        cnx = self._getConnection()

        with closing(cnx.cursor()) as cursor:
            cursor.execute(query, queryArgsTuple)
            results = cursor.fetchall()

        cnx.close()

        return results

    def modify(self, query, queryArgsTuple=None):
        cnx = self._getConnection()

        with closing(cnx.cursor()) as cursor:
            cursor.execute(query, queryArgsTuple)
            cnx.commit()

        cnx.close()
Ejemplo n.º 19
0
 def create_mysql(self, database=None):
     global MYSQL, DATABASE, mysqlPool
     config = self.configs[self.MYSQL]
     hosts, port, user, password = self.get_params(config)
     if not database:
         database = config[self.DATABASE]
     dbconfig = {
         'database': database,
         'user': user,
         'password': password,
         'host': hosts,
         'port': port
     }
     self.mysqlPool = MySQLConnectionPool(pool_size=CNX_POOL_MAXSIZE,
                                          pool_name='POOL',
                                          **dbconfig)
Ejemplo n.º 20
0
def test():
    from mysql.connector.pooling import MySQLConnectionPool
    from config.db_config import mysql_config as db_conf

    cnx_pool = MySQLConnectionPool(
        pool_name='default_cnx_pool', **db_conf)
    model = DbModel(cnx_pool)
Ejemplo n.º 21
0
def test():
    from mysql.connector.pooling import MySQLConnectionPool
    from config.db_config import mysql_config as db_conf

    cnx_pool = MySQLConnectionPool(pool_name='default_cnx_pool', **db_conf)
    model = ModelExchange(cnx_pool)

    print(model.get_exchange_dict())
Ejemplo n.º 22
0
class Database:

    dbconfig = {"database": "pythontest", "user": "******", "password": "******"}

    conn = MySQLConnectionPool(pool_name="mypool", pool_size=2, **dbconfig)

    def getConn(self):
        return self.conn.get_connection()
Ejemplo n.º 23
0
def test():
    from mysql.connector.pooling import MySQLConnectionPool
    from config.db_config import mysql_config as db_conf

    cnx_pool = MySQLConnectionPool(pool_name='default_cnx_pool', **db_conf)
    model = ModelTrCoinone(cnx_pool)
    #model.insert_tr(1, [{'price':355, 'qty':10, 'datetime':datetime.now() },{'price':355, 'qty':12, 'datetime':datetime.now() }] )
    print(model.get_tr(1))
Ejemplo n.º 24
0
def dbconfigload(db_name, user_name, port, password, host_name, pool_name):
    dbconfig = {}
    dbconfig["database"] = db_name
    dbconfig["user"] = user_name
    dbconfig["port"] = port
    dbconfig["password"] = password
    dbconfig["host"] = host_name
    return MySQLConnectionPool(pool_name=pool_name, pool_size=3, **dbconfig)
Ejemplo n.º 25
0
class Event(Flask):
    def __init__(self, *args, **kwargs):
        kwargs.update({
            'template_folder': settings.STATIC_ASSETS_PATH,
            'static_folder': settings.STATIC_ASSETS_PATH,
            'static_url_path': '',
        })
        super(Event, self).__init__(__name__, *args, **kwargs)
        self.connection_pool = None
        self.enums = None

    def create_connection_pool(self):
        pool_config = {
            "host": settings.EVENT_MYSQL_DATABASE_HOST,
            "port": settings.EVENT_MYSQL_DATABASE_PORT,
            "database": settings.EVENT_MYSQL_DATABASE_NAME,
            "user": settings.EVENT_MYSQL_DATABASE_USER,
            "password": settings.EVENT_MYSQL_DATABASE_PASSWORD,
            "charset": settings.EVENT_MYSQL_DATABASE_CHARSET,
            "use_unicode": True,
            "get_warnings": True,
            "ssl_verify_cert": False,
            "pool_name": self.__class__.__name__,
            "pool_reset_session":
            settings.EVENT_MYSQL_DATABASE_POOL_RESET_SESSION,
            "pool_size": settings.EVENT_MYSQL_DATABASE_POOL_SIZE,
            "autocommit": True
        }
        if settings.EVENT_MYSQL_DATABASE_SSL_VERIFY_CERT:
            pool_config["ssl_verify_cert"] = True
            pool_config["ssl_ca"] = settings.EVENT_MYSQL_DATABASE_SSL_CA_PATH
        self.connection_pool = MySQLConnectionPool(**pool_config)

    def load_enums(self):
        sql = 'SELECT * FROM c_enum'
        cnx = self.connection_pool.get_connection()
        enum_list = execute_sql_statement(sql, sql_cnx=cnx)
        enum_map = defaultdict(list)
        enum_obj_map = {}
        for enum in enum_list:
            enum_map[enum.get("enum_type_name")].append(enum)
        value_tuple = namedtuple("VALUE", ["value", "name"])
        for enum_name_code, enum_value_list in enum_map.items():
            value_map = {
                enum_value.get("enum_code"):
                value_tuple(enum_value.get("enum_value"),
                            enum_value.get("enum_name"))
                for enum_value in enum_value_list
            }
            temp_named_tuple = namedtuple(enum_name_code,
                                          list(value_map.keys()))
            enum_obj_map[enum_name_code.upper()] = temp_named_tuple(
                **value_map)
        enum_type_tuple = namedtuple("ENUM", enum_obj_map.keys())
        self.enums = enum_type_tuple(**enum_obj_map)
        cnx.close()
        REGISTRATION_TYPE_LIST.extend(
            self.enums.REGISTRATION_TYPE._asdict().keys())
Ejemplo n.º 26
0
    def __init__(self,
                 host='127.0.0.1',
                 port='3306',
                 user='******',
                 password='',
                 database='jground',
                 _pool_name='remote_query',
                 _pool_size=10):

        self.pool = MySQLConnectionPool(pool_size=_pool_size,
                                        host=host,
                                        port=port,
                                        user=user,
                                        password=password,
                                        database=database,
                                        pool_reset_session=True,
                                        autocommit=True,
                                        charset='utf8mb4')
Ejemplo n.º 27
0
	def connectDbPool(self,POOLSIZE=1): 
		try:
			if PRINTLEVEL>=INFO_PRINT: print "MySQLConnectionPool connectDbPool ..."
			cnxpool=MySQLConnectionPool(pool_name=self.config['host'],pool_size=POOLSIZE,**self.config)
			self.pool=cnxpool
			return 1
		except Exception, e:
			print "! Failed connectDbPool !"
			if PRINTLEVEL>=ERROR_PRINT: print(e)
Ejemplo n.º 28
0
def init_connection_pool(user, password, database, host, port):
    global CONNECTION_POOL
    CONNECTION_POOL = MySQLConnectionPool(pool_name="opendcpool",
                                          pool_size=5,
                                          user=user,
                                          password=password,
                                          database=database,
                                          host=host,
                                          port=port)
Ejemplo n.º 29
0
 def get_connection_pool():
     MysqlDatabase.pool = MySQLConnectionPool(pool_name="connection_pool",
                                              pool_size=32,
                                              pool_reset_session=True,
                                              host=HOST,
                                              port=PORT,
                                              database=DATABASE,
                                              user=USER,
                                              password=PASSWORD)
Ejemplo n.º 30
0
 def connect_database(self, **kwargs) -> MySQLConnectionPool:
     print("[DEBUG] set connection pool configuration configure: ", kwargs)
     for key, value in kwargs.items():
         # self.__MySQL_Stock_Data_Config[key] = value
         self._Database_Config[key] = value
     print("[DEBUG] database config: ", self._Database_Config)
     connection_pool = MySQLConnectionPool(**self._Database_Config)
     self._logger.debug(
         f"MySQL_Connection_Pool at 'connection_strategy.init_connection_pool': {connection_pool}"
     )
     return connection_pool
Ejemplo n.º 31
0
class Database(object, metaclass=Singleton):
    def __init__(self):
        cfg = self.__get_cfg()
        self.pool = MySQLConnectionPool(pool_name='dbpool',
                                        pool_size=5,
                                        **cfg)

    def __get_cfg(self):
        with open('database.yml') as f:
            cfg = yaml.load(f.read())['production']

        keys = ['username', 'password', 'host', 'port', 'database', 'ssl_ca',
                'ssl_cert', 'ssl_key']

        no_ = lambda s: s.replace('_', '')
        cfg = { k: cfg.get(no_(k)) for k in keys if no_(k) in cfg }
        cfg['ssl_verify_cert'] = True
        # Mysql documentation incorrectly says 'username' is an alias of 'user'.
        cfg['user'] = cfg.pop('username')
        return cfg

    @contextmanager
    def get_connection(self):
        def get_conn(attempt=0):
            try:
                return self.pool.get_connection()
            except PoolError:
                if 20 > attempt:
                    time.sleep(0.2)
                    return get_conn(attempt + 1)
                else:
                    raise
        connection = get_conn()
        yield connection
        # connection.close() will fail with an unread_result.
        if connection._cnx.unread_result:
            connection._cnx.get_rows()
        connection.close()

    def close_all(self):
        self.pool.reset_session()
Ejemplo n.º 32
0
    def __init__(self):

        dbconfig = {
            "database": "geoDev",
            "user": "******",
            "password": "******",
            "host": "localhost",
            #"raw": True,
            "pool_name": "geo_pool",
            "pool_size": 20,
            "pool_reset_session": True
        }

        try:
            self.__conn_pool = MySQLConnectionPool(**dbconfig)
            #self.__conn_pool = mysql.connect(**dbconfig)
        except Exception:
            raise
Ejemplo n.º 33
0
class Db(object):
    """
    Instantiate a db connection.
    """

    def __init__(self):

        dbconfig = {
            "database": "geoDev",
            "user": "******",
            "password": "******",
            "host": "localhost",
            #"raw": True,
            "pool_name": "geo_pool",
            "pool_size": 20,
            "pool_reset_session": True
        }

        try:
            self.__conn_pool = MySQLConnectionPool(**dbconfig)
            #self.__conn_pool = mysql.connect(**dbconfig)
        except Exception:
            raise

    def __get_session(self):
        """
        Returns the private session var.
        """
        return self.__conn_pool.get_connection()
        #return self.__conn_pool

    def __cant_set(self):
        """Raises runtime error."""
        raise RuntimeError("Private property cannot be set.")

    def __cant_get(self):
        """Raises runtime error."""
        raise RuntimeError("Cannot get protected property.")

    db_conn = property(__cant_get, __cant_set)
    session = property(__get_session, __cant_set)
Ejemplo n.º 34
0
    def __init__(self, debug=False):
        if debug:
            logging.info('Initializing The Flash Server as debug mode')
        else:
            logging.info('Initializing The Flash Server as production mode')

        self.debug = debug

        self.app = Flask(__name__)
        logging.info('Flask Initialized')

        dbconfig = { "user" : settings.db_user_name,
                     "password" : settings.db_password,
                     "database" : settings.db_name,
                     "host" : settings.db_host
                     }
        self.mysql_pool = MySQLConnectionPool(pool_name=None, pool_size=4, pool_reset_session=True, **dbconfig)
        logging.info('Database Initialized')

        self.redis_pool = redis.ConnectionPool(host=settings.redis_host, port=settings.redis_port, db=0)
        logging.info('Redis Initialized')

        self.application = tornado.web.Application([
            (r"/auth", RequestAuth, dict(mysql_pool=self.mysql_pool, redis_pool=self.redis_pool)),
            (r"/get_users", RequestGetUsers, dict(mysql_pool=self.mysql_pool, redis_pool=self.redis_pool)),
            (r"/get_beacons", RequestGetBeacons, dict(mysql_pool=self.mysql_pool, redis_pool=self.redis_pool)),
            (r"/update_user_pos", RequestUpdateUserPosition, dict(mysql_pool=self.mysql_pool, redis_pool=self.redis_pool)),
            (r"/register_beacon", RequestRegisterBeacon, dict(mysql_pool=self.mysql_pool, redis_pool=self.redis_pool)),
            (r"/unregister_beacon", RequestUnregisterBeacon, dict(mysql_pool=self.mysql_pool, redis_pool=self.redis_pool)),
            (r"/", RequestPage, dict(mysql_pool=self.mysql_pool, redis_pool=self.redis_pool, page_name="index.html")),
            (r"/manage", RequestPage, dict(mysql_pool=self.mysql_pool, redis_pool=self.redis_pool, page_name="manage.html")),
            (r"/about", RequestPage, dict(mysql_pool=self.mysql_pool, redis_pool=self.redis_pool, page_name="about.html")),
        ],
            debug=debug,
            autoreload=debug,
            static_path=settings.static_path
        )
        logging.info('Application Initialized')

        self.init_cache()
Ejemplo n.º 35
0
 def __init__(self):
     cfg = self.__get_cfg()
     self.pool = MySQLConnectionPool(pool_name='dbpool',
                                     pool_size=5,
                                     **cfg)
Ejemplo n.º 36
0
class TheFlashServer:
    def __init__(self, debug=False):
        if debug:
            logging.info('Initializing The Flash Server as debug mode')
        else:
            logging.info('Initializing The Flash Server as production mode')

        self.debug = debug

        self.app = Flask(__name__)
        logging.info('Flask Initialized')

        dbconfig = { "user" : settings.db_user_name,
                     "password" : settings.db_password,
                     "database" : settings.db_name,
                     "host" : settings.db_host
                     }
        self.mysql_pool = MySQLConnectionPool(pool_name=None, pool_size=4, pool_reset_session=True, **dbconfig)
        logging.info('Database Initialized')

        self.redis_pool = redis.ConnectionPool(host=settings.redis_host, port=settings.redis_port, db=0)
        logging.info('Redis Initialized')

        self.application = tornado.web.Application([
            (r"/auth", RequestAuth, dict(mysql_pool=self.mysql_pool, redis_pool=self.redis_pool)),
            (r"/get_users", RequestGetUsers, dict(mysql_pool=self.mysql_pool, redis_pool=self.redis_pool)),
            (r"/get_beacons", RequestGetBeacons, dict(mysql_pool=self.mysql_pool, redis_pool=self.redis_pool)),
            (r"/update_user_pos", RequestUpdateUserPosition, dict(mysql_pool=self.mysql_pool, redis_pool=self.redis_pool)),
            (r"/register_beacon", RequestRegisterBeacon, dict(mysql_pool=self.mysql_pool, redis_pool=self.redis_pool)),
            (r"/unregister_beacon", RequestUnregisterBeacon, dict(mysql_pool=self.mysql_pool, redis_pool=self.redis_pool)),
            (r"/", RequestPage, dict(mysql_pool=self.mysql_pool, redis_pool=self.redis_pool, page_name="index.html")),
            (r"/manage", RequestPage, dict(mysql_pool=self.mysql_pool, redis_pool=self.redis_pool, page_name="manage.html")),
            (r"/about", RequestPage, dict(mysql_pool=self.mysql_pool, redis_pool=self.redis_pool, page_name="about.html")),
        ],
            debug=debug,
            autoreload=debug,
            static_path=settings.static_path
        )
        logging.info('Application Initialized')

        self.init_cache()

    def init_cache(self):
        conn = None
        cursor = None

        try:
            conn = self.mysql_pool.get_connection()
            cursor = conn.cursor()

            beacon_cache(conn, cursor, self.redis_pool)
            user_cache(conn, cursor, self.redis_pool)

        except Exception as e:
            logging.error(str(e))
            raise

        finally:
            if cursor is not None:
                cursor.close()
            if conn is not None:
                conn.close()

    def start(self):
        logging.info('The Flash Server Start')

        server = tornado.httpserver.HTTPServer(self.application)
        if self.debug:
            print '- Start as debug mode'
            server.listen(settings.service_port)
        else:
            server.bind(settings.service_port)
            server.start(0)

        IOLoop.current().start()
Ejemplo n.º 37
0
 def __init__(self, **kwargs):
     self.pool = MySQLConnectionPool(**kwargs)
Ejemplo n.º 38
0
class MySQLStorage(object):
    DoesNotExist = DoesNotExist

    def __init__(self, **kwargs):
        self.pool = MySQLConnectionPool(**kwargs)

    def get_list_by_path(self, path):
        con = None
        cur = None

        try:
            con = self.pool.get_connection()

            cur = con.cursor(cursor_class=MySQLCursorDict)
            cur.execute('SELECT * FROM rules WHERE path=%s', (path,))

            rows = cur.fetchall()
            if not rows:
                raise DoesNotExist(
                    'Rule with path={path} does not exist.'.format(path=path)
                )

            return [_rule_from_row(row) for row in rows]
        finally:
            if cur is not None:
                cur.close()
            if con is not None:
                con.close()

    def get_by_id(self, _id):
        con = None
        cur = None

        try:
            con = self.pool.get_connection()
            cur = con.cursor(cursor_class=MySQLCursorDict)
            cur.execute('SELECT * FROM rules WHERE id=%s', (_id,))

            row = cur.fetchone()
            if not row:
                raise DoesNotExist(
                    'Rule with id={id} does not exist.'.format(id=_id)
                )

            return _rule_from_row(row)
        finally:
            if cur is not None:
                cur.close()
            if con is not None:
                con.close()

    def get_list(self):
        con = None
        cur = None

        try:
            con = self.pool.get_connection()
            cur = con.cursor(cursor_class=MySQLCursorDict)
            cur.execute('SELECT * FROM `rules`')

            rows = cur.fetchall()

            return [_rule_from_row(row) for row in rows]
        finally:
            if cur is not None:
                cur.close()
            if con is not None:
                con.close()

    def create(self, rule):
        sql = 'INSERT INTO `rules` (`path`, `request`, `response`)' \
              'VALUES (%s, %s, %s)'

        con = None
        cur = None

        try:
            con = self.pool.get_connection()
            cur = con.cursor(cursor_class=MySQLCursorDict)

            cur.execute(sql, _row_from_rule(rule))
            rule['id'] = cur.lastrowid

            con.commit()

            return rule
        except:
            if con is not None:
                con.rollback()

            raise
        finally:
            if cur is not None:
                cur.close()
            if con is not None:
                con.close()

    def delete_by_id(self, _id):
        con = None
        cur = None

        try:
            con = self.pool.get_connection()
            cur = con.cursor()
            cur.execute('DELETE FROM rules WHERE id=%s', (_id,))
            con.commit()

            if cur.rowcount == 0:
                raise DoesNotExist(
                    'Rule with id={id} does not exist.'.format(id=_id)
                )
        except:
            if con is not None:
                con.rollback()

            raise
        finally:
            if cur is not None:
                cur.close()
            if con is not None:
                con.close()

    def clear(self):
        con = None
        cur = None

        try:
            con = self.pool.get_connection()
            cur = con.cursor()
            cur.execute('TRUNCATE TABLE `rules`')
            con.commit()
        except:
            if con is not None:
                con.rollback()

            raise
        finally:
            if cur is not None:
                cur.close()
            if con is not None:
                con.close()