Skip to content

Commit

Permalink
Use event.listen() instead of deprecated listeners kwarg
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Anthony Dodd committed Jul 1, 2013
1 parent 6362fb7 commit 05e7a87
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions keystone/common/sql/core.py
Expand Up @@ -176,8 +176,7 @@ def iteritems(self):
#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:
Expand All @@ -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
Expand Down Expand Up @@ -235,10 +232,13 @@ def new_engine():

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()
Expand Down

0 comments on commit 05e7a87

Please sign in to comment.