com.mysql.cj.conf.HostInfo Java Examples

The following examples show how to use com.mysql.cj.conf.HostInfo. 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: CoreSession.java    From FoxTelem with GNU General Public License v3.0 6 votes vote down vote up
public CoreSession(HostInfo hostInfo, PropertySet propSet) {
    this.connectionCreationTimeMillis = System.currentTimeMillis();
    this.hostInfo = hostInfo;
    this.propertySet = propSet;

    this.gatherPerfMetrics = getPropertySet().getBooleanProperty(PropertyKey.gatherPerfMetrics);
    this.characterEncoding = getPropertySet().getStringProperty(PropertyKey.characterEncoding);
    this.disconnectOnExpiredPasswords = getPropertySet().getBooleanProperty(PropertyKey.disconnectOnExpiredPasswords);
    this.cacheServerConfiguration = getPropertySet().getBooleanProperty(PropertyKey.cacheServerConfiguration);
    this.autoReconnect = getPropertySet().getBooleanProperty(PropertyKey.autoReconnect);
    this.autoReconnectForPools = getPropertySet().getBooleanProperty(PropertyKey.autoReconnectForPools);
    this.maintainTimeStats = getPropertySet().getBooleanProperty(PropertyKey.maintainTimeStats);

    this.log = LogFactory.getLogger(getPropertySet().getStringProperty(PropertyKey.logger).getStringValue(), Log.LOGGER_INSTANCE_NAME);
    if (getPropertySet().getBooleanProperty(PropertyKey.profileSQL).getValue()
            || getPropertySet().getBooleanProperty(PropertyKey.useUsageAdvisor).getValue()) {
        ProfilerEventHandlerFactory.getInstance(this);
    }
}
 
Example #2
Source File: ReplicationConnectionProxy.java    From FoxTelem with GNU General Public License v3.0 6 votes vote down vote up
public synchronized void promoteSlaveToMaster(String hostPortPair) throws SQLException {
    HostInfo host = getSlaveHost(hostPortPair);
    if (host == null) {
        return;
    }
    this.masterHosts.add(host);
    removeSlave(hostPortPair);
    if (this.masterConnection != null) {
        this.masterConnection.addHost(hostPortPair);
    }

    // Switch back to the masters connection if this connection was running in fail-safe mode.
    if (!this.readOnly && !isMasterConnection()) {
        switchToMasterConnection();
    }
}
 
Example #3
Source File: LoadBalancedConnectionProxy.java    From FoxTelem with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates a new physical connection for the given {@link HostInfo} and updates required internal mappings and statistics for that connection.
 * 
 * @param hostInfo
 *            The host info instance.
 * @return
 *         The new Connection instance.
 * @throws SQLException
 *             if an error occurs
 */
@Override
public synchronized ConnectionImpl createConnectionForHost(HostInfo hostInfo) throws SQLException {
    ConnectionImpl conn = super.createConnectionForHost(hostInfo);

    this.liveConnections.put(hostInfo.getHostPortPair(), conn);
    this.connectionsToHostsMap.put(conn, hostInfo.getHostPortPair());

    this.totalPhysicalConnections++;

    for (QueryInterceptor stmtInterceptor : conn.getQueryInterceptorsInstances()) {
        if (stmtInterceptor instanceof LoadBalancedAutoCommitInterceptor) {
            ((LoadBalancedAutoCommitInterceptor) stmtInterceptor).resumeCounters();
            break;
        }
    }

    return conn;
}
 
Example #4
Source File: SessionFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates {@link Session} by given URL.
 * 
 * @param url
 *            the session URL.
 * @return a {@link Session} instance.
 */
public Session getSession(String url) {
    CJCommunicationsException latestException = null;
    ConnectionUrl connUrl = parseUrl(url);
    for (HostInfo hi : connUrl.getHostsList()) {
        try {
            return new SessionImpl(hi);
        } catch (CJCommunicationsException e) {
            latestException = e;
        }
    }
    if (latestException != null) {
        throw latestException;
    }
    return null;
}
 
