From: Maxim Bublis Date: Mon, 2 Jul 2018 16:34:32 +0000 (-0400) Subject: Use MySQL protocol-level ping. X-Git-Tag: rel_1_3_0b1~136^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bd3255760b24bf9f3772415afd2e87c7a12a91e6;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Use MySQL protocol-level ping. Utilizes MySQL protocol-level pings for disconnection detection. This is just a 5-byte packet followed by a 7-byte response. Affects MySQLdb, MySQL Connector and PyMySQL dialects. Change-Id: I672f75e3746878d88987a31750444dde0cf8eb9b Pull-request: https://github.com/zzzeek/sqlalchemy/pull/460 --- diff --git a/doc/build/changelog/migration_13.rst b/doc/build/changelog/migration_13.rst index 71ef4d0113..a4ac864c69 100644 --- a/doc/build/changelog/migration_13.rst +++ b/doc/build/changelog/migration_13.rst @@ -99,6 +99,18 @@ Dialect Improvements and Changes - PostgreSQL Dialect Improvements and Changes - MySQL ============================================= +.. _change_mysql_ping: + +Protocol-level ping now used for pre-ping +------------------------------------------ + +The MySQL dialects including mysqlclient, python-mysql, PyMySQL and +mysql-connector-python now use the ``connection.ping()`` method for the +pool pre-ping feature, described at :ref:`pool_disconnects_pessimistic`. +This is a much more lightweight ping than the previous method of emitting +"SELECT 1" on the connection. + + Dialect Improvements and Changes - SQLite ============================================= diff --git a/doc/build/changelog/unreleased_13/mysql_ping.rst b/doc/build/changelog/unreleased_13/mysql_ping.rst new file mode 100644 index 0000000000..daf1125492 --- /dev/null +++ b/doc/build/changelog/unreleased_13/mysql_ping.rst @@ -0,0 +1,11 @@ +.. change:: + :tags: feature, mysql + + The "pre-ping" feature of the connection pool now uses + the ``ping()`` method of the DBAPI connection in the case of + mysqlclient, PyMySQL and mysql-connector-python. Pull request + courtesy Maxim Bublis. + + .. seealso:: + + :ref:`change_mysql_ping` diff --git a/lib/sqlalchemy/dialects/mysql/mysqlconnector.py b/lib/sqlalchemy/dialects/mysql/mysqlconnector.py index 1ead8aaf50..e16b68bada 100644 --- a/lib/sqlalchemy/dialects/mysql/mysqlconnector.py +++ b/lib/sqlalchemy/dialects/mysql/mysqlconnector.py @@ -160,6 +160,17 @@ class MySQLDialect_mysqlconnector(MySQLDialect): from mysql import connector return connector + def do_ping(self, dbapi_connection): + try: + dbapi_connection.ping(False) + except self.dbapi.Error as err: + if self.is_disconnect(err, dbapi_connection, None): + return False + else: + raise + else: + return True + def create_connect_args(self, url): opts = url.translate_connect_args(username='user') @@ -225,7 +236,8 @@ class MySQLDialect_mysqlconnector(MySQLDialect): exceptions = (self.dbapi.OperationalError, self.dbapi.InterfaceError) if isinstance(e, exceptions): return e.errno in errnos or \ - "MySQL Connection not available." in str(e) + "MySQL Connection not available." in str(e) or \ + "Connection to MySQL is not available" in str(e) else: return False diff --git a/lib/sqlalchemy/dialects/mysql/mysqldb.py b/lib/sqlalchemy/dialects/mysql/mysqldb.py index 535c8ec52d..7554d244cc 100644 --- a/lib/sqlalchemy/dialects/mysql/mysqldb.py +++ b/lib/sqlalchemy/dialects/mysql/mysqldb.py @@ -101,6 +101,17 @@ class MySQLDialect_mysqldb(MySQLDialect): def dbapi(cls): return __import__('MySQLdb') + def do_ping(self, dbapi_connection): + try: + dbapi_connection.ping(False) + except self.dbapi.Error as err: + if self.is_disconnect(err, dbapi_connection, None): + return False + else: + raise + else: + return True + def do_executemany(self, cursor, statement, parameters, context=None): rowcount = cursor.executemany(statement, parameters) if context is not None: diff --git a/lib/sqlalchemy/dialects/mysql/pymysql.py b/lib/sqlalchemy/dialects/mysql/pymysql.py index 4f1c792f95..5f176cef2c 100644 --- a/lib/sqlalchemy/dialects/mysql/pymysql.py +++ b/lib/sqlalchemy/dialects/mysql/pymysql.py @@ -61,6 +61,14 @@ class MySQLDialect_pymysql(MySQLDialect_mysqldb): def dbapi(cls): return __import__('pymysql') + def is_disconnect(self, e, connection, cursor): + if super(MySQLDialect_pymysql, self).is_disconnect(e, connection, cursor): + return True + elif isinstance(e, self.dbapi.Error): + return "Already closed" in str(e) + else: + return False + if py3k: def _extract_error_code(self, exception): if isinstance(exception.args[0], Exception):