]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
check connection in Listener. refer to Bug #943031
authorPeng Yong <ppyy@pubyun.com>
Sun, 11 Mar 2012 16:06:53 +0000 (00:06 +0800)
committerPeng Yong <ppyy@pubyun.com>
Mon, 12 Mar 2012 06:54:59 +0000 (14:54 +0800)
Change-Id: I5a0c975ab7998627a213ac4c69c037e9e2d95bfa

quantum/db/api.py

index 32cae159dcb474bac5e31ebf3fc9d214a7695638..455453816d00e8f445d6ca9217a4b81180ef9688 100644 (file)
@@ -18,8 +18,9 @@
 # @author: Dan Wendlandt, Nicira Networks, Inc.
 
 import logging
-
+import sqlalchemy as sql
 from sqlalchemy import create_engine
+from sqlalchemy.exc import DisconnectionError
 from sqlalchemy.orm import sessionmaker, exc
 
 from quantum.api.api_common import OperationalStatus
@@ -33,6 +34,27 @@ BASE = models.BASE
 LOG = logging.getLogger('quantum.db.api')
 
 
+class MySQLPingListener(object):
+
+    """
+    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
+
+
 def configure_db(options):
     """
     Establish the database, create an engine if needed, and
@@ -42,10 +64,17 @@ def configure_db(options):
     """
     global _ENGINE
     if not _ENGINE:
-        _ENGINE = create_engine(options['sql_connection'],
-                                echo=False,
-                                echo_pool=True,
-                                pool_recycle=3600)
+        connection_dict = sql.engine.url.make_url(options['sql_connection'])
+        engine_args = {
+            'pool_recycle': 3600,
+            'echo': False,
+            'convert_unicode': True,
+        }
+
+        if 'mysql' in connection_dict.drivername:
+            engine_args['listeners'] = [MySQLPingListener()]
+
+        _ENGINE = create_engine(options['sql_connection'], **engine_args)
         register_models()