Skip to content

Commit 142dba9

Browse files
committed
BUG19331658: Fix connection pooling with MySQL Fabric
Using connection pooling with MySQL Fabric was raising AttributeError. We fix this by creating a pool of connections to a server as and when first time it is required to be connected. To use pooling with Fabric, 'pool_size' argument should be used. Using 'pool_name' with Fabric raises AttributeError. This is because we can't have two pools with same names on different servers. A unit test has been added for BUG#19331658.
1 parent 08847cc commit 142dba9

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

lib/mysql/connector/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ def connect(*args, **kwargs):
139139
raise InterfaceError("fabric and failover arguments can not be used")
140140

141141
if 'fabric' in kwargs:
142+
if 'pool_name' in kwargs:
143+
raise AttributeError("'pool_name' argument is not supported with "
144+
" MySQL Fabric. Use 'pool_size' instead.")
142145
from .fabric import connect as fabric_connect
143146
return fabric_connect(*args, **kwargs)
144147

lib/mysql/connector/fabric/connection.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
HAVE_SSL = True
6262
# pylint: enable=F0401,E0611
6363

64-
from ..connection import MySQLConnection
64+
import ...connector import connect
6565
from ..pooling import MySQLConnectionPool
6666
from ..errors import (
6767
Error, InterfaceError, NotSupportedError, MySQLFabricError, InternalError,
@@ -1123,6 +1123,8 @@ def store_config(self, **kwargs):
11231123
del test_config['pool_name']
11241124
if 'pool_size' in test_config:
11251125
del test_config['pool_size']
1126+
if 'pool_reset_session' in test_config:
1127+
del test_config['pool_reset_session']
11261128
try:
11271129
pool = MySQLConnectionPool(pool_name=str(uuid.uuid4()))
11281130
pool.set_config(**test_config)
@@ -1186,7 +1188,7 @@ def _connect(self):
11861188
dbconfig['host'] = mysqlserver.host
11871189
dbconfig['port'] = mysqlserver.port
11881190
try:
1189-
self._mysql_cnx = MySQLConnection(**dbconfig)
1191+
self._mysql_cnx = connect(**dbconfig)
11901192
except Error as exc:
11911193
if counter == attempts:
11921194
self.reset_cache(mysqlserver.group)

tests/test_fabric.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from mysql.connector import fabric, errorcode
4242
from mysql.connector.fabric import connection, balancing
4343
from mysql.connector.catch23 import UNICODE_TYPES, PY2
44+
from mysql.connector.pooling import PooledMySQLConnection
4445

4546
ERR_NO_FABRIC_CONFIG = "Fabric configuration not available"
4647

@@ -578,3 +579,42 @@ def test_bug19642249(self):
578579
self.assertEqual("Key invalid; was '1977-01-01'", str(exc))
579580
else:
580581
self.fail("ValueError not raised")
582+
583+
def test_bug19331658(self):
584+
"""Pooling not working with fabric
585+
"""
586+
self.assertRaises(
587+
AttributeError, mysql.connector.connect,
588+
fabric=tests.FABRIC_CONFIG, user='root', database='employees',
589+
pool_name='mypool')
590+
591+
pool_size = 2
592+
cnx = mysql.connector.connect(
593+
fabric=tests.FABRIC_CONFIG, user='root', database='employees',
594+
pool_size=pool_size, pool_reset_session=False
595+
)
596+
tbl_name = "employees_range"
597+
598+
tables = ["employees.{0}".format(tbl_name)]
599+
600+
cnx.set_property(tables=tables,
601+
scope=fabric.SCOPE_GLOBAL,
602+
mode=fabric.MODE_READWRITE)
603+
cnx.cursor()
604+
self.assertTrue(isinstance(cnx._mysql_cnx, PooledMySQLConnection))
605+
606+
data = self.emp_data[1985]
607+
608+
for emp in data:
609+
cnx.set_property(tables=tables,
610+
key=emp[0],
611+
scope=fabric.SCOPE_LOCAL,
612+
mode=fabric.MODE_READWRITE)
613+
cnx.cursor()
614+
mysqlserver = cnx._fabric_mysql_server
615+
config = cnx._mysql_config
616+
self.assertEqual(
617+
cnx._mysql_cnx.pool_name, "{0}_{1}_{2}_{3}".format(
618+
mysqlserver.host, mysqlserver.port, config['user'],
619+
config['database'])
620+
)

0 commit comments

Comments
 (0)