Example #5
Source File: MysqlxSession.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public MysqlxSession(HostInfo hostInfo, PropertySet propSet) {
    super(hostInfo, propSet);

    // create protocol instance
    this.host = hostInfo.getHost();
    if (this.host == null || StringUtils.isEmptyOrWhitespaceOnly(this.host)) {
        this.host = "localhost";
    }
    this.port = hostInfo.getPort();
    if (this.port < 0) {
        this.port = 33060;
    }

    this.protocol = XProtocol.getInstance(this.host, this.port, propSet);

    this.messageBuilder = this.protocol.getMessageBuilder();

    this.protocol.connect(hostInfo.getUser(), hostInfo.getPassword(), hostInfo.getDatabase());
}
 
Example #6
Source File: XProtocol.java    From FoxTelem with GNU General Public License v3.0 6 votes vote down vote up
public XProtocol(HostInfo hostInfo, PropertySet propertySet) {
    String host = hostInfo.getHost();
    if (host == null || StringUtils.isEmptyOrWhitespaceOnly(host)) {
        host = "localhost";
    }
    int port = hostInfo.getPort();
    if (port < 0) {
        port = 33060;
    }
    this.defaultSchemaName = hostInfo.getDatabase();

    // Override common connectTimeout with xdevapi.connect-timeout to provide unified logic in StandardSocketFactory
    RuntimeProperty<Integer> connectTimeout = propertySet.getIntegerProperty(PropertyKey.connectTimeout);
    RuntimeProperty<Integer> xdevapiConnectTimeout = propertySet.getIntegerProperty(PropertyKey.xdevapiConnectTimeout);
    if (xdevapiConnectTimeout.isExplicitlySet() || !connectTimeout.isExplicitlySet()) {
        connectTimeout.setValue(xdevapiConnectTimeout.getValue());
    }

    SocketConnection socketConn = propertySet.getBooleanProperty(PropertyKey.xdevapiUseAsyncProtocol).getValue() ? new XAsyncSocketConnection()
            : new NativeSocketConnection();
    socketConn.connect(host, port, propertySet, null, null, 0);
    init(null, socketConn, propertySet, null);
}
 
Example #7
Source File: CoreSession.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public CoreSession(HostInfo hostInfo, PropertySet propSet) {
    this.connectionCreationTimeMillis = System.currentTimeMillis();
    this.hostInfo = hostInfo;
    this.propertySet = propSet;

    this.gatherPerfMetrics = getPropertySet().getBooleanReadableProperty(PropertyDefinitions.PNAME_gatherPerfMetrics);
    this.characterEncoding = getPropertySet().getModifiableProperty(PropertyDefinitions.PNAME_characterEncoding);
    this.useOldUTF8Behavior = getPropertySet().getBooleanReadableProperty(PropertyDefinitions.PNAME_useOldUTF8Behavior);
    this.disconnectOnExpiredPasswords = getPropertySet().getBooleanReadableProperty(PropertyDefinitions.PNAME_disconnectOnExpiredPasswords);
    this.cacheServerConfiguration = getPropertySet().getBooleanReadableProperty(PropertyDefinitions.PNAME_cacheServerConfiguration);
    this.autoReconnect = getPropertySet().<Boolean> getModifiableProperty(PropertyDefinitions.PNAME_autoReconnect);
    this.autoReconnectForPools = getPropertySet().getBooleanReadableProperty(PropertyDefinitions.PNAME_autoReconnectForPools);
    this.maintainTimeStats = getPropertySet().getBooleanReadableProperty(PropertyDefinitions.PNAME_maintainTimeStats);

    this.log = LogFactory.getLogger(getPropertySet().getStringReadableProperty(PropertyDefinitions.PNAME_logger).getStringValue(),
            Log.LOGGER_INSTANCE_NAME);
    if (getPropertySet().getBooleanReadableProperty(PropertyDefinitions.PNAME_profileSQL).getValue()
            || getPropertySet().getBooleanReadableProperty(PropertyDefinitions.PNAME_useUsageAdvisor).getValue()) {
        ProfilerEventHandlerFactory.getInstance(this);
    }
}
 
