use of com.mysql.jdbc.MySQLConnection in project dal by ctripcorp.
the class DataSourceValidator method validate.
@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;
}
Aggregations