Skip to content

Commit

Permalink
Merge "check connection in Listener. refer to Bug #943031"
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenkins authored and openstack-gerrit committed Mar 14, 2012
2 parents 7cfe4e9 + 189b89a commit e2b1b4a
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions quantum/db/api.py
Expand Up @@ -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
Expand All @@ -33,6 +34,27 @@
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
Expand All @@ -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()


Expand Down

0 comments on commit e2b1b4a

Please sign in to comment.