]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Use event.listen() instead of deprecated listeners kwarg
authorunicell <unicell@gmail.com>
Mon, 20 Aug 2012 11:34:19 +0000 (19:34 +0800)
committerunicell <unicell@gmail.com>
Mon, 20 Aug 2012 11:46:18 +0000 (19:46 +0800)
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

cinder/db/sqlalchemy/session.py

index ed3600f74c446c331c2050273f2f734a7c1d4b0d..ef94c4b2bceee76b5ecedcbdf8fa959f8c514c49 100644 (file)
@@ -51,16 +51,12 @@ 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.
@@ -68,16 +64,14 @@ class MySQLPingListener(object):
     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):
@@ -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()