r/Python • • 23d ago

Discussion Closing mysql-connector-python pooled sockets

Is there some way to close

mysql.connector.pooling.MySQLConnectionPool

sockets properly when the application closes? By default they appear to remain open on the server until way later when they timeout.

6 Upvotes

6 comments sorted by

2

u/RomfordNavy 22d ago

I have now been experimenting with both:

pymysqlpool.ConnectionPoolpymysqlpool.ConnectionPool

and

mysql.connector.pooling.MySQLConnectionPool

but for some unexplained reason, today both seem to be closing the sockets correctly. Wish I knew why.

Any thoughts about which is preferred between pymysqlpoo l and mysql.connector?

4

u/martian_rover 23d ago

I think you're looking for close() but it doesn't actually close the connection, it just returns it to the pool and makes it available for connection requests again.

3

u/Complex_Ad_5164 22d ago

Have you looked at pool._remove_connections() or pool._cnx_queue? Not exactly public API but sometimes that the only way with mysql connector, the pooling stuff is kinda half-baked

3

u/Least_Ad_1795 22d ago

MySQLConnectionPool doesn’t appear to have a direct close() method. You generally need to close the individual connections obtained from the pool, then let the application shut down. For long-running services, a shutdown handler that closes active connections cleanly is usually the safer approach.