Skip to content

Commit

Permalink
use custom connection validator instead of sending SELECT 1 query
Browse files Browse the repository at this point in the history
  • Loading branch information
nobodyiam committed May 28, 2017
1 parent 425f0e9 commit f6816a2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.ctrip.framework.apollo.common.utils;

import org.apache.tomcat.jdbc.pool.Validator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;

/**
* @author Jason Song([email protected])
*/
public class DataSourceValidator implements Validator {
private static final Logger LOGGER = LoggerFactory.getLogger(DataSourceValidator.class);
private static final int DEFAULT_VALIDATE_TIMEOUT_IN_SECONDS = 5;

@Override
public boolean validate(Connection connection, int validateAction) {
boolean isValid = false;
try {
isValid = connection.isValid(DEFAULT_VALIDATE_TIMEOUT_IN_SECONDS);
} catch (Throwable ex) {
LOGGER.warn("Data source validation error", ex);
}

return isValid;
}
}
3 changes: 2 additions & 1 deletion apollo-common/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ spring.http.converters.preferred-json-mapper=gson
# DataSource
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=true
spring.datasource.validationQuery=SELECT 1
spring.datasource.validatorClassName=com.ctrip.framework.apollo.common.utils.DataSourceValidator
spring.datasource.validationInterval=5000
spring.datasource.initSQL=set names utf8mb4

# Naming strategy
Expand Down

0 comments on commit f6816a2

Please sign in to comment.