mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Merge pull request #733 from wenshao/master
change oracle valid connection impl
This commit is contained in:
+21
-37
@@ -15,6 +15,13 @@
|
||||
*/
|
||||
package com.alibaba.druid.pool.vendor;
|
||||
|
||||
import java.io.Serializable;
|
||||
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;
|
||||
import com.alibaba.druid.pool.ValidConnectionCheckerAdapter;
|
||||
@@ -22,41 +29,21 @@ import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
import com.alibaba.druid.util.Utils;
|
||||
|
||||
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 java.util.Properties;
|
||||
|
||||
public class OracleValidConnectionChecker extends ValidConnectionCheckerAdapter implements ValidConnectionChecker, Serializable {
|
||||
|
||||
private static final long serialVersionUID = -2227528634302168877L;
|
||||
private static final long serialVersionUID = -2227528634302168877L;
|
||||
|
||||
private static final Log LOG = LogFactory.getLog(OracleValidConnectionChecker.class);
|
||||
private static final Log LOG = LogFactory.getLog(OracleValidConnectionChecker.class);
|
||||
|
||||
private final Class<?> clazz;
|
||||
private final Method ping;
|
||||
private final static Object[] params = new Object[] { new Integer(5000) };
|
||||
private int timeout = 1;
|
||||
|
||||
private String defaultValidateQuery = "SELECT 'x' FROM DUAL";
|
||||
|
||||
public OracleValidConnectionChecker(){
|
||||
try {
|
||||
clazz = Utils.loadClass("oracle.jdbc.driver.OracleConnection");
|
||||
if (clazz != null) {
|
||||
ping = clazz.getMethod("pingDatabase", new Class[] { Integer.TYPE });
|
||||
} else {
|
||||
ping = null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Unable to resolve pingDatabase method:", e);
|
||||
}
|
||||
|
||||
configFromProperties(System.getProperties());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void configFromProperties(Properties properties) {
|
||||
String property = properties.getProperty("druid.oracle.pingTimeout");
|
||||
@@ -65,12 +52,16 @@ public class OracleValidConnectionChecker extends ValidConnectionCheckerAdapter
|
||||
setTimeout(value);
|
||||
}
|
||||
}
|
||||
|
||||
public void setTimeout(int timeout) {
|
||||
params[0] = timeout;
|
||||
|
||||
public void setTimeout(int seconds) {
|
||||
this.timeout = seconds;
|
||||
}
|
||||
|
||||
public boolean isValidConnection(Connection conn, String validateQuery, int validationQueryTimeout) {
|
||||
if (validateQuery == null || validateQuery.isEmpty()) {
|
||||
validateQuery = this.defaultValidateQuery;
|
||||
}
|
||||
|
||||
try {
|
||||
if (conn.isClosed()) {
|
||||
return false;
|
||||
@@ -89,18 +80,11 @@ public class OracleValidConnectionChecker extends ValidConnectionCheckerAdapter
|
||||
conn = ((ConnectionProxy) conn).getRawObject();
|
||||
}
|
||||
|
||||
// unwrap
|
||||
if (clazz != null && clazz.isAssignableFrom(conn.getClass())) {
|
||||
Integer status = (Integer) ping.invoke(conn, params);
|
||||
|
||||
// Error
|
||||
return status >= 0;
|
||||
}
|
||||
|
||||
Statement stmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
stmt = conn.createStatement();
|
||||
stmt.setQueryTimeout(timeout);
|
||||
rs = stmt.executeQuery(validateQuery);
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
|
||||
Reference in New Issue
Block a user