public ConnectionImpl pickConnection()

in src/main/user-impl/java/com/mysql/cj/jdbc/ha/SequentialBalanceStrategy.java [44:154]


    public ConnectionImpl pickConnection(InvocationHandler proxy, List<String> configuredHosts, Map<String, JdbcConnection> liveConnections,
            long[] responseTimes, int numRetries) throws SQLException {
        int numHosts = configuredHosts.size();

        SQLException ex = null;

        Map<String, Long> blockList = ((LoadBalancedConnectionProxy) proxy).getGlobalBlocklist();

        for (int attempts = 0; attempts < numRetries; attempts++) {
            if (numHosts == 1) {
                this.currentHostIndex = 0; // pathological case
            } else if (this.currentHostIndex == -1) {
                int random = (int) Math.floor(Math.random() * numHosts);

                for (int i = random; i < numHosts; i++) {
                    if (!blockList.containsKey(configuredHosts.get(i))) {
                        this.currentHostIndex = i;
                        break;
                    }
                }

                if (this.currentHostIndex == -1) {
                    for (int i = 0; i < random; i++) {
                        if (!blockList.containsKey(configuredHosts.get(i))) {
                            this.currentHostIndex = i;
                            break;
                        }
                    }
                }

                if (this.currentHostIndex == -1) {
                    blockList = ((LoadBalancedConnectionProxy) proxy).getGlobalBlocklist(); // it may have changed
                    // and the proxy returns a copy

                    try {
                        Thread.sleep(250);
                    } catch (InterruptedException e) {
                    }

                    continue; // retry
                }
            } else {

                int i = this.currentHostIndex + 1;
                boolean foundGoodHost = false;

                for (; i < numHosts; i++) {
                    if (!blockList.containsKey(configuredHosts.get(i))) {
                        this.currentHostIndex = i;
                        foundGoodHost = true;
                        break;
                    }
                }

                if (!foundGoodHost) {
                    for (i = 0; i < this.currentHostIndex; i++) {
                        if (!blockList.containsKey(configuredHosts.get(i))) {
                            this.currentHostIndex = i;
                            foundGoodHost = true;
                            break;
                        }
                    }
                }

                if (!foundGoodHost) {
                    blockList = ((LoadBalancedConnectionProxy) proxy).getGlobalBlocklist(); // it may have changed
                    // and the proxy returns a copy

                    try {
                        Thread.sleep(250);
                    } catch (InterruptedException e) {
                    }

                    continue; // retry
                }
            }

            String hostPortSpec = configuredHosts.get(this.currentHostIndex);

            ConnectionImpl conn = (ConnectionImpl) liveConnections.get(hostPortSpec);

            if (conn == null) {
                try {
                    conn = ((LoadBalancedConnectionProxy) proxy).createConnectionForHost(hostPortSpec);
                } catch (SQLException sqlEx) {
                    ex = sqlEx;

                    if (((LoadBalancedConnectionProxy) proxy).shouldExceptionTriggerConnectionSwitch(sqlEx)) {

                        ((LoadBalancedConnectionProxy) proxy).addToGlobalBlocklist(hostPortSpec);

                        try {
                            Thread.sleep(250);
                        } catch (InterruptedException e) {
                        }

                        continue;
                    }
                    throw sqlEx;
                }
            }

            return conn;
        }

        if (ex != null) {
            throw ex;
        }

        return null; // we won't get here, compiler can't tell
    }