Skip to content

Commit 8d1c9c5

Browse files
committed
BUG#33923516: Allow tuple of dictionaries as "failover" argument
In Connector/Python documentation states that the connect() method accepts a failover argument that provides information to use for the server failover in the event of connection failures, and this argument value should be a tuple or a list of dictionaries. Using a tuple fails with an AttributeError exception. This patch fixes this issue by applying the sorted() function instead of using the sort() method in the failover argument, for sorting the servers by priority. Change-Id: I8d6911d70d425317374539a830a839ca1cff751f
1 parent b05b342 commit 8d1c9c5

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ v8.0.30
1616
- WL#15035: Enforce PEP 7 and PEP 8 coding style
1717
- WL#14822: Refactor the authentication plugin mechanism
1818
- WL#14815: Support OpenSSL 3.0
19+
- BUG#33923516: Allow tuple of dictionaries as "failover" argument
1920
- BUG#28821983: Fix rounding errors for decimal values
2021

2122
v8.0.29

lib/mysql/connector/pooling.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,9 @@ def _get_failover_connection(**kwargs):
164164
"every router"
165165
)
166166

167-
failover.sort(key=lambda x: x["priority"], reverse=True)
168-
169167
server_directory = {}
170168
server_priority_list = []
171-
for server in failover:
169+
for server in sorted(failover, key=lambda x: x["priority"], reverse=True):
172170
if server["priority"] not in server_directory:
173171
server_directory[server["priority"]] = [server]
174172
server_priority_list.append(server["priority"])

tests/test_connection.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3486,7 +3486,7 @@ def test_failover(self):
34863486
"passwd": passw,
34873487
}
34883488

3489-
# connection must fail with error: "Unable to connect to any of the target hosts"
3489+
# Connection must fail with error: "Unable to connect to any of the target hosts"
34903490
with self.assertRaises(InterfaceError) as context:
34913491
_ = connect(**settings)
34923492

@@ -3499,8 +3499,13 @@ def test_failover(self):
34993499
settings["failover"].append(
35003500
{"host": host, "port": port, "user": user, "priority": 100}
35013501
)
3502-
# connection must be successful
3503-
_ = connect(**cnx_config)
3502+
# Connection must be successful
3503+
_ = connect(**settings)
3504+
3505+
# Test 'failover' using a tuple instead
3506+
settings["failover"] = tuple(settings["failover"])
3507+
# Connection must be successful
3508+
_ = connect(**settings)
35043509

35053510

35063511
@unittest.skipIf(

0 commit comments

Comments
 (0)