From 7ab605c2d25c3cd83af41e3250c97c623220cc7a Mon Sep 17 00:00:00 2001 From: valievkarim Date: Wed, 18 May 2022 17:54:57 +0200 Subject: [PATCH] Include new MySQL error code 4031 for MySQL disconnect check When using pool_pre_ping=True with MySQL version 8.0.28 and a stale connection, SQLAlchemy raises the following error instead of reconnecting: MySQLdb._exceptions.OperationalError: (4031, 'The client was disconnected by the server because of inactivity. See wait_timeout and interactive_timeout for configuring this behavior.') 4031 (ER_CLIENT_INTERACTION_TIMEOUT) is a new error code added in MySQL version 8.0.24: https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_client_interaction_timeout This commit adds 4031 to the error codes list checked in MySQL dialect "is_disconnect" check. Fixes: #8036 --- doc/build/changelog/unreleased_14/8036.rst | 7 +++++++ lib/sqlalchemy/dialects/mysql/base.py | 1 + test/dialect/mysql/test_dialect.py | 4 ++++ 3 files changed, 12 insertions(+) create mode 100644 doc/build/changelog/unreleased_14/8036.rst diff --git a/doc/build/changelog/unreleased_14/8036.rst b/doc/build/changelog/unreleased_14/8036.rst new file mode 100644 index 0000000000..c2b61634ff --- /dev/null +++ b/doc/build/changelog/unreleased_14/8036.rst @@ -0,0 +1,7 @@ +.. change:: + :tags: bug, mysql + :tickets: 8036 + + Fixed an issue in the mysql dialect when using the + :paramref:`.create_engine.pool_pre_ping` parameter with MySQL >= + 8.0.24, an exception was raised instead of reconnecting diff --git a/lib/sqlalchemy/dialects/mysql/base.py b/lib/sqlalchemy/dialects/mysql/base.py index 420fcfdfdb..65de88cfe6 100644 --- a/lib/sqlalchemy/dialects/mysql/base.py +++ b/lib/sqlalchemy/dialects/mysql/base.py @@ -2568,6 +2568,7 @@ class MySQLDialect(default.DefaultDialect): 2014, 2045, 2055, + 4031, ): return True elif isinstance( diff --git a/test/dialect/mysql/test_dialect.py b/test/dialect/mysql/test_dialect.py index 4fa524a35c..d79f2629fa 100644 --- a/test/dialect/mysql/test_dialect.py +++ b/test/dialect/mysql/test_dialect.py @@ -199,6 +199,10 @@ class DialectTest(fixtures.TestBase): (2006, "foo", "OperationalError", "pymysql", True), (2007, "foo", "OperationalError", "mysqldb", False), (2007, "foo", "OperationalError", "pymysql", False), + (4031, "foo", "OperationalError", "mysqldb", True), + (4031, "foo", "OperationalError", "pymysql", True), + (4032, "foo", "OperationalError", "mysqldb", False), + (4032, "foo", "OperationalError", "pymysql", False), ) def test_is_disconnect( self, arg0, message, exc_cls_name, dialect_name, is_disconnect -- 2.47.3