Skip to content

Commit 9332f01

Browse files
amitabnmariz
authored andcommitted
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 7555f2b commit 9332f01

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
@@ -4342,3 +4342,36 @@ def test_insert_binary(self):
43424342
self.cnx.commit()
43434343
self.assertEqual(1, cursor.lastrowid)
43444344
cursor.close()
4345+
4346+
4347+
class BugOra25383644(tests.MySQLConnectorTests):
4348+
"""BUG#25383644: LOST SERVER CONNECTION LEAKS POOLED CONNECTIONS
4349+
"""
4350+
def setUp(self):
4351+
config = tests.get_mysql_config()
4352+
config["pool_size"] = 3
4353+
self.cnxpool = pooling.MySQLConnectionPool(**config)
4354+
self.mysql_server = tests.MYSQL_SERVERS[0]
4355+
4356+
def test_pool_exhaustion(self):
4357+
sql = "SELECT * FROM dummy"
4358+
4359+
i = 4
4360+
while i > 0:
4361+
cnx = self.cnxpool.get_connection()
4362+
cur = cnx.cursor()
4363+
try:
4364+
self.mysql_server.stop()
4365+
self.mysql_server.wait_down()
4366+
cur.execute(sql)
4367+
except mysql.connector.errors.OperationalError:
4368+
try:
4369+
cur.close()
4370+
cnx.close()
4371+
except mysql.connector.errors.OperationalError:
4372+
pass
4373+
finally:
4374+
i -= 1
4375+
if not self.mysql_server.check_running():
4376+
self.mysql_server.start()
4377+
self.mysql_server.wait_up()

0 commit comments

Comments
 (0)