From 05e7a87b893aae0cb22ef85e282ada4979f9db9e Mon Sep 17 00:00:00 2001 From: Anthony Dodd Date: Mon, 24 Jun 2013 16:23:00 -0500 Subject: Use event.listen() instead of deprecated listeners kwarg The listeners kwarg is deprecated in SQLAlchemy 0.7.0. Use the new event.listen() method instead. Instead of using the original approach of testing whether or not the string 'mysql' is in 'connection_dict.drivername', now we are using a more direct string equality test to ensure that engine.name == 'mysql' before passing it to sql.event.listen(). We've decided to change the name of our ping_listener function to reflect its purpose more accurately. Its name has been changed from "ping_listener" to "mysql_on_checkout". Change-Id: Ib9ef52404e3d474a60cdc82e8fcf8c6a9616bce3 Fixes: bug #1031405 --- keystone/common/sql/core.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/keystone/common/sql/core.py b/keystone/common/sql/core.py index e1bd138e..d75c73a8 100644 --- a/keystone/common/sql/core.py +++ b/keystone/common/sql/core.py @@ -176,8 +176,7 @@ class DictBase(object): #return local.iteritems() -class MySQLPingListener(object): - +def mysql_on_checkout(dbapi_conn, connection_rec, connection_proxy): """Ensures that MySQL connections checked out of the pool are alive. Borrowed from: @@ -192,16 +191,14 @@ class MySQLPingListener(object): from http://dev.mysql.com/doc/refman/5.6/en/error-messages-client.html """ - - def checkout(self, dbapi_con, con_record, con_proxy): - try: - dbapi_con.cursor().execute('select 1') - except dbapi_con.OperationalError as e: - if e.args[0] in (2006, 2013, 2014, 2045, 2055): - logging.warn(_('Got mysql server has gone away: %s'), e) - raise DisconnectionError("Database server went away") - else: - raise + try: + dbapi_conn.cursor().execute('select 1') + except dbapi_conn.OperationalError as e: + if e.args[0] in (2006, 2013, 2014, 2045, 2055): + logging.warn(_('Got mysql server has gone away: %s'), e) + raise DisconnectionError("Database server went away") + else: + raise # Backends @@ -235,10 +232,13 @@ class Base(object): if 'sqlite' in connection_dict.drivername: engine_config['poolclass'] = sqlalchemy.pool.StaticPool - elif 'mysql' in connection_dict.drivername: - engine_config['listeners'] = [MySQLPingListener()] - return sql.create_engine(CONF.sql.connection, **engine_config) + engine = sql.create_engine(CONF.sql.connection, **engine_config) + + if engine.name == 'mysql': + sql.event.listen(engine, 'checkout', mysql_on_checkout) + + return engine if not allow_global_engine: return new_engine() -- cgit