Example #8
Source File: ReplicationConnectionProxy.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public synchronized void promoteSlaveToMaster(String hostPortPair) throws SQLException {
    HostInfo host = getSlaveHost(hostPortPair);
    if (host == null) {
        return;
    }
    this.masterHosts.add(host);
    removeSlave(hostPortPair);
    if (this.masterConnection != null) {
        this.masterConnection.addHost(hostPortPair);
    }

    // Switch back to the masters connection if this connection was running in fail-safe mode.
    if (!this.readOnly && !isMasterConnection()) {
        switchToMasterConnection();
    }
}
 
Example #9
Source File: ConnectionUrlTest.java    From FoxTelem with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Tests default values.
 */
@Test
public void testDefaultValues() {
    Map<String, Integer> connStr = new HashMap<>();
    connStr.put("jdbc:mysql:", 3306);
    connStr.put("jdbc:mysql://,", 3306);
    connStr.put("jdbc:mysql:loadbalance://,", 3306);
    connStr.put("jdbc:mysql:replication://,", 3306);
    connStr.put("mysqlx:", 33060);

    for (String cs : connStr.keySet()) {
        ConnectionUrl connUrl = ConnectionUrl.getConnectionUrlInstance(cs, null);
        for (HostInfo hi : connUrl.getHostsList()) {
            assertEquals(cs + "#databaseUrl", cs, hi.getDatabaseUrl());
            assertEquals(cs + "#host", "localhost", hi.getHost());
            assertEquals(cs + "#port", connStr.get(cs).intValue(), hi.getPort());
            assertEquals(cs + "#hostPortPair", "localhost:" + connStr.get(cs), hi.getHostPortPair());
            assertEquals(cs + "#user", "", hi.getUser());
            assertEquals(cs + "#password", "", hi.getPassword());
            assertEquals(cs + "#database", "", hi.getDatabase());
        }
    }
}
 
Example #10
Source File: ConnectionUrlTest.java    From FoxTelem with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Tests fix for BUG#28150662, CONNECTOR/J 8 MALFORMED DATABASE URL EXCEPTION WHIT CORRECT URL STRING.
 */
@Test
public void testBug28150662() {
    List<String> connStr = new ArrayList<>();
    connStr.add(
            "jdbc:mysql://localhost:3306/db1?connectionCollation=utf8mb4_unicode_ci&user=user1&sessionVariables=sql_mode='IGNORE_SPACE,ANSI',FOREIGN_KEY_CHECKS=0");
    connStr.add(
            "jdbc:mysql://localhost:3306/db1?connectionCollation=utf8mb4_unicode_ci&sessionVariables=sql_mode='IGNORE_SPACE,ANSI',FOREIGN_KEY_CHECKS=0&user=user1");
    connStr.add(
            "jdbc:mysql://address=(host=localhost)(port=3306)(connectionCollation=utf8mb4_unicode_ci)(sessionVariables=sql_mode='IGNORE_SPACE,ANSI',FOREIGN_KEY_CHECKS=0)(user=user1)/db1");
    connStr.add(
            "jdbc:mysql://(host=localhost,port=3306,connectionCollation=utf8mb4_unicode_ci,sessionVariables=sql_mode='IGNORE_SPACE%2CANSI'%2CFOREIGN_KEY_CHECKS=0,user=user1)/db1");

    for (String cs : connStr) {
        ConnectionUrl url = ConnectionUrl.getConnectionUrlInstance(cs, null);
        HostInfo hi = url.getMainHost();
        assertEquals("utf8mb4_unicode_ci", hi.getHostProperties().get("connectionCollation"));
        assertEquals("user1", hi.getUser());
        assertEquals("sql_mode='IGNORE_SPACE,ANSI',FOREIGN_KEY_CHECKS=0", hi.getHostProperties().get("sessionVariables"));
    }
}
 
Example #11
Source File: LoadBalancedConnectionProxy.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a new physical connection for the given {@link HostInfo} and updates required internal mappings and statistics for that connection.
 * 
 * @param hostInfo
 *            The host info instance.
 * @return
 *         The new Connection instance.
 */
