Java Code Examples for org.apache.tomcat.jdbc.pool.PoolProperties#getInitSQL()

The following examples show how to use org.apache.tomcat.jdbc.pool.PoolProperties#getInitSQL() . 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: DataSourceValidator.java    From dal with Apache License 2.0 6 votes vote down vote up
private QueryParameter getQueryParameter(int validateAction) {
    QueryParameter parameter = null;
    if (validateAction != PooledConnection.VALIDATE_INIT)
        return parameter;

    PoolProperties poolProperties = getPoolProperties();
    if (poolProperties == null)
        return parameter;

    String query = poolProperties.getInitSQL();
    int validationQueryTimeout = poolProperties.getValidationQueryTimeout();
    if (validationQueryTimeout <= 0) {
        validationQueryTimeout = DEFAULT_VALIDATE_TIMEOUT_IN_SECONDS;
    }

    parameter = new QueryParameter();
    parameter.setQuery(query);
    parameter.setValidationQueryTimeout(validationQueryTimeout);
    return parameter;
}
 
Example 2
Source File: DataSourceValidator.java    From das with Apache License 2.0 4 votes vote down vote up
@Override
public boolean validate(Connection connection, int validateAction) {
    boolean isValid = false;
    try {
        String query = null;
        int validationQueryTimeout = -1;

        if (validateAction == PooledConnection.VALIDATE_INIT) {
            PoolProperties poolProperties = getPoolProperties(connection);
            if (poolProperties != null) {
                query = poolProperties.getInitSQL();
                validationQueryTimeout = poolProperties.getValidationQueryTimeout();
                if (validationQueryTimeout <= 0) {
                    validationQueryTimeout = DEFAULT_VALIDATE_TIMEOUT_IN_SECONDS;
                }
            }
        }

        if (query == null) {
            if (connection instanceof MySQLConnection) {
                MySQLConnection mySqlConnection = (MySQLConnection) connection;
                isValid = MySqlConnectionHelper.isValid(mySqlConnection, DEFAULT_VALIDATE_TIMEOUT_IN_SECONDS);
            } else {
                isValid = connection.isValid(DEFAULT_VALIDATE_TIMEOUT_IN_SECONDS);
            }

            if (!isValid) {
                LOGGER.warn("isValid() returned false.");
            }
        } else {
            Statement stmt = null;
            try {
                stmt = connection.createStatement();
                stmt.setQueryTimeout(validationQueryTimeout);
                stmt.execute(query);
                isValid = true;
            } finally {
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (Exception ignore2) {
                        /* NOOP */}
                }
            }
        }
    } catch (Throwable ex) {
        LOGGER.warn("Datasource validation error", ex);
    }

    return isValid;
}