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
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
=============================================
--- /dev/null
+.. 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`
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')
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
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:
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):