def get_from_pools(self, **kwargs): arg_str = str(kwargs) if self._pools.get(arg_str) is None: self._pools[arg_str] = db_pool.ConnectionPool(MySQLdb, **kwargs) pool = self._pools[arg_str] return pool.get()
def __init__(self, host, database, user=None, password=None): args = dict(conv=CONVERSIONS, use_unicode=True, charset="utf8", db=database, init_command='SET time_zone = "+0:00"', sql_mode="TRADITIONAL") if user is not None: args['user'] = user if password is not None: args['passwd'] = password if '/' in host: args['unix_socket'] = host else: self.socket = None pair = host.split(":") if len(pair) == 2: args["host"] = pair[0] args["port"] = int(pair[1]) else: args["host"] = host args["port"] = 3306 from eventlet import db_pool self._pool = db_pool.ConnectionPool(MySQLdb, **args)
def configure_db(): """Configure database. Establish the database, create an engine if needed, and register the models. """ global _ENGINE if not _ENGINE: sql_connection = cfg.CONF.DATABASE.sql_connection if not sql_connection: LOG.warn( _("Option 'sql_connection' not specified " "in any config file - using default " "value '%s'") % SQL_CONNECTION_DEFAULT) sql_connection = SQL_CONNECTION_DEFAULT connection_dict = sql.engine.url.make_url(sql_connection) engine_args = { 'pool_recycle': 3600, 'echo': False, 'convert_unicode': True, } if cfg.CONF.DATABASE.sqlalchemy_pool_size is not None: pool_size = cfg.CONF.DATABASE.sqlalchemy_pool_size engine_args['pool_size'] = pool_size if cfg.CONF.DATABASE.sqlalchemy_max_overflow is not None: max_overflow = cfg.CONF.DATABASE.sqlalchemy_max_overflow engine_args['max_overflow'] = max_overflow if cfg.CONF.DATABASE.sqlalchemy_pool_timeout is not None: pool_timeout = cfg.CONF.DATABASE.sqlalchemy_pool_timeout engine_args['pool_timeout'] = pool_timeout if 'mysql' in connection_dict.drivername: engine_args['listeners'] = [MySQLPingListener()] if (MySQLdb is not None and cfg.CONF.DATABASE.sql_dbpool_enable): pool_args = { 'db': connection_dict.database, 'passwd': connection_dict.password or '', 'host': connection_dict.host, 'user': connection_dict.username, 'min_size': cfg.CONF.DATABASE.sql_min_pool_size, 'max_size': cfg.CONF.DATABASE.sql_max_pool_size, 'max_idle': cfg.CONF.DATABASE.sql_idle_timeout } pool = db_pool.ConnectionPool(MySQLdb, **pool_args) def creator(): conn = pool.create() # NOTE(belliott) eventlet >= 0.10 returns a tuple if isinstance(conn, tuple): _1, _2, conn = conn return conn engine_args['creator'] = creator if (MySQLdb is None and cfg.CONF.DATABASE.sql_dbpool_enable): LOG.warn( _("Eventlet connection pooling will not work without " "python-mysqldb!")) if 'sqlite' in connection_dict.drivername: engine_args['listeners'] = [SqliteForeignKeysListener()] if sql_connection == "sqlite://": engine_args["connect_args"] = {'check_same_thread': False} _ENGINE = create_engine(sql_connection, **engine_args) sql.event.listen(_ENGINE, 'checkin', greenthread_yield) if not register_models(): if cfg.CONF.DATABASE.reconnect_interval: remaining = cfg.CONF.DATABASE.sql_max_retries reconnect_interval = cfg.CONF.DATABASE.reconnect_interval retry_registration(remaining, reconnect_interval)
import eventlet import time import random import MySQLdb import eventlet.db_pool as db_pool from DBUtils.PooledDB import PooledDB conn_kwargs = { 'host': '10.241.226.41', 'user': '******', 'passwd': 'feisky', 'db': 'news', 'charset': 'utf8' } sql = """select * from news limit 1""" pooled = db_pool.ConnectionPool(MySQLdb, **conn_kwargs) pooldb = PooledDB(MySQLdb, **conn_kwargs) def query(conn): cur = conn.cursor() #cur.execute(sql % (random.randint(1,1000))) cur.execute(sql) data = cur.fetchall() cur.close() return data def test_mysqldb(times): now = time.time() for i in range(times):
def create_engine(sql_connection): """Return a new SQLAlchemy engine.""" connection_dict = sqlalchemy.engine.url.make_url(sql_connection) engine_args = { "pool_recycle": CONF.sql_idle_timeout, "echo": False, 'convert_unicode': True, } # Map our SQL debug level to SQLAlchemy's options if CONF.sql_connection_debug >= 100: engine_args['echo'] = 'debug' elif CONF.sql_connection_debug >= 50: engine_args['echo'] = True if "sqlite" in connection_dict.drivername: engine_args["poolclass"] = NullPool if CONF.sql_connection == "sqlite://": engine_args["poolclass"] = StaticPool engine_args["connect_args"] = {'check_same_thread': False} elif all((CONF.sql_dbpool_enable, MySQLdb, "mysql" in connection_dict.drivername)): LOG.info(_("Using mysql/eventlet db_pool.")) # MySQLdb won't accept 'None' in the password field password = connection_dict.password or '' pool_args = { 'db': connection_dict.database, 'passwd': password, 'host': connection_dict.host, 'user': connection_dict.username, 'min_size': CONF.sql_min_pool_size, 'max_size': CONF.sql_max_pool_size, 'max_idle': CONF.sql_idle_timeout, 'client_flag': mysql_client_constants.FOUND_ROWS } pool = db_pool.ConnectionPool(MySQLdb, **pool_args) def creator(): conn = pool.create() if isinstance(conn, tuple): # NOTE(belliott) eventlet >= 0.10 returns a tuple now, now, conn = conn return conn engine_args['creator'] = creator else: engine_args['pool_size'] = CONF.sql_max_pool_size if CONF.sql_max_overflow is not None: engine_args['max_overflow'] = CONF.sql_max_overflow engine = sqlalchemy.create_engine(sql_connection, **engine_args) sqlalchemy.event.listen(engine, 'checkin', greenthread_yield) if 'mysql' in connection_dict.drivername: sqlalchemy.event.listen(engine, 'checkout', ping_listener) elif 'sqlite' in connection_dict.drivername: if not CONF.sqlite_synchronous: sqlalchemy.event.listen(engine, 'connect', synchronous_switch_listener) sqlalchemy.event.listen(engine, 'connect', add_regexp_listener) if (CONF.sql_connection_trace and engine.dialect.dbapi.__name__ == 'MySQLdb'): patch_mysqldb_with_stacktrace_comments() try: engine.connect() except OperationalError, e: if not is_db_connection_error(e.args[0]): raise remaining = CONF.sql_max_retries if remaining == -1: remaining = 'infinite' while True: msg = _('SQL connection failed. %s attempts left.') LOG.warn(msg % remaining) if remaining != 'infinite': remaining -= 1 time.sleep(CONF.sql_retry_interval) try: engine.connect() break except OperationalError, e: if (remaining != 'infinite' and remaining == 0) or \ not is_db_connection_error(e.args[0]): raise
def __init__(self, host, user, password, database): self._db_pool = db_pool.ConnectionPool(dbapi, host=host, user=user, password=password, database=database)