Skip to content

Commit 87f25a9

Browse files
committed
Bug#30561339: Connector should only move to next host on a network error
1 parent 62f6785 commit 87f25a9

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

driver/mysql_connection.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,7 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
11011101
//Connect loop
11021102
{
11031103
bool connected = false;
1104+
std::random_device generator;
11041105

11051106
while(!host_list.empty() && !connected)
11061107
{
@@ -1120,7 +1121,7 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
11201121

11211122
while (!weights.empty() && !connected)
11221123
{
1123-
std::random_device generator;
1124+
11241125
std::discrete_distribution<int> distribution(
11251126
weights.begin(), weights.end());
11261127

@@ -1145,8 +1146,28 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
11451146
connected = true;
11461147
break;
11471148
}
1148-
catch (sql::SQLException&)
1149-
{}
1149+
catch (sql::SQLException& e)
1150+
{
1151+
switch (e.getErrorCode())
1152+
{
1153+
case ER_CON_COUNT_ERROR:
1154+
case CR_SOCKET_CREATE_ERROR:
1155+
case CR_CONNECTION_ERROR:
1156+
case CR_CONN_HOST_ERROR:
1157+
case CR_IPSOCK_ERROR:
1158+
case CR_UNKNOWN_HOST:
1159+
//On Network errors, continue
1160+
break;
1161+
default:
1162+
//If SQLSTATE not 08xxx, which is used for network errors
1163+
if(e.getSQLState().compare(0,2, "08") != 0)
1164+
{
1165+
//Re-throw error and do not try another host
1166+
throw;
1167+
}
1168+
}
1169+
1170+
}
11501171

11511172
same_prio.erase(el);
11521173
weights.erase(weight_el);

0 commit comments

Comments
 (0)