@Override
public synchronized ConnectionImpl createConnectionForHost(HostInfo hostInfo) throws SQLException {
    ConnectionImpl conn = super.createConnectionForHost(hostInfo);

    this.liveConnections.put(hostInfo.getHostPortPair(), conn);
    this.connectionsToHostsMap.put(conn, hostInfo.getHostPortPair());

    this.totalPhysicalConnections++;

    for (QueryInterceptor stmtInterceptor : conn.getQueryInterceptorsInstances()) {
        if (stmtInterceptor instanceof LoadBalancedAutoCommitInterceptor) {
            ((LoadBalancedAutoCommitInterceptor) stmtInterceptor).resumeCounters();
            break;
        }
    }

    return conn;
}
 
Example #12
Source File: ReplicationConnectionProxy.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public synchronized void removeSlave(String hostPortPair, boolean closeGently) throws SQLException {
    HostInfo host = getSlaveHost(hostPortPair);
    if (host == null) {
        return;
    }
    this.slaveHosts.remove(host);
    resetReadFromMasterWhenNoSlaves();

    if (this.slavesConnection == null || this.slavesConnection.isClosed()) {
        this.slavesConnection = null;
        return;
    }

    if (closeGently) {
        this.slavesConnection.removeHostWhenNotInUse(hostPortPair);
    } else {
        this.slavesConnection.removeHost(hostPortPair);
    }

    // Close the connection if that was the last slave.
    if (this.slaveHosts.isEmpty()) {
        this.slavesConnection.close();
        this.slavesConnection = null;

        // Default behavior, no need to check this.readFromMasterWhenNoSlaves.
        switchToMasterConnection();
        if (isMasterConnection()) {
            this.currentConnection.setReadOnly(this.readOnly); // Maintain.
        }
    }
}
 
Example #13
Source File: MySQLBackupProcessor.java    From gocd with Apache License 2.0 5 votes vote down vote up
private ProcessExecutor createProcessExecutor(File targetDir, DbProperties dbProperties) {
    ConnectionUrl connectionUrlInstance = ConnectionUrl.getConnectionUrlInstance(dbProperties.url(), dbProperties.connectionProperties());

    LinkedHashMap<String, String> env = new LinkedHashMap<>();
    if (isNotBlank(dbProperties.password())) {
        env.put("MYSQL_PWD", dbProperties.password());
    }
    // override with any user specified environment
    env.putAll(dbProperties.extraBackupEnv());

    ArrayList<String> argv = new ArrayList<>();
    argv.add("mysqldump");


    String dbName = connectionUrlInstance.getDatabase();
    HostInfo mainHost = connectionUrlInstance.getMainHost();

    if (mainHost != null) {
        argv.add("--host=" + mainHost.getHost());
        argv.add("--port=" + mainHost.getPort());
    }
    if (isNotBlank(dbProperties.user())) {
        argv.add("--user=" + dbProperties.user());
    }

    // append any user specified args for mysqldump
    if (isNotBlank(dbProperties.extraBackupCommandArgs())) {
        Collections.addAll(argv, Commandline.translateCommandline(dbProperties.extraBackupCommandArgs()));
    }

    argv.add("--result-file=" + new File(targetDir, "db." + dbName).toString());
    argv.add(connectionUrlInstance.getDatabase());

    ProcessExecutor processExecutor = new ProcessExecutor();
    processExecutor.redirectOutputAlsoTo(Slf4jStream.of(getClass()).asDebug());
    processExecutor.redirectErrorAlsoTo(Slf4jStream.of(getClass()).asDebug());
    processExecutor.environment(env);
    processExecutor.command(argv);
    return processExecutor;
}
 
Example #14
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 #15
Source File: NativeSession.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void connect(HostInfo hi, String user, String password, String database, int loginTimeout, TransactionEventHandler transactionManager)
        throws IOException {

    this.hostInfo = hi;

    // reset max-rows to default value
    this.setSessionMaxRows(-1);

    // TODO do we need different types of physical connections?
    SocketConnection socketConnection = new NativeSocketConnection();
    socketConnection.connect(this.hostInfo.getHost(), this.hostInfo.getPort(), this.propertySet, getExceptionInterceptor(), this.log, loginTimeout);

    // we use physical connection to create a -> protocol
    // this configuration places no knowledge of protocol or session on physical connection.
    // physical connection is responsible *only* for I/O streams
    if (this.protocol == null) {
        this.protocol = NativeProtocol.getInstance(this, socketConnection, this.propertySet, this.log, transactionManager);
    } else {
        this.protocol.init(this, socketConnection, this.propertySet, transactionManager);
    }

    // use protocol to create a -> session
    // protocol is responsible for building a session and authenticating (using AuthenticationProvider) internally
    this.protocol.connect(user, password, database);

    // error messages are returned according to character_set_results which, at this point, is set from the response packet
    this.protocol.getServerSession().setErrorMessageEncoding(this.protocol.getAuthenticationProvider().getEncodingForHandshake());

    this.isClosed = false;
}
 
