mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
兼容DBCP
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@498 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -274,7 +274,10 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
if (isTestOnBorrow()) {
|
||||
boolean validate = testConnectionInternal(poolalbeConnection.getConnection());
|
||||
if (!validate) {
|
||||
LOG.debug("skip not validate connection.");
|
||||
if (LOG.isDebugEnabled()) {
|
||||
LOG.debug("skip not validate connection.");
|
||||
}
|
||||
|
||||
Connection realConnection = poolalbeConnection.getConnection();
|
||||
JdbcUtils.close(realConnection);
|
||||
continue;
|
||||
|
||||
@@ -3,5 +3,5 @@ package com.alibaba.druid.pool;
|
||||
import java.sql.Connection;
|
||||
|
||||
public interface ValidConnectionChecker {
|
||||
boolean isValidConnection(Connection c, String query, int validationQueryTimeout);
|
||||
boolean isValidConnection(Connection c, String query, int validationQueryTimeout) ;
|
||||
}
|
||||
|
||||
@@ -21,6 +21,19 @@ public class MSSQLValidConnectionChecker implements ValidConnectionChecker, Seri
|
||||
private static final Log LOG = LogFactory.getLog(MSSQLValidConnectionChecker.class);
|
||||
|
||||
public boolean isValidConnection(final Connection c, String valiateQuery, int validationQueryTimeout) {
|
||||
try {
|
||||
if (c.isClosed()) {
|
||||
return false;
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
// skip
|
||||
return false;
|
||||
}
|
||||
|
||||
if (valiateQuery == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Statement stmt = null;
|
||||
|
||||
try {
|
||||
|
||||
+21
-8
@@ -36,19 +36,32 @@ public class MySqlValidConnectionChecker implements ValidConnectionChecker, Seri
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isValidConnection(Connection c, String valiateQuery, int validationQueryTimeout) {
|
||||
public boolean isValidConnection(Connection conn, String valiateQuery, int validationQueryTimeout) {
|
||||
try {
|
||||
if (conn.isClosed()) {
|
||||
return false;
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
// skip
|
||||
return false;
|
||||
}
|
||||
|
||||
if (valiateQuery == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (driverHasPingMethod) {
|
||||
if (c instanceof PoolableConnection) {
|
||||
c = ((PoolableConnection) c).getConnection();
|
||||
if (conn instanceof PoolableConnection) {
|
||||
conn = ((PoolableConnection) conn).getConnection();
|
||||
}
|
||||
|
||||
if (c instanceof ConnectionProxy) {
|
||||
c = ((ConnectionProxy) c).getConnectionRaw();
|
||||
if (conn instanceof ConnectionProxy) {
|
||||
conn = ((ConnectionProxy) conn).getConnectionRaw();
|
||||
}
|
||||
|
||||
if (clazz.isAssignableFrom(c.getClass())) {
|
||||
if (clazz.isAssignableFrom(conn.getClass())) {
|
||||
try {
|
||||
ping.invoke(c);
|
||||
ping.invoke(conn);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Unexpected error in ping", e);
|
||||
@@ -60,7 +73,7 @@ public class MySqlValidConnectionChecker implements ValidConnectionChecker, Seri
|
||||
Statement stmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
stmt = c.createStatement();
|
||||
stmt = conn.createStatement();
|
||||
stmt.setQueryTimeout(validationQueryTimeout);
|
||||
rs = stmt.executeQuery(valiateQuery);
|
||||
return true;
|
||||
|
||||
+21
-8
@@ -55,19 +55,32 @@ public class OracleValidConnectionChecker implements ValidConnectionChecker, Ser
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isValidConnection(Connection c, String valiateQuery, int validationQueryTimeout) {
|
||||
public boolean isValidConnection(Connection conn, String valiateQuery, int validationQueryTimeout) {
|
||||
try {
|
||||
if (c instanceof PoolableConnection) {
|
||||
c = ((PoolableConnection) c).getConnection();
|
||||
if (conn.isClosed()) {
|
||||
return false;
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
// skip
|
||||
return false;
|
||||
}
|
||||
|
||||
if (valiateQuery == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
if (conn instanceof PoolableConnection) {
|
||||
conn = ((PoolableConnection) conn).getConnection();
|
||||
}
|
||||
|
||||
if (c instanceof ConnectionProxy) {
|
||||
c = ((ConnectionProxy) c).getConnectionRaw();
|
||||
if (conn instanceof ConnectionProxy) {
|
||||
conn = ((ConnectionProxy) conn).getConnectionRaw();
|
||||
}
|
||||
|
||||
// unwrap
|
||||
if (clazz.isAssignableFrom(c.getClass())) {
|
||||
Integer status = (Integer) ping.invoke(c, params);
|
||||
if (clazz.isAssignableFrom(conn.getClass())) {
|
||||
Integer status = (Integer) ping.invoke(conn, params);
|
||||
|
||||
// Error
|
||||
if (status.intValue() < 0) {
|
||||
@@ -80,7 +93,7 @@ public class OracleValidConnectionChecker implements ValidConnectionChecker, Ser
|
||||
Statement stmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
stmt = c.createStatement();
|
||||
stmt = conn.createStatement();
|
||||
rs = stmt.executeQuery(valiateQuery);
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
|
||||
@@ -47,15 +47,15 @@ public class Case3 extends TestCase {
|
||||
driverClass = "com.alibaba.druid.mock.MockDriver";
|
||||
// connectionProperties = "connectSleep=3;executeSleep=1";
|
||||
|
||||
jdbcUrl = "jdbc:mysql://10.20.153.104:3306/druid2";
|
||||
user = "root";
|
||||
password = "root";
|
||||
driverClass = "com.mysql.jdbc.Driver";
|
||||
// jdbcUrl = "jdbc:mysql://10.20.153.104:3306/druid2";
|
||||
// user = "root";
|
||||
// password = "root";
|
||||
// driverClass = "com.mysql.jdbc.Driver";
|
||||
}
|
||||
|
||||
public void test_perf() throws Exception {
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
druid();
|
||||
// druid();
|
||||
dbcp();
|
||||
// boneCP();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user