mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Merge pull request #4677 from RichardMa1989/add_config_usePingMethod
添加usePingMethod配置
This commit is contained in:
@@ -170,6 +170,8 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
protected volatile long timeBetweenConnectErrorMillis = DEFAULT_TIME_BETWEEN_CONNECT_ERROR_MILLIS;
|
||||
|
||||
protected volatile ValidConnectionChecker validConnectionChecker = null;
|
||||
|
||||
protected volatile boolean usePingMethod = false;
|
||||
|
||||
protected final Map<DruidPooledConnection, Object> activeConnections = new IdentityHashMap<DruidPooledConnection, Object>();
|
||||
protected final static Object PRESENT = new Object();
|
||||
@@ -641,6 +643,14 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
this.validConnectionChecker = validConnectionChecker;
|
||||
}
|
||||
|
||||
public boolean isUsePingMethod() {
|
||||
return usePingMethod;
|
||||
}
|
||||
|
||||
public void setUsePingMethod(boolean usePingMethod) {
|
||||
this.usePingMethod = usePingMethod;
|
||||
}
|
||||
|
||||
public String getValidConnectionCheckerClassName() {
|
||||
if (validConnectionChecker == null) {
|
||||
return null;
|
||||
|
||||
@@ -1318,7 +1318,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
|
||||
String realDriverClassName = driver.getClass().getName();
|
||||
if (JdbcUtils.isMySqlDriver(realDriverClassName)) {
|
||||
this.validConnectionChecker = new MySqlValidConnectionChecker();
|
||||
this.validConnectionChecker = new MySqlValidConnectionChecker(usePingMethod);
|
||||
|
||||
} else if (realDriverClassName.equals(JdbcConstants.ORACLE_DRIVER)
|
||||
|| realDriverClassName.equals(JdbcConstants.ORACLE_DRIVER2)) {
|
||||
|
||||
+3
-3
@@ -45,7 +45,7 @@ public class MySqlValidConnectionChecker extends ValidConnectionCheckerAdapter i
|
||||
private Method ping;
|
||||
private boolean usePingMethod = false;
|
||||
|
||||
public MySqlValidConnectionChecker(){
|
||||
public MySqlValidConnectionChecker(boolean usePingMethod){
|
||||
try {
|
||||
clazz = Utils.loadClass("com.mysql.jdbc.MySQLConnection");
|
||||
if (clazz == null) {
|
||||
@@ -56,8 +56,8 @@ public class MySqlValidConnectionChecker extends ValidConnectionCheckerAdapter i
|
||||
ping = clazz.getMethod("pingInternal", boolean.class, int.class);
|
||||
}
|
||||
|
||||
if (ping != null) {
|
||||
usePingMethod = true;
|
||||
if (ping != null && usePingMethod == true) {
|
||||
this.usePingMethod = true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Cannot resolve com.mysql.jdbc.Connection.ping method. Will use 'SELECT 1' instead.", e);
|
||||
|
||||
@@ -239,7 +239,7 @@ public class DataSourceTest3 extends PoolTestCase {
|
||||
|
||||
public void test_ValidConnectionChecker() throws Exception {
|
||||
dataSource.getValidConnectionCheckerClassName();
|
||||
dataSource.setValidConnectionChecker(new MySqlValidConnectionChecker());
|
||||
dataSource.setValidConnectionChecker(new MySqlValidConnectionChecker(false));
|
||||
Assert.assertEquals(MySqlValidConnectionChecker.class.getName(),
|
||||
dataSource.getValidConnectionCheckerClassName());
|
||||
}
|
||||
@@ -326,7 +326,7 @@ public class DataSourceTest3 extends PoolTestCase {
|
||||
|
||||
public void test_error_validateConnection_3() throws Exception {
|
||||
dataSource.setValidationQuery(null);
|
||||
dataSource.setValidConnectionChecker(new MySqlValidConnectionChecker());
|
||||
dataSource.setValidConnectionChecker(new MySqlValidConnectionChecker(false));
|
||||
|
||||
DruidPooledConnection conn = dataSource.getConnection().unwrap(DruidPooledConnection.class);
|
||||
dataSource.validateConnection(conn);
|
||||
|
||||
+1
-1
@@ -21,7 +21,7 @@ public class MySQLValidConnectionCheckerTest extends PoolTestCase {
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setDbType("mysql");
|
||||
dataSource.setValidationQuery("select 1");
|
||||
dataSource.setValidConnectionChecker(new MySqlValidConnectionChecker());
|
||||
dataSource.setValidConnectionChecker(new MySqlValidConnectionChecker(false));
|
||||
dataSource.setInitialSize(1);
|
||||
dataSource.setTestOnBorrow(true);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user