Skip to content

Commit 0aa4b14

Browse files
committed
BUG25383644: Add connection back to pool on exception
Each time the connection to the server is lost during a query, the connection is not added back to the connection pool. Eventually, there are no connections left in the connection pool and the following error message is thrown: mysql.connector.errors.PoolError: Failed getting connection; pool exhausted To fix this error, we add the connection back to the pool even if the closing of the connection was unsuccessful. Tests added for regression.
1 parent d9c5cf6 commit 0aa4b14

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

lib/mysql/connector/pooling.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# MySQL Connector/Python - MySQL driver written in Python.
2-
# Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.
33

44
# MySQL Connector/Python is licensed under the terms of the GPLv2
55
# <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
@@ -112,12 +112,13 @@ def close(self):
112112
When the pool is configured to reset the session, the session
113113
state will be cleared by re-authenticating the user.
114114
"""
115-
cnx = self._cnx
116-
if self._cnx_pool.reset_session:
117-
cnx.reset_session()
118-
119-
self._cnx_pool.add_connection(cnx)
120-
self._cnx = None
115+
try:
116+
cnx = self._cnx
117+
if self._cnx_pool.reset_session:
118+
cnx.reset_session()
119+
finally:
120+
self._cnx_pool.add_connection(cnx)
121+
self._cnx = None
121122

122123
def config(self, **kwargs):
123124
"""Configuration is done through the pool"""

tests/test_bugs.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4307,3 +4307,36 @@ def test_insert_binary(self):
43074307
self.cnx.commit()
43084308
self.assertEqual(1, cursor.lastrowid)
43094309
cursor.close()
4310+
4311+
4312+
class BugOra25383644(tests.MySQLConnectorTests):
4313+
"""BUG#25383644: LOST SERVER CONNECTION LEAKS POOLED CONNECTIONS
4314+
"""
4315+
def setUp(self):
4316+
config = tests.get_mysql_config()
4317+
config["pool_size"] = 3
4318+
self.cnxpool = pooling.MySQLConnectionPool(**config)
4319+
self.mysql_server = tests.MYSQL_SERVERS[0]
4320+
4321+
def test_pool_exhaustion(self):
4322+
sql = "SELECT * FROM dummy"
4323+
4324+
i = 4
4325+
while i > 0:
4326+
cnx = self.cnxpool.get_connection()
4327+
cur = cnx.cursor()
4328+
try:
4329+
self.mysql_server.stop()
4330+
self.mysql_server.wait_down()
4331+
cur.execute(sql)
4332+
except mysql.connector.errors.OperationalError:
4333+
try:
4334+
cur.close()
4335+
cnx.close()
4336+
except mysql.connector.errors.OperationalError:
4337+
pass
4338+
finally:
4339+
i -= 1
4340+
if not self.mysql_server.check_running():
4341+
self.mysql_server.start()
4342+
self.mysql_server.wait_up()

0 commit comments

Comments
 (0)