Example #16
Source File: MultiHostConnectionProxy.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Initializes the hosts lists and makes a "clean" local copy of the given connection properties so that it can be later used to create standard
 * connections.
 * 
 * @param connUrl
 *            The connection URL that initialized this multi-host connection.
 * @param hosts
 *            The list of hosts for this multi-host connection.
 * @return
 *         The number of hosts found in the hosts list.
 */
int initializeHostsSpecs(ConnectionUrl connUrl, List<HostInfo> hosts) {
    this.connectionUrl = connUrl;

    Properties props = connUrl.getConnectionArgumentsAsProperties();

    this.autoReconnect = "true".equalsIgnoreCase(props.getProperty(PropertyKey.autoReconnect.getKeyName()))
            || "true".equalsIgnoreCase(props.getProperty(PropertyKey.autoReconnectForPools.getKeyName()));

    this.hostsList = new ArrayList<>(hosts);
    int numHosts = this.hostsList.size();
    return numHosts;
}
 
Example #17
Source File: ReplicationConnectionProxy.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public synchronized void removeMasterHost(String hostPortPair, boolean waitUntilNotInUse, boolean isNowSlave) throws SQLException {
    HostInfo host = getMasterHost(hostPortPair);
    if (host == null) {
        return;
    }
    if (isNowSlave) {
        this.slaveHosts.add(host);
        resetReadFromMasterWhenNoSlaves();
    }
    this.masterHosts.remove(host);

    // The master connection may have been implicitly closed by a previous op., don't let it stop us.
    if (this.masterConnection == null || this.masterConnection.isClosed()) {
        this.masterConnection = null;
        return;
    }

    if (waitUntilNotInUse) {
        this.masterConnection.removeHostWhenNotInUse(hostPortPair);
    } else {
        this.masterConnection.removeHost(hostPortPair);
    }

    // Close the connection if that was the last master.
    if (this.masterHosts.isEmpty()) {
        this.masterConnection.close();
        this.masterConnection = null;

        // Default behavior, no need to check this.readFromMasterWhenNoSlaves.
        switchToSlavesConnectionIfNecessary();
    }
}
 
Example #18
Source File: SessionImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public SessionImpl(HostInfo hostInfo) {
    PropertySet pset = new DefaultPropertySet();
    pset.initializeProperties(hostInfo.exposeAsProperties());
    this.session = new MysqlxSession(hostInfo, pset);
    this.defaultSchemaName = hostInfo.getDatabase();
    this.xbuilder = (XMessageBuilder) this.session.<XMessage> getMessageBuilder();
}
 
Example #19
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 #20
Source File: ReplicationConnectionProxy.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
public synchronized void removeMasterHost(String hostPortPair, boolean waitUntilNotInUse, boolean isNowSlave) throws SQLException {
    HostInfo host = getMasterHost(hostPortPair);
    if (host == null) {
        return;
    }
    if (isNowSlave) {
        this.slaveHosts.add(host);
        resetReadFromMasterWhenNoSlaves();
    }
    this.masterHosts.remove(host);

    // The master connection may have been implicitly closed by a previous op., don't let it stop us.
    if (this.masterConnection == null || this.masterConnection.isClosed()) {
        this.masterConnection = null;
        return;
    }

    if (waitUntilNotInUse) {
        this.masterConnection.removeHostWhenNotInUse(hostPortPair);
    } else {
        this.masterConnection.removeHost(hostPortPair);
    }

    // Close the connection if that was the last master.
    if (this.masterHosts.isEmpty()) {
        this.masterConnection.close();
        this.masterConnection = null;

        // Default behavior, no need to check this.readFromMasterWhenNoSlaves.
        switchToSlavesConnectionIfNecessary();
    }
}
 
