Search in sources :

Example 1 with DalExtendedPoolConfiguration

use of com.ctrip.platform.dal.dao.configure.DalExtendedPoolConfiguration in project dal by ctripcorp.

the class DataSourceValidator method validate.

@Override
public boolean validate(Connection connection, int validateAction) {
    long startTime = System.currentTimeMillis();
    boolean isValid = false;
    String transactionName = "";
    try {
        PoolConfiguration poolConfig = getPoolProperties();
        DataSourceIdentity dataSourceId = poolConfig instanceof DalExtendedPoolConfiguration ? ((DalExtendedPoolConfiguration) poolConfig).getDataSourceId() : null;
        String connUrl = ConnectionUtils.getConnectionUrl(connection, poolConfig.getUrl());
        transactionName = String.format(CONNECTION_VALIDATE_CONNECTION_FORMAT, connUrl);
        tryValidateClusterConnection(connection, validateAction);
        isValid = validateConnection(connection, validateAction);
        LOGGER.logTransaction(DalLogTypes.DAL_DATASOURCE, transactionName, String.format(IS_VALID_FORMAT, isValid) + " Connection url: " + poolConfig.getUrl(), LogUtils.buildPropertiesFromDataSourceId(dataSourceId), startTime);
        if (!isValid) {
            LOGGER.warn(IS_VALID_RETURN_INFO);
        }
    } catch (InvalidConnectionException e) {
        handleException(isValid, e, transactionName, startTime);
        throw e;
    } catch (Throwable t) {
        handleException(isValid, t, transactionName, startTime);
    }
    return isValid;
}
Also used : InvalidConnectionException(com.ctrip.platform.dal.exceptions.InvalidConnectionException) PoolConfiguration(org.apache.tomcat.jdbc.pool.PoolConfiguration) DalExtendedPoolConfiguration(com.ctrip.platform.dal.dao.configure.DalExtendedPoolConfiguration) DalExtendedPoolConfiguration(com.ctrip.platform.dal.dao.configure.DalExtendedPoolConfiguration)

Example 2 with DalExtendedPoolConfiguration

use of com.ctrip.platform.dal.dao.configure.DalExtendedPoolConfiguration in project dal by ctripcorp.

the class DalConnectionPool method createConnection.

@Override
protected PooledConnection createConnection(long now, PooledConnection notUsed, String username, String password) throws SQLException {
    long startTime = System.currentTimeMillis();
    PooledConnection pooledConnection;
    PoolConfiguration poolConfig = getPoolProperties();
    DataSourceIdentity dataSourceId = poolConfig instanceof DalExtendedPoolConfiguration ? ((DalExtendedPoolConfiguration) poolConfig).getDataSourceId() : null;
    try {
        pooledConnection = super.createConnection(now, notUsed, username, password);
    } catch (Throwable e) {
        String connectionUrl = LoggerHelper.getSimplifiedDBUrl(getPoolProperties().getUrl());
        connectionListener.onCreateConnectionFailed(getName(), connectionUrl, dataSourceId, e, startTime);
        throw e;
    }
    try {
        connectionListener.onCreateConnection(getName(), getConnection(pooledConnection), dataSourceId, startTime);
    } catch (Throwable e) {
        logger.error("[createConnection]" + this, e);
    }
    preHandleConnection(pooledConnection, true);
    return pooledConnection;
}
Also used : PoolConfiguration(org.apache.tomcat.jdbc.pool.PoolConfiguration) DalExtendedPoolConfiguration(com.ctrip.platform.dal.dao.configure.DalExtendedPoolConfiguration) DataSourceIdentity(com.ctrip.platform.dal.dao.datasource.DataSourceIdentity) PooledConnection(org.apache.tomcat.jdbc.pool.PooledConnection) DalExtendedPoolConfiguration(com.ctrip.platform.dal.dao.configure.DalExtendedPoolConfiguration)

Example 3 with DalExtendedPoolConfiguration

use of com.ctrip.platform.dal.dao.configure.DalExtendedPoolConfiguration 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 4 with DalExtendedPoolConfiguration

use of com.ctrip.platform.dal.dao.configure.DalExtendedPoolConfiguration 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 5 with DalExtendedPoolConfiguration

use of com.ctrip.platform.dal.dao.configure.DalExtendedPoolConfiguration in project dal by ctripcorp.

the class DalConnectionPool method trySetSessionWaitTimeout.

private void trySetSessionWaitTimeout(Connection conn) {
    PoolConfiguration config = getPoolProperties();
    if (config instanceof DalExtendedPoolConfiguration && DatabaseCategory.MySql == DatabaseCategory.matchWithConnectionUrl(config.getUrl())) {
        int sessionWaitTimeout = ((DalExtendedPoolConfiguration) config).getSessionWaitTimeout();
        if (sessionWaitTimeout > 0) {
            String connUrl = ConnectionUtils.getConnectionUrl(conn, config.getUrl());
            String logName = String.format("Connection::setSessionWaitTimeout:%s", connUrl);
            try {
                logger.logTransaction(DalLogTypes.DAL_DATASOURCE, logName, String.format("sessionWaitTimeout: %ds. Connection url: %s", sessionWaitTimeout, config.getUrl()), () -> setSessionWaitTimeout(conn, sessionWaitTimeout));
            } catch (Throwable t) {
                logger.error("set sessionWaitTimeout exception: " + connUrl, t);
            }
        }
    }
}
Also used : PoolConfiguration(org.apache.tomcat.jdbc.pool.PoolConfiguration) DalExtendedPoolConfiguration(com.ctrip.platform.dal.dao.configure.DalExtendedPoolConfiguration) DalExtendedPoolConfiguration(com.ctrip.platform.dal.dao.configure.DalExtendedPoolConfiguration)

Aggregations

DalExtendedPoolConfiguration (com.ctrip.platform.dal.dao.configure.DalExtendedPoolConfiguration)5 PoolConfiguration (org.apache.tomcat.jdbc.pool.PoolConfiguration)4 InvalidConnectionException (com.ctrip.platform.dal.exceptions.InvalidConnectionException)3 DefaultHostConnection (com.ctrip.platform.dal.dao.datasource.cluster.DefaultHostConnection)2 HostConnection (com.ctrip.platform.dal.dao.datasource.cluster.HostConnection)2 DataSourceIdentity (com.ctrip.platform.dal.dao.datasource.DataSourceIdentity)1 PooledConnection (org.apache.tomcat.jdbc.pool.PooledConnection)1