mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
fixed testcase
This commit is contained in:
+26
-5
@@ -22,6 +22,7 @@ import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Properties;
|
||||
|
||||
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||
import com.alibaba.druid.pool.ValidConnectionChecker;
|
||||
@@ -33,23 +34,43 @@ import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class MySqlValidConnectionChecker extends ValidConnectionCheckerAdapter implements ValidConnectionChecker, Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final Log LOG = LogFactory.getLog(MySqlValidConnectionChecker.class);
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final Log LOG = LogFactory.getLog(MySqlValidConnectionChecker.class);
|
||||
|
||||
private Class<?> clazz;
|
||||
private Method ping;
|
||||
private boolean driverHasPingMethod = false;
|
||||
private boolean usePingMethod = false;
|
||||
|
||||
public MySqlValidConnectionChecker(){
|
||||
try {
|
||||
clazz = JdbcUtils.loadDriverClass("com.mysql.jdbc.Connection");
|
||||
ping = clazz.getMethod("ping");
|
||||
if (ping != null) {
|
||||
driverHasPingMethod = true;
|
||||
usePingMethod = true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Cannot resolve com.mysq.jdbc.Connection.ping method. Will use 'SELECT 1' instead.", e);
|
||||
}
|
||||
|
||||
configFromProperties(System.getProperties());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configFromProperties(Properties properties) {
|
||||
String property = properties.getProperty("druid.mysql.usePingMethod");
|
||||
if ("true".equals(property)) {
|
||||
setUsePingMethod(true);
|
||||
} else if ("false".equals(property)) {
|
||||
setUsePingMethod(false);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isUsePingMethod() {
|
||||
return usePingMethod;
|
||||
}
|
||||
|
||||
public void setUsePingMethod(boolean usePingMethod) {
|
||||
this.usePingMethod = usePingMethod;
|
||||
}
|
||||
|
||||
public boolean isValidConnection(Connection conn, String valiateQuery, int validationQueryTimeout) {
|
||||
@@ -62,7 +83,7 @@ public class MySqlValidConnectionChecker extends ValidConnectionCheckerAdapter i
|
||||
return false;
|
||||
}
|
||||
|
||||
if (driverHasPingMethod) {
|
||||
if (usePingMethod) {
|
||||
if (conn instanceof DruidPooledConnection) {
|
||||
conn = ((DruidPooledConnection) conn).getConnection();
|
||||
}
|
||||
|
||||
@@ -89,6 +89,14 @@ public enum SQLBinaryOperator {
|
||||
this.priority = priority;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public int getPriority() {
|
||||
return this.priority;
|
||||
}
|
||||
|
||||
public boolean isRelational() {
|
||||
switch (this) {
|
||||
case Equality:
|
||||
|
||||
@@ -587,6 +587,7 @@ public class WallFilter extends FilterAdapter implements WallFilterMBean {
|
||||
}
|
||||
}
|
||||
|
||||
//TODO
|
||||
return sql;
|
||||
}
|
||||
|
||||
|
||||
@@ -585,11 +585,12 @@ public abstract class WallProvider {
|
||||
}
|
||||
|
||||
public static <T> T doPrivileged(PrivilegedAction<T> action) {
|
||||
final Boolean original = privileged.get();
|
||||
privileged.set(Boolean.TRUE);
|
||||
try {
|
||||
return action.run();
|
||||
} finally {
|
||||
privileged.set(null);
|
||||
privileged.set(original);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import junit.framework.TestCase;
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.ValidConnectionChecker;
|
||||
import com.alibaba.druid.pool.ValidConnectionCheckerAdapter;
|
||||
|
||||
public class DruidDataSourceTest5 extends TestCase {
|
||||
|
||||
@@ -25,7 +25,7 @@ public class DruidDataSourceTest5 extends TestCase {
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setInitialSize(1);
|
||||
|
||||
dataSource.setValidConnectionChecker(new ValidConnectionChecker() {
|
||||
dataSource.setValidConnectionChecker(new ValidConnectionCheckerAdapter() {
|
||||
|
||||
@Override
|
||||
public boolean isValidConnection(Connection c, String query, int validationQueryTimeout) {
|
||||
|
||||
+2
-2
@@ -10,7 +10,7 @@ import junit.framework.TestCase;
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.ValidConnectionChecker;
|
||||
import com.alibaba.druid.pool.ValidConnectionCheckerAdapter;
|
||||
|
||||
/**
|
||||
* 这个场景测试defaultAutoCommit
|
||||
@@ -28,7 +28,7 @@ public class DruidDataSourceTest_testOnBorrowFailed extends TestCase {
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setTestOnBorrow(true);
|
||||
dataSource.setValidationQuery("select 'x'");
|
||||
dataSource.setValidConnectionChecker(new ValidConnectionChecker() {
|
||||
dataSource.setValidConnectionChecker(new ValidConnectionCheckerAdapter() {
|
||||
|
||||
@Override
|
||||
public boolean isValidConnection(Connection c, String query, int validationQueryTimeout) {
|
||||
|
||||
+2
-2
@@ -10,7 +10,7 @@ import junit.framework.TestCase;
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.ValidConnectionChecker;
|
||||
import com.alibaba.druid.pool.ValidConnectionCheckerAdapter;
|
||||
|
||||
/**
|
||||
* 这个场景测试defaultAutoCommit
|
||||
@@ -30,7 +30,7 @@ public class DruidDataSourceTest_testOnWhileIdleFailed extends TestCase {
|
||||
dataSource.setTestWhileIdle(true);
|
||||
dataSource.setTimeBetweenEvictionRunsMillis(20);
|
||||
dataSource.setValidationQuery("select 'x'");
|
||||
dataSource.setValidConnectionChecker(new ValidConnectionChecker() {
|
||||
dataSource.setValidConnectionChecker(new ValidConnectionCheckerAdapter() {
|
||||
|
||||
@Override
|
||||
public boolean isValidConnection(Connection c, String query, int validationQueryTimeout) {
|
||||
|
||||
Reference in New Issue
Block a user