Example #21
Source File: ReplicationConnectionProxy.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
public synchronized void removeSlave(String hostPortPair, boolean closeGently) throws SQLException {
    HostInfo host = getSlaveHost(hostPortPair);
    if (host == null) {
        return;
    }
    this.slaveHosts.remove(host);
    resetReadFromMasterWhenNoSlaves();

    if (this.slavesConnection == null || this.slavesConnection.isClosed()) {
        this.slavesConnection = null;
        return;
    }

    if (closeGently) {
        this.slavesConnection.removeHostWhenNotInUse(hostPortPair);
    } else {
        this.slavesConnection.removeHost(hostPortPair);
    }

    // Close the connection if that was the last slave.
    if (this.slaveHosts.isEmpty()) {
        this.slavesConnection.close();
        this.slavesConnection = null;

        // Default behavior, no need to check this.readFromMasterWhenNoSlaves.
        switchToMasterConnection();
        if (isMasterConnection()) {
            this.currentConnection.setReadOnly(this.readOnly); // Maintain.
        }
    }
}
 
Example #22
Source File: SessionImpl.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Constructor.
 * 
 * @param hostInfo
 *            {@link HostInfo} instance
 */
public SessionImpl(HostInfo hostInfo) {
    PropertySet pset = new DefaultPropertySet();
    pset.initializeProperties(hostInfo.exposeAsProperties());
    this.session = new MysqlxSession(hostInfo, pset);
    this.defaultSchemaName = hostInfo.getDatabase();
    this.xbuilder = (XMessageBuilder) this.session.<XMessage> getMessageBuilder();
}
 
Example #23
Source File: SessionFactory.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates {@link Session} by given URL.
 * 
 * @param connUrl
 *            the session {@link ConnectionUrl}.
 * @return a {@link Session} instance.
 */
protected Session getSession(ConnectionUrl connUrl) {
    CJCommunicationsException latestException = null;
    for (HostInfo hi : connUrl.getHostsList()) {
        try {
            return new SessionImpl(hi);
        } catch (CJCommunicationsException e) {
            latestException = e;
        }
    }
    if (latestException != null) {
        throw latestException;
    }
    return null;
}
 
Example #24
Source File: NativeSession.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
public void connect(HostInfo hi, String user, String password, String database, int loginTimeout, TransactionEventHandler transactionManager)
        throws IOException {

    this.hostInfo = hi;

    // reset max-rows to default value
    this.setSessionMaxRows(-1);

    // TODO do we need different types of physical connections?
    SocketConnection socketConnection = new NativeSocketConnection();
    socketConnection.connect(this.hostInfo.getHost(), this.hostInfo.getPort(), this.propertySet, getExceptionInterceptor(), this.log, loginTimeout);

    // we use physical connection to create a -> protocol
    // this configuration places no knowledge of protocol or session on physical connection.
    // physical connection is responsible *only* for I/O streams
    if (this.protocol == null) {
        this.protocol = NativeProtocol.getInstance(this, socketConnection, this.propertySet, this.log, transactionManager);
    } else {
        this.protocol.init(this, socketConnection, this.propertySet, transactionManager);
    }

    // use protocol to create a -> session
    // protocol is responsible for building a session and authenticating (using AuthenticationProvider) internally
    this.protocol.connect(user, password, database);

    // error messages are returned according to character_set_results which, at this point, is set from the response packet
    this.protocol.getServerSession().setErrorMessageEncoding(this.protocol.getAuthenticationProvider().getEncodingForHandshake());

    this.isClosed = false;
}
 
