Commit 1ca1b8d1 by 张乐 Committed by GitHub

Merge pull request #627 from nobodyiam/datasource

use custom connection validator instead of sending SELECT 1 query
parents 425f0e94 037e0036
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(song_s@ctrip.com)
*/
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,7 +3,8 @@ spring.http.converters.preferred-json-mapper=gson ...@@ -3,7 +3,8 @@ spring.http.converters.preferred-json-mapper=gson
# DataSource # DataSource
spring.datasource.testWhileIdle=true spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=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 spring.datasource.initSQL=set names utf8mb4
# Naming strategy # Naming strategy
......
package com.ctrip.framework.apollo.common; package com.ctrip.framework.apollo.common;
import com.ctrip.framework.apollo.common.utils.DataSourceValidatorTest;
import com.ctrip.framework.apollo.common.utils.InputValidatorTest; import com.ctrip.framework.apollo.common.utils.InputValidatorTest;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
...@@ -8,7 +9,7 @@ import org.junit.runners.Suite.SuiteClasses; ...@@ -8,7 +9,7 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class) @RunWith(Suite.class)
@SuiteClasses({ @SuiteClasses({
InputValidatorTest.class InputValidatorTest.class, DataSourceValidatorTest.class
}) })
public class AllTests { public class AllTests {
......
package com.ctrip.framework.apollo.common.utils;
import org.junit.Before;
import org.junit.Test;
import java.sql.Connection;
import static org.junit.Assert.*;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public class DataSourceValidatorTest {
private DataSourceValidator datasourceValidator;
@Before
public void setUp() throws Exception {
datasourceValidator = new DataSourceValidator();
}
@Test
public void testValidateSuccessfully() throws Exception {
Connection someConnection = mock(Connection.class);
int someValidationAction = 1;
when(someConnection.isValid(anyInt())).thenReturn(true);
assertTrue(datasourceValidator.validate(someConnection, someValidationAction));
}
@Test
public void testValidateFailed() throws Exception {
Connection someConnection = mock(Connection.class);
int someValidationAction = 1;
when(someConnection.isValid(anyInt())).thenReturn(false);
assertFalse(datasourceValidator.validate(someConnection, someValidationAction));
}
@Test
public void testValidateWithException() throws Exception {
Connection someConnection = mock(Connection.class);
int someValidationAction = 1;
when(someConnection.isValid(anyInt())).thenThrow(new RuntimeException("error"));
assertFalse(datasourceValidator.validate(someConnection, someValidationAction));
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment