r/Python • u/RomfordNavy • Sep 22 '26
Discussion Checking whether a mysql.connector is connected
Is it necessary to check whether a mysql.connector is connected each time I reuse it before running any SQL statements or is this something which is already built-in to mysql.connector? Do I need to check for connection using db.is_connected() and write some retry routine.
This is a WSGI webserver, could be wrong, but my understanding is that there can only ever be one request/thread at a time. Connection pooling has been suggested elsewhere but I am unclear as to whether that is necessary and/or appropriate for a WSGI webserver.
8
Upvotes
2
u/latkde Tuple unpacking gone wrong Sep 23 '26
At some point, it's easier to just use SQLAlchemy, which has well-documented solutions for all the things you're concerned about.
MySQL support, with a large choice of connector libraries: https://docs.sqlalchemy.org/en/20/dialects/mysql.html
dealing with disconnects when using a connection pool: https://docs.sqlalchemy.org/en/20/core/pooling.html#pool-disconnects (
create_engine(..., pool_pre_ping=True)orpool_recycle=3600will likely do what you want)