Search in sources :

Example 1 with DefaultHostConnection

use of com.ctrip.platform.dal.dao.datasource.cluster.DefaultHostConnection in project dal by ctripcorp.

the class RoundRobinAccessStrategyTest method pickNode_3_exceptionNode.

@Test
public void pickNode_3_exceptionNode() throws InterruptedException {
    RouteStrategy strategy = getRouteStrategy();
    strategy.init(shardMeta.configuredHosts(), caseInsensitiveProperties);
    routeStrategy.interceptException(new SQLException("test"), new DefaultHostConnection(null, HostSpecOY_1));
    HostSpec hostSpec1 = strategy.pickNode(dalHints);
    HostSpec hostSpec2 = strategy.pickNode(dalHints);
    Assert.assertEquals(HostSpecOY_2, hostSpec1);
    Assert.assertEquals(HostSpecOY_2, hostSpec2);
    TimeUnit.MILLISECONDS.sleep(custom_black_list_timeout + 1);
    // remove from black list
    hostSpec1 = strategy.pickNode(dalHints);
    hostSpec2 = strategy.pickNode(dalHints);
    Assert.assertNotEquals(hostSpec1, hostSpec2);
    strategy.dispose();
}
Also used : SQLException(java.sql.SQLException) DefaultHostConnection(com.ctrip.platform.dal.dao.datasource.cluster.DefaultHostConnection) HostSpec(com.ctrip.framework.dal.cluster.client.base.HostSpec) Test(org.junit.Test)

Example 2 with DefaultHostConnection

use of com.ctrip.platform.dal.dao.datasource.cluster.DefaultHostConnection in project dal by ctripcorp.

the class DalConnectionPool method tryValidateClusterConnection.

private void tryValidateClusterConnection(PooledConnection conn) {
    if (clusterConnValidator != null) {
        boolean isValid = true;
        try {
            PoolConfiguration config = getPoolProperties();
            HostConnection connection;
            if (config instanceof DalExtendedPoolConfiguration)
                connection = new DefaultHostConnection(getConnection(conn), ((DalExtendedPoolConfiguration) config).getHost());
            else
                connection = new DefaultHostConnection(getConnection(conn), null);
            isValid = clusterConnValidator.validate(connection);
        } catch (Throwable t) {
            logger.warn("tryValidateClusterConnection exception", t);
        }
        if (!isValid) {
            release(conn);
            throw new InvalidConnectionException("Created connection is invalid");
        }
    }
}
Also used : InvalidConnectionException(com.ctrip.platform.dal.exceptions.InvalidConnectionException) PoolConfiguration(org.apache.tomcat.jdbc.pool.PoolConfiguration) DalExtendedPoolConfiguration(com.ctrip.platform.dal.dao.configure.DalExtendedPoolConfiguration) HostConnection(com.ctrip.platform.dal.dao.datasource.cluster.HostConnection) DefaultHostConnection(com.ctrip.platform.dal.dao.datasource.cluster.DefaultHostConnection) DefaultHostConnection(com.ctrip.platform.dal.dao.datasource.cluster.DefaultHostConnection) DalExtendedPoolConfiguration(com.ctrip.platform.dal.dao.configure.DalExtendedPoolConfiguration)

Example 3 with DefaultHostConnection

use of com.ctrip.platform.dal.dao.datasource.cluster.DefaultHostConnection in project dal by ctripcorp.

the class DataSourceValidator method tryValidateClusterConnection.

private void tryValidateClusterConnection(Connection connection, int validateAction) {
    if (clusterConnValidator != null) {
        boolean isValid = true;
        try {
            HostConnection conn;
            if (poolProperties instanceof DalExtendedPoolConfiguration)
                conn = new DefaultHostConnection(connection, ((DalExtendedPoolConfiguration) poolProperties).getHost());
            else
                conn = new DefaultHostConnection(connection, null);
            isValid = clusterConnValidator.validate(conn);
        } catch (Throwable t) {
            LOGGER.warn("tryValidateClusterConnection exception", t);
        }
        if (!isValid)
            throw new InvalidConnectionException("Borrowed connection is invalid");
    }
}
Also used : InvalidConnectionException(com.ctrip.platform.dal.exceptions.InvalidConnectionException) HostConnection(com.ctrip.platform.dal.dao.datasource.cluster.HostConnection) DefaultHostConnection(com.ctrip.platform.dal.dao.datasource.cluster.DefaultHostConnection) DefaultHostConnection(com.ctrip.platform.dal.dao.datasource.cluster.DefaultHostConnection) DalExtendedPoolConfiguration(com.ctrip.platform.dal.dao.configure.DalExtendedPoolConfiguration)

Example 4 with DefaultHostConnection

use of com.ctrip.platform.dal.dao.datasource.cluster.DefaultHostConnection in project dal by ctripcorp.

the class MajorityHostValidatorTest method failToNullConnection.

@Test
public void failToNullConnection() throws SQLException {
    MockMajorityHostValidator validator = new MockMajorityHostValidator(configuredHost, orderedHosts, failOverTime, blackListTimeOut, fixedValidatePeriod);
    DefaultHostConnection defaultHostConnection = new DefaultHostConnection(null, hostSpec1);
    assertEquals(false, validator.validate(defaultHostConnection));
    assertEquals(true, validator.available(hostSpec1));
}
Also used : MockDefaultHostConnection(com.ctrip.platform.dal.dao.base.MockDefaultHostConnection) DefaultHostConnection(com.ctrip.platform.dal.dao.datasource.cluster.DefaultHostConnection) Test(org.junit.Test)

Aggregations

DefaultHostConnection (com.ctrip.platform.dal.dao.datasource.cluster.DefaultHostConnection)4 DalExtendedPoolConfiguration (com.ctrip.platform.dal.dao.configure.DalExtendedPoolConfiguration)2 HostConnection (com.ctrip.platform.dal.dao.datasource.cluster.HostConnection)2 InvalidConnectionException (com.ctrip.platform.dal.exceptions.InvalidConnectionException)2 Test (org.junit.Test)2 HostSpec (com.ctrip.framework.dal.cluster.client.base.HostSpec)1 MockDefaultHostConnection (com.ctrip.platform.dal.dao.base.MockDefaultHostConnection)1 SQLException (java.sql.SQLException)1 PoolConfiguration (org.apache.tomcat.jdbc.pool.PoolConfiguration)1