Java Code Examples for com.mysql.cj.conf.HostInfo#getHostPortPair()

The following examples show how to use com.mysql.cj.conf.HostInfo#getHostPortPair() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LoadBalancedConnectionProxy.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Removes a host from the host list.
 * 
 * @param hostPortPair
 *            The host to be removed.
 * @throws SQLException
 */
public synchronized void removeHost(String hostPortPair) throws SQLException {
    if (this.connectionGroup != null) {
        if (this.connectionGroup.getInitialHosts().size() == 1 && this.connectionGroup.getInitialHosts().contains(hostPortPair)) {
            throw SQLError.createSQLException(Messages.getString("LoadBalancedConnectionProxy.0"), null);
        }
    }

    this.hostsToRemove.add(hostPortPair);

    this.connectionsToHostsMap.remove(this.liveConnections.remove(hostPortPair));
    if (this.hostsToListIndexMap.remove(hostPortPair) != null) {
        long[] newResponseTimes = new long[this.responseTimes.length - 1];
        int newIdx = 0;
        for (HostInfo hostInfo : this.hostsList) {
            String host = hostInfo.getHostPortPair();
            if (!this.hostsToRemove.contains(host)) {
                Integer idx = this.hostsToListIndexMap.get(host);
                if (idx != null && idx < this.responseTimes.length) {
                    newResponseTimes[newIdx] = this.responseTimes[idx];
                }
                this.hostsToListIndexMap.put(host, newIdx++);
            }
        }
        this.responseTimes = newResponseTimes;
    }

    if (hostPortPair.equals(this.currentConnection.getHostPortPair())) {
        invalidateConnection(this.currentConnection);
        pickNewConnection();
    }
}
 
Example 2
Source File: LoadBalancedConnectionProxy.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Removes a host from the host list.
 * 
 * @param hostPortPair
 *            The host to be removed.
 * @throws SQLException
 *             if an error occurs
 */
public synchronized void removeHost(String hostPortPair) throws SQLException {
    if (this.connectionGroup != null) {
        if (this.connectionGroup.getInitialHosts().size() == 1 && this.connectionGroup.getInitialHosts().contains(hostPortPair)) {
            throw SQLError.createSQLException(Messages.getString("LoadBalancedConnectionProxy.0"), null);
        }
    }

    this.hostsToRemove.add(hostPortPair);

    this.connectionsToHostsMap.remove(this.liveConnections.remove(hostPortPair));
    if (this.hostsToListIndexMap.remove(hostPortPair) != null) {
        long[] newResponseTimes = new long[this.responseTimes.length - 1];
        int newIdx = 0;
        for (HostInfo hostInfo : this.hostsList) {
            String host = hostInfo.getHostPortPair();
            if (!this.hostsToRemove.contains(host)) {
                Integer idx = this.hostsToListIndexMap.get(host);
                if (idx != null && idx < this.responseTimes.length) {
                    newResponseTimes[newIdx] = this.responseTimes[idx];
                }
                this.hostsToListIndexMap.put(host, newIdx++);
            }
        }
        this.responseTimes = newResponseTimes;
    }

    if (hostPortPair.equals(this.currentConnection.getHostPortPair())) {
        invalidateConnection(this.currentConnection);
        pickNewConnection();
    }
}
 
Example 3
Source File: LoadBalancedConnectionProxy.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Pings live connections.
 */
public synchronized void doPing() throws SQLException {
    SQLException se = null;
    boolean foundHost = false;
    int pingTimeout = this.currentConnection.getPropertySet().getIntegerReadableProperty(PropertyDefinitions.PNAME_loadBalancePingTimeout).getValue();

    synchronized (this) {
        for (HostInfo hi : this.hostsList) {
            String host = hi.getHostPortPair();
            ConnectionImpl conn = this.liveConnections.get(host);
            if (conn == null) {
                continue;
            }
            try {
                if (pingTimeout == 0) {
                    conn.ping();
                } else {
                    conn.pingInternal(true, pingTimeout);
                }
                foundHost = true;
            } catch (SQLException e) {
                // give up if it is the current connection, otherwise NPE faking resultset later.
                if (host.equals(this.connectionsToHostsMap.get(this.currentConnection))) {
                    // clean up underlying connections, since connection pool won't do it
                    closeAllConnections();
                    this.isClosed = true;
                    this.closedReason = "Connection closed because ping of current connection failed.";
                    throw e;
                }

                // if the Exception is caused by ping connection lifetime checks, don't add to blacklist
                if (e.getMessage().equals(Messages.getString("Connection.exceededConnectionLifetime"))) {
                    // only set the return Exception if it's null
                    if (se == null) {
                        se = e;
                    }
                } else {
                    // overwrite the return Exception no matter what
                    se = e;
                    if (isGlobalBlacklistEnabled()) {
                        addToGlobalBlacklist(host);
                    }
                }
                // take the connection out of the liveConnections Map
                this.liveConnections.remove(this.connectionsToHostsMap.get(conn));
            }
        }
    }
    // if there were no successful pings
    if (!foundHost) {
        closeAllConnections();
        this.isClosed = true;
        this.closedReason = "Connection closed due to inability to ping any active connections.";
        // throw the stored Exception, if exists
        if (se != null) {
            throw se;
        }
        // or create a new SQLException and throw it, must be no liveConnections
        ((ConnectionImpl) this.currentConnection).throwConnectionClosedException();
    }
}
 
Example 4
Source File: LoadBalancedConnectionProxy.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Pings live connections.
 * 
 * @throws SQLException
 *             if an error occurs
 */
@Override
public synchronized void doPing() throws SQLException {
    SQLException se = null;
    boolean foundHost = false;
    int pingTimeout = this.currentConnection.getPropertySet().getIntegerProperty(PropertyKey.loadBalancePingTimeout).getValue();

    synchronized (this) {
        for (HostInfo hi : this.hostsList) {
            String host = hi.getHostPortPair();
            ConnectionImpl conn = this.liveConnections.get(host);
            if (conn == null) {
                continue;
            }
            try {
                if (pingTimeout == 0) {
                    conn.ping();
                } else {
                    conn.pingInternal(true, pingTimeout);
                }
                foundHost = true;
            } catch (SQLException e) {
                // give up if it is the current connection, otherwise NPE faking resultset later.
                if (host.equals(this.connectionsToHostsMap.get(this.currentConnection))) {
                    // clean up underlying connections, since connection pool won't do it
                    closeAllConnections();
                    this.isClosed = true;
                    this.closedReason = "Connection closed because ping of current connection failed.";
                    throw e;
                }

                // if the Exception is caused by ping connection lifetime checks, don't add to blacklist
                if (e.getMessage().equals(Messages.getString("Connection.exceededConnectionLifetime"))) {
                    // only set the return Exception if it's null
                    if (se == null) {
                        se = e;
                    }
                } else {
                    // overwrite the return Exception no matter what
                    se = e;
                    if (isGlobalBlacklistEnabled()) {
                        addToGlobalBlacklist(host);
                    }
                }
                // take the connection out of the liveConnections Map
                this.liveConnections.remove(this.connectionsToHostsMap.get(conn));
            }
        }
    }
    // if there were no successful pings
    if (!foundHost) {
        closeAllConnections();
        this.isClosed = true;
        this.closedReason = "Connection closed due to inability to ping any active connections.";
        // throw the stored Exception, if exists
        if (se != null) {
            throw se;
        }
        // or create a new SQLException and throw it, must be no liveConnections
        ((ConnectionImpl) this.currentConnection).throwConnectionClosedException();
    }
}