Skip to content

fix: support pool_pre_ping in SQLAlchemy ORM MySQL dialect#1245

Merged
karenc-bq merged 1 commit into
aws:mainfrom
sudosubin:fix/sqlalchemy-mysql-dialect-pool-pre-ping
Jun 9, 2026
Merged

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

Conversation

@sudosubin

@sudosubin sudosubin commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

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>
@karenc-bq karenc-bq force-pushed the fix/sqlalchemy-mysql-dialect-pool-pre-ping branch from 5b65ea7 to e0541c8 Compare June 9, 2026 17:40
@karenc-bq karenc-bq merged commit 3ae4538 into aws:main Jun 9, 2026
6 checks passed
@karenc-bq

Copy link
Copy Markdown
Contributor

Hi @sudosubin, thank you for contributing this fix, we will merge the changes now and post an update here once it has been released

AhmadMasry pushed a commit to AhmadMasry/aws-advanced-python-wrapper that referenced this pull request Jun 14, 2026
Signed-off-by: sudosubin <sudosubin@gmail.com>
AhmadMasry added a commit to AhmadMasry/aws-advanced-python-wrapper that referenced this pull request Jun 14, 2026
Adopts AWS PR aws#1245 (do_ping), generalized from AWS sync-MySQL-only to all
four wrapper dialects (MySQL+PG, sync+async). AwsWrapperConnection exposes no
.ping(), so SA pool_pre_ping=True raised AttributeError. Each dialect now
reaches the native driver connection via target_connection:
- MySQL (sync/async): native ping(reconnect=False) (aiomysql bridged via await_only)
- PG (sync/async): SELECT 1 (psycopg has no ping(); async bridged via await_)
Returns False on failure -> SA _do_ping_w_event recycles the pooled conn.
initialize() override (AWS mysql_orm_dialect) NOT needed: our _detect_charset
override already handles the only wrapper-incompatible introspection path.
Adds unit tests + [mypy-pymysql] ignore_missing_imports.
AhmadMasry added a commit to AhmadMasry/aws-advanced-python-wrapper that referenced this pull request Jun 14, 2026
The branch already contains all of origin/main's commits (merged earlier in
66fbcd4 and adopted: SQLAlchemy ORM support, gdb RWS aws#1243, initial_connection
default plugins aws#1242, do_ping aws#1245, etc.). The Co-Authored-By trailer rewrite
re-hashed those merged-in main commits, so git's ancestry counter showed the
branch as '11 behind main' even though the content is present (verified).

Use -s ours to re-link origin/main as an ancestor without altering the
validated, integration-tested tree -- a content merge would instead re-add
files we intentionally dropped (mysql_orm_dialect.py replaced by our
sqlalchemy_dialects/) and re-surface already-resolved conflicts.
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.

2 participants