diff options
author | Lingxian Kong <anlin.kong@gmail.com> | 2021-06-12 23:30:10 +1200 |
---|---|---|
committer | Lingxian Kong <anlin.kong@gmail.com> | 2021-06-16 11:34:01 +1200 |
commit | 8f91898de992ca63511d124b5382ad3b542a9969 (patch) | |
tree | adf53bbef11c1db9266469baa519eef15222fe1d | |
parent | 7ebce17e463e07d29dbc4f09669ca825c1f15b0d (diff) | |
download | trove-8f91898de992ca63511d124b5382ad3b542a9969.tar.gz |
Fix sqlalchemy engine listener
The sqlalchemy 1.4 had introduced some incompatible changes.
Change-Id: I98c9c1d193934c37e77297a04231a24585674306
-rw-r--r-- | trove/guestagent/datastore/mysql_common/service.py | 6 | ||||
-rw-r--r-- | trove/guestagent/utils/mysql.py | 42 |
2 files changed, 23 insertions, 25 deletions
diff --git a/trove/guestagent/datastore/mysql_common/service.py b/trove/guestagent/datastore/mysql_common/service.py index b0861b88..1ab1d5fc 100644 --- a/trove/guestagent/datastore/mysql_common/service.py +++ b/trove/guestagent/datastore/mysql_common/service.py @@ -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 diff --git a/trove/guestagent/utils/mysql.py b/trove/guestagent/utils/mysql.py index d3511fa7..890177ac 100644 --- a/trove/guestagent/utils/mysql.py +++ b/trove/guestagent/utils/mysql.py @@ -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 |