Skip to content

Commit

Permalink
Use event.listen() instead of deprecated listeners kwarg
Browse files Browse the repository at this point in the history
Fixes bug 1031405

The listeners kwarg is deprecated in SQLAlchemy 0.7.0. Since 0.7.3 is
the minimum required version of SQLAlchemy, use the new event.listen()
API instead.

Change-Id: I1b851e197c04b131ae96e814780b04e6dcdeeb1a
  • Loading branch information
unicell committed Aug 20, 2012
1 parent d0c6448 commit 5da61a7
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions cinder/db/sqlalchemy/session.py
Expand Up @@ -51,33 +51,27 @@ def get_session(autocommit=True, expire_on_commit=False):
return session


class SynchronousSwitchListener(sqlalchemy.interfaces.PoolListener):

def synchronous_switch_listener(dbapi_conn, connection_rec):
"""Switch sqlite connections to non-synchronous mode"""
dbapi_conn.execute("PRAGMA synchronous = OFF")

def connect(self, dbapi_con, con_record):
dbapi_con.execute("PRAGMA synchronous = OFF")


class MySQLPingListener(object):

def ping_listener(dbapi_conn, connection_rec, connection_proxy):
"""
Ensures that MySQL connections checked out of the
pool are alive.
Borrowed from:
http://groups.google.com/group/sqlalchemy/msg/a4ce563d802c929f
"""

def checkout(self, dbapi_con, con_record, con_proxy):
try:
dbapi_con.cursor().execute('select 1')
except dbapi_con.OperationalError, ex:
if ex.args[0] in (2006, 2013, 2014, 2045, 2055):
LOG.warn('Got mysql server has gone away: %s', ex)
raise DisconnectionError("Database server went away")
else:
raise
try:
dbapi_conn.cursor().execute('select 1')
except dbapi_conn.OperationalError, ex:
if ex.args[0] in (2006, 2013, 2014, 2045, 2055):
LOG.warn('Got mysql server has gone away: %s', ex)
raise DisconnectionError("Database server went away")
else:
raise


def is_db_connection_error(args):
Expand Down Expand Up @@ -116,13 +110,14 @@ def get_engine():
engine_args["poolclass"] = StaticPool
engine_args["connect_args"] = {'check_same_thread': False}

if not FLAGS.sqlite_synchronous:
engine_args["listeners"] = [SynchronousSwitchListener()]
_ENGINE = sqlalchemy.create_engine(FLAGS.sql_connection, **engine_args)

if 'mysql' in connection_dict.drivername:
engine_args['listeners'] = [MySQLPingListener()]

_ENGINE = sqlalchemy.create_engine(FLAGS.sql_connection, **engine_args)
sqlalchemy.event.listen(_ENGINE, 'checkout', ping_listener)
elif "sqlite" in connection_dict.drivername:
if not FLAGS.sqlite_synchronous:
sqlalchemy.event.listen(_ENGINE, 'connect',
synchronous_switch_listener)

try:
_ENGINE.connect()
Expand Down

0 comments on commit 5da61a7

Please sign in to comment.