iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
PooledMySQLConnection.close() does not close its socket: it returns the connection to the pool for reuse. The documented MySQLConnectionPool API examined here does not provide a public pool-wide close() or dispose() method. So if your app expects calling close() on borrowed connections to drain the pool and terminate every socket, that is not what the built-in pool promises.
Why pooled sockets can remain open after close()
Oracle MySQL’s MySQL Connector/Python Developer Guide explains that a pooled connection’s close() returns it to the pool and makes it available for later requests; it does not actually close the connection. This is a lease-return operation, not socket teardown.
That distinction explains why a connection may remain available on the server after your code calls close(). The connection is idle and reusable while the pool manages it. Returning every checked-out connection is still important, but it does not amount to disposing of the pool.
Does MySQL Connector/Python have a pool-wide close method?
The current documented MySQLConnectionPool API lists construction, add_connection(), get_connection(), set_config(), and the pool_name property. It does not list a public pool-wide close or disposal operation. The pooled connection API documents returning a connection to the pool, not destroying the pool.
#1 Best Overall
This conclusion is limited to the documented API examined here; it is not a claim about undocumented behavior or every future Connector/Python release. Do not assume that calling pool.close() is supported unless the official API for your installed version documents it. Avoid manipulating private Connector/Python attributes as a substitute for a supported lifecycle method.
What to do when the application shuts down
- Identify the object and creation path. Check whether the app obtains
PooledMySQLConnectionobjects throughmysql.connector.connect(pool_name=..., pool_size=...)or creates an explicitMySQLConnectionPool. Confirm which process owns the sockets you are observing. - Finish work and close cursors. Close each cursor after its work is complete so it does not outlive the operation that created it.
- Return each borrowed connection. Call
close()on each checked-out pooled connection when finished. Treat that call as releasing the connection to the pool, not as terminating its underlying socket. - Check the shutdown requirement. If the application must explicitly dispose of a pool and its connections, the built-in API documented here does not establish a public method for doing so. Evaluate a pool manager with a documented disposal operation, verifying its driver compatibility and how it handles checked-out connections during shutdown.
- Test the actual deployment. Record the Connector/Python and MySQL server versions, whether the C extension is enabled, how the pool is initialized, and how the process exits. Observe whether the connections belong to the expected process and whether borrowed connections were returned.
Whether and when the operating system or server releases sockets after process termination depends on the deployment. The documented pool behavior alone cannot establish the timing for a particular application.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Distinguish expected idle connections from a close/reset error
An idle server-side connection after returning a pooled connection is consistent with reuse. An exception during close or reset is a separate issue and may warrant checking the versions and connector mode rather than treating every open socket as the same failure.
Recommended Free Tools
A historical Oracle MySQL bug report described an exception while closing a pooled connection with Connector/Python 8.0.22–8.0.26, the C extension, and a MySQL server older than 5.7.13. A developer response said the issue was fixed in the upcoming Connector/Python 8.0.29 release. This is a version-specific historical clue, not evidence that current releases have the same problem. See the Oracle MySQL bug record.
A separate historical report concerned pooled connections becoming unavailable after a reset exception when the server connection was lost; the report says the behavior was noted in the Connector/Python 2.1.6 changelog. That case concerns reset failure and pool availability, not the ordinary documented meaning of close(). See the Oracle MySQL bug record.
Quick Recap
Rank #4
What to verify before changing the design
- The exact Connector/Python and MySQL server versions, and whether the C extension is active.
- Whether the connection came from a pool and whether every checked-out connection is returned during normal work and shutdown.
- Whether the observation is an idle connection available for reuse, an exception during close/reset, or a connection still checked out by application code.
- Whether the shutdown requirement is simply to return borrowed connections or to explicitly destroy the pool and its connections.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

