mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
修改OracleValidateConnectionCheck的实现
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@493 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -701,6 +701,13 @@ public abstract class DruidAbstractDataSource implements DruidAbstractDataSource
|
||||
throw new SQLException("validateConnection: connection closed");
|
||||
}
|
||||
|
||||
if (validConnectionChecker != null) {
|
||||
if (!validConnectionChecker.isValidConnection(conn, validationQuery, validationQueryTimeout)) {
|
||||
throw new SQLException("validateConnection false");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (null != query) {
|
||||
Statement stmt = null;
|
||||
ResultSet rset = null;
|
||||
@@ -731,23 +738,23 @@ public abstract class DruidAbstractDataSource implements DruidAbstractDataSource
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean testConnection(Connection conn) {
|
||||
if (conn instanceof PoolableConnection) {
|
||||
conn = ((PoolableConnection) conn).getConnection();
|
||||
}
|
||||
|
||||
|
||||
if (conn instanceof ConnectionProxy) {
|
||||
conn = ((ConnectionProxy) conn).getConnectionRaw();
|
||||
}
|
||||
|
||||
|
||||
return testConnectionInternal(conn);
|
||||
}
|
||||
|
||||
protected boolean testConnectionInternal(Connection conn) {
|
||||
try {
|
||||
if (validConnectionChecker != null) {
|
||||
return validConnectionChecker.isValidConnection(conn);
|
||||
return validConnectionChecker.isValidConnection(conn, validationQuery, validationQueryTimeout);
|
||||
}
|
||||
|
||||
String query = getValidationQuery();
|
||||
|
||||
@@ -3,5 +3,5 @@ package com.alibaba.druid.pool;
|
||||
import java.sql.Connection;
|
||||
|
||||
public interface ValidConnectionChecker {
|
||||
boolean isValidConnection(Connection c);
|
||||
boolean isValidConnection(Connection c, String query, int validationQueryTimeout);
|
||||
}
|
||||
|
||||
+2
-1
@@ -20,11 +20,12 @@ public class MSSQLValidConnectionChecker implements ValidConnectionChecker, Seri
|
||||
private static final String QUERY = "SELECT 1";
|
||||
private static final Log LOG = LogFactory.getLog(MSSQLValidConnectionChecker.class);
|
||||
|
||||
public boolean isValidConnection(final Connection c) {
|
||||
public boolean isValidConnection(final Connection c, String valiateQuery, int validationQueryTimeout) {
|
||||
Statement stmt = null;
|
||||
|
||||
try {
|
||||
stmt = c.createStatement();
|
||||
stmt.setQueryTimeout(validationQueryTimeout);
|
||||
stmt.execute(QUERY);
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
|
||||
+3
-2
@@ -34,7 +34,7 @@ public class MySqlValidConnectionChecker implements ValidConnectionChecker, Seri
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isValidConnection(Connection c) {
|
||||
public boolean isValidConnection(Connection c, String valiateQuery, int validationQueryTimeout) {
|
||||
Connection conn = null;
|
||||
|
||||
if (clazz.isAssignableFrom(c.getClass())) {
|
||||
@@ -64,7 +64,8 @@ public class MySqlValidConnectionChecker implements ValidConnectionChecker, Seri
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
stmt = c.createStatement();
|
||||
rs = stmt.executeQuery("SELECT 1");
|
||||
stmt.setQueryTimeout(validationQueryTimeout);
|
||||
rs = stmt.executeQuery(valiateQuery);
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
return false;
|
||||
|
||||
+38
-11
@@ -24,11 +24,17 @@ package com.alibaba.druid.pool.vendor;
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Method;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
import com.alibaba.druid.logging.Log;
|
||||
import com.alibaba.druid.logging.LogFactory;
|
||||
import com.alibaba.druid.pool.PoolableConnection;
|
||||
import com.alibaba.druid.pool.ValidConnectionChecker;
|
||||
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
|
||||
import com.alibaba.druid.util.DruidLoaderUtils;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class OracleValidConnectionChecker implements ValidConnectionChecker, Serializable {
|
||||
|
||||
@@ -49,21 +55,42 @@ public class OracleValidConnectionChecker implements ValidConnectionChecker, Ser
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isValidConnection(Connection c) {
|
||||
public boolean isValidConnection(Connection c, String valiateQuery, int validationQueryTimeout) {
|
||||
try {
|
||||
if (c instanceof PoolableConnection) {
|
||||
c = ((PoolableConnection) c).getConnection();
|
||||
}
|
||||
|
||||
if (c instanceof ConnectionProxy) {
|
||||
c = ((ConnectionProxy) c).getConnectionRaw();
|
||||
}
|
||||
|
||||
// unwrap
|
||||
if (!clazz.isAssignableFrom(c.getClass())) {
|
||||
Connection conn = (Connection) c.unwrap(clazz);
|
||||
if (conn != null) {
|
||||
c = conn;
|
||||
if (clazz.isAssignableFrom(c.getClass())) {
|
||||
Integer status = (Integer) ping.invoke(c, params);
|
||||
|
||||
Integer status = (Integer) ping.invoke(c, params);
|
||||
|
||||
// Error
|
||||
if (status.intValue() < 0) {
|
||||
return false;
|
||||
}
|
||||
// Error
|
||||
if (status.intValue() < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Statement stmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
stmt = c.createStatement();
|
||||
rs = stmt.executeQuery(valiateQuery);
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
return false;
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Unexpected error in ping", e);
|
||||
return false;
|
||||
} finally {
|
||||
JdbcUtils.close(rs);
|
||||
JdbcUtils.close(stmt);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Unexpected error in pingDatabase", e);
|
||||
|
||||
@@ -40,6 +40,8 @@ public class CaseKylin_Oracle extends TestCase {
|
||||
private long minEvictableIdleTimeMillis = 1800000;
|
||||
private int numTestsPerEvictionRun = 20;
|
||||
private String validateQuery = "SELECT @@SQL_MODE";
|
||||
|
||||
private String oracleDriverClass = "com.alibaba.china.jdbc.SimpleDriver";
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
// jdbcUrl = "jdbc:fake:dragoon_v25masterdb";
|
||||
@@ -105,7 +107,7 @@ public class CaseKylin_Oracle extends TestCase {
|
||||
dataSource.setMinIdle(minIdle);
|
||||
dataSource.setMaxWait(maxWait);
|
||||
dataSource.setPoolPreparedStatements(true);
|
||||
dataSource.setDriverClassName(driverClass);
|
||||
dataSource.setDriverClassName(oracleDriverClass);
|
||||
dataSource.setUrl(jdbcUrl);
|
||||
dataSource.setPoolPreparedStatements(true);
|
||||
dataSource.setUsername(user);
|
||||
|
||||
Reference in New Issue
Block a user