Fix sqlalchemy engine listener

The sqlalchemy 1.4 had introduced some incompatible changes.

Change-Id: I98c9c1d193934c37e77297a04231a24585674306
This commit is contained in:
Lingxian Kong 2021-06-12 23:30:10 +12:00
parent 7ebce17e46
commit 8f91898de9
2 changed files with 23 additions and 25 deletions

View File

@ -20,6 +20,7 @@ import urllib
from oslo_log import log as logging
from oslo_utils import encodeutils
from sqlalchemy import event
from sqlalchemy import exc
from sqlalchemy.sql.expression import text
@ -448,8 +449,9 @@ class BaseMySqlApp(service.BaseDbApp):
ENGINE = sqlalchemy.create_engine(
CONNECTION_STR_FORMAT % (user,
urllib.parse.quote(password.strip())),
pool_recycle=120, echo=CONF.sql_query_logging,
listeners=[mysql_util.BaseKeepAliveConnection()])
pool_recycle=120, echo=CONF.sql_query_logging
)
event.listen(ENGINE, 'checkout', mysql_util.connection_checkout)
return ENGINE

View File

@ -14,7 +14,6 @@
from oslo_log import log as logging
from pymysql import err as pymysql_err
from sqlalchemy import exc
from sqlalchemy import interfaces
from sqlalchemy.sql.expression import text
from trove.guestagent.common import sql_query
@ -55,31 +54,28 @@ class SqlClient(object):
raise
class BaseKeepAliveConnection(interfaces.PoolListener):
def connection_checkout(dbapi_con, con_record, con_proxy):
"""
A connection pool listener that ensures live connections are returned
from the connection pool at checkout. This alleviates the problem of
MySQL connections timing out.
"""
def checkout(self, dbapi_con, con_record, con_proxy):
"""Event triggered when a connection is checked out from the pool."""
try:
try:
try:
dbapi_con.ping(False)
except TypeError:
dbapi_con.ping()
except dbapi_con.OperationalError as ex:
if ex.args[0] in (2006, 2013, 2014, 2045, 2055):
raise exc.DisconnectionError()
else:
raise
# MariaDB seems to timeout the client in a different
# way than MySQL and PXC
except pymysql_err.InternalError as ex:
if "Packet sequence number wrong" in str(ex):
raise exc.DisconnectionError()
elif 'Connection was killed' in str(ex):
raise exc.DisconnectionError()
else:
raise
dbapi_con.ping(False)
except TypeError:
dbapi_con.ping()
except dbapi_con.OperationalError as ex:
if ex.args[0] in (2006, 2013, 2014, 2045, 2055):
raise exc.DisconnectionError()
else:
raise
# MariaDB seems to timeout the client in a different
# way than MySQL and PXC
except pymysql_err.InternalError as ex:
if "Packet sequence number wrong" in str(ex):
raise exc.DisconnectionError()
elif 'Connection was killed' in str(ex):
raise exc.DisconnectionError()
else:
raise