Example #25
Source File: BaseTestCase.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
protected Connection getUnreliableMultiHostConnection(String haMode, String[] hostNames, Properties props, Set<String> downedHosts) throws Exception {
    if (downedHosts == null) {
        downedHosts = new HashSet<>();
    }

    props = getHostFreePropertiesFromTestsuiteUrl(props);
    props.setProperty(PropertyKey.socketFactory.getKeyName(), "testsuite.UnreliableSocketFactory");

    HostInfo defaultHost = mainConnectionUrl.getMainHost();
    String db = defaultHost.getDatabase();
    String port = String.valueOf(defaultHost.getPort());
    String host = defaultHost.getHost();

    UnreliableSocketFactory.flushAllStaticData();

    StringBuilder hostString = new StringBuilder();
    String delimiter = "";
    for (String hostName : hostNames) {
        UnreliableSocketFactory.mapHost(hostName, host);
        hostString.append(delimiter);
        delimiter = ",";
        hostString.append(hostName + ":" + port);

        if (downedHosts.contains(hostName)) {
            UnreliableSocketFactory.downHost(hostName);
        }
    }

    if (haMode == null) {
        haMode = "";
    } else if (haMode.length() > 0) {
        haMode += ":";
    }

    return getConnectionWithProps(ConnectionUrl.Type.FAILOVER_CONNECTION.getScheme() + haMode + "//" + hostString.toString() + "/" + db, props);
}
 
Example #26
Source File: BaseTestCase.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
protected ReplicationConnection getUnreliableReplicationConnection(Set<MockConnectionConfiguration> configs, Properties props) throws Exception {
    props = getHostFreePropertiesFromTestsuiteUrl(props);
    props.setProperty(PropertyKey.socketFactory.getKeyName(), "testsuite.UnreliableSocketFactory");

    HostInfo defaultHost = mainConnectionUrl.getMainHost();
    String db = defaultHost.getDatabase();
    String port = String.valueOf(defaultHost.getPort());
    String host = defaultHost.getHost();

    UnreliableSocketFactory.flushAllStaticData();

    StringBuilder hostString = new StringBuilder();
    String glue = "";
    for (MockConnectionConfiguration config : configs) {
        UnreliableSocketFactory.mapHost(config.hostName, host);
        hostString.append(glue);
        glue = ",";
        if (config.port == null) {
            config.port = (port == null ? "3306" : port);
        }
        hostString.append(config.getAddress());
        if (config.isDowned) {
            UnreliableSocketFactory.downHost(config.hostName);
        }
    }

    return (ReplicationConnection) getConnectionWithProps(ConnectionUrl.Type.REPLICATION_CONNECTION.getScheme() + "//" + hostString.toString() + "/" + db,
            props);
}
 
Example #27
Source File: ConnectionCreateInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public Object afterMethod(Class clazz, Method method, Object[] allArguments, Class<?>[] parameterTypes,
    Object ret) {
    if (ret instanceof EnhancedInstance) {
        final HostInfo hostInfo = (HostInfo) allArguments[0];
        ConnectionInfo connectionInfo = URLParser.parser(hostInfo.getDatabaseUrl());
        ((EnhancedInstance) ret).setSkyWalkingDynamicField(connectionInfo);
    }
    return ret;
}
 
Example #28
Source File: MultiHostConnectionProxy.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Initializes the hosts lists and makes a "clean" local copy of the given connection properties so that it can be later used to create standard
 * connections.
 * 
 * @param connUrl
 *            The connection URL that initialized this multi-host connection.
 * @param hosts
 *            The list of hosts for this multi-host connection.
 * @return
 *         The number of hosts found in the hosts list.
 */
int initializeHostsSpecs(ConnectionUrl connUrl, List<HostInfo> hosts) {
    this.connectionUrl = connUrl;

    Properties props = connUrl.getConnectionArgumentsAsProperties();

    this.autoReconnect = "true".equalsIgnoreCase(props.getProperty(PropertyDefinitions.PNAME_autoReconnect))
            || "true".equalsIgnoreCase(props.getProperty(PropertyDefinitions.PNAME_autoReconnectForPools));

    this.hostsList = new ArrayList<>(hosts);
    int numHosts = this.hostsList.size();
    return numHosts;
}
 
Example #29
Source File: ConnectionImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Creates a connection instance
 */
public static JdbcConnection getInstance(HostInfo hostInfo) throws SQLException {
    return new ConnectionImpl(hostInfo);
}
 
Example #30
Source File: ReplicationConnectionProxy.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private HostInfo getSlaveHost(String hostPortPair) {
    return this.slaveHosts.stream().filter(hi -> hostPortPair.equalsIgnoreCase(hi.getHostPortPair())).findFirst().orElse(null);
}