Skip to content

fix: support pool_pre_ping in SQLAlchemy ORM MySQL dialect#1245

Open
sudosubin wants to merge 1 commit into
aws:mainfrom
sudosubin:fix/sqlalchemy-mysql-dialect-pool-pre-ping
Open

fix: support pool_pre_ping in SQLAlchemy ORM MySQL dialect#1245
sudosubin wants to merge 1 commit into
aws:mainfrom
sudosubin:fix/sqlalchemy-mysql-dialect-pool-pre-ping

Conversation

@sudosubin
Copy link
Copy Markdown

@sudosubin sudosubin commented Jun 5, 2026

Description

engine = create_engine(
    "mysql+aws_wrapper_mysqlconnector://user:pass@host:3306/db",
    pool_pre_ping=True,
)

Fixes pool_pre_ping=True for the SQLAlchemy ORM MySQL dialect. SQLAlchemy passes the DBAPI connection to do_ping, which here is an AwsWrapperConnection. The inherited do_ping calls dbapi_connection.ping(), but AwsWrapperConnection does not expose ping(), so every pool checkout raised 'AwsWrapperConnection' object has no attribute 'ping'.

This overrides do_ping to ping the wrapped driver connection (target_connection) instead. A dead connection raises a native mysql.connector error, which SQLAlchemy cannot classify as a disconnect because import_dbapi reports the wrapper module as the DBAPI (its exception hierarchy is unrelated to mysql.connector's), so do_ping catches it and returns False, letting the pool invalidate and reconnect.

Unit tests are added for the live and dead connection paths.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Signed-off-by: sudosubin <sudosubin@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant