mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improve dynamic config support for ValidConnectionChecker
This commit is contained in:
@@ -17,9 +17,17 @@ package com.alibaba.druid.pool;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
* @since 0.2.19
|
||||
*/
|
||||
public interface DruidDataSourceStatLogger {
|
||||
|
||||
void log(DruidDataSourceStatValue statValue);
|
||||
|
||||
|
||||
/**
|
||||
* @param properties
|
||||
* @since 0.2.21
|
||||
*/
|
||||
void configFromProperties(Properties properties);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,11 @@ package com.alibaba.druid.pool;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
* @since 0.2.21
|
||||
*/
|
||||
public abstract class DruidDataSourceStatLoggerAdapter implements DruidDataSourceStatLogger {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -27,17 +27,24 @@ import com.alibaba.druid.support.json.JSONUtils;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
|
||||
/**
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
* @since 0.2.19
|
||||
*/
|
||||
public class DruidDataSourceStatLoggerImpl extends DruidDataSourceStatLoggerAdapter {
|
||||
|
||||
private static Log LOG = LogFactory.getLog(DruidDataSourceStatLoggerImpl.class);
|
||||
|
||||
public DruidDataSourceStatLoggerImpl() {
|
||||
|
||||
public DruidDataSourceStatLoggerImpl(){
|
||||
this.configFromProperties(System.getProperties());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @since 0.2.21
|
||||
*/
|
||||
@Override
|
||||
public void configFromProperties(Properties properties) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -65,72 +72,72 @@ public class DruidDataSourceStatLoggerImpl extends DruidDataSourceStatLoggerAdap
|
||||
if (statValue.getWaitThreadCount() > 0) {
|
||||
map.put("waitThreadCount", statValue.getWaitThreadCount());
|
||||
}
|
||||
|
||||
|
||||
if (statValue.getNotEmptyWaitCount() > 0) {
|
||||
map.put("notEmptyWaitCount", statValue.getNotEmptyWaitCount());
|
||||
}
|
||||
|
||||
|
||||
if (statValue.getNotEmptyWaitMillis() > 0) {
|
||||
map.put("notEmptyWaitMillis", statValue.getNotEmptyWaitMillis());
|
||||
}
|
||||
|
||||
|
||||
if (statValue.getLogicConnectErrorCount() > 0) {
|
||||
map.put("logicConnectErrorCount", statValue.getLogicConnectErrorCount());
|
||||
}
|
||||
|
||||
|
||||
if (statValue.getPhysicalConnectCount() > 0) {
|
||||
map.put("physicalConnectCount", statValue.getPhysicalConnectCount());
|
||||
}
|
||||
|
||||
|
||||
if (statValue.getPhysicalCloseCount() > 0) {
|
||||
map.put("physicalCloseCount", statValue.getPhysicalCloseCount());
|
||||
}
|
||||
|
||||
|
||||
if (statValue.getPhysicalConnectErrorCount() > 0) {
|
||||
map.put("physicalConnectErrorCount", statValue.getPhysicalConnectErrorCount());
|
||||
}
|
||||
|
||||
|
||||
if (statValue.getExecuteCount() > 0) {
|
||||
map.put("executeCount", statValue.getExecuteCount());
|
||||
}
|
||||
|
||||
|
||||
if (statValue.getErrorCount() > 0) {
|
||||
map.put("errorCount", statValue.getErrorCount());
|
||||
}
|
||||
|
||||
|
||||
if (statValue.getCommitCount() > 0) {
|
||||
map.put("commitCount", statValue.getCommitCount());
|
||||
}
|
||||
|
||||
|
||||
if (statValue.getRollbackCount() > 0) {
|
||||
map.put("rollbackCount", statValue.getRollbackCount());
|
||||
}
|
||||
|
||||
|
||||
if (statValue.getPstmtCacheHitCount() > 0) {
|
||||
map.put("pstmtCacheHitCount", statValue.getPstmtCacheHitCount());
|
||||
}
|
||||
|
||||
|
||||
if (statValue.getPstmtCacheMissCount() > 0) {
|
||||
map.put("pstmtCacheMissCount", statValue.getPstmtCacheMissCount());
|
||||
}
|
||||
|
||||
|
||||
if (statValue.getStartTransactionCount() > 0) {
|
||||
map.put("startTransactionCount", statValue.getStartTransactionCount());
|
||||
map.put("transactionHistogram", rtrim(statValue.getTransactionHistogram()));
|
||||
}
|
||||
|
||||
|
||||
if (statValue.getConnectCount() > 0) {
|
||||
map.put("connectionHoldTimeHistogram", rtrim(statValue.getConnectionHoldTimeHistogram()));
|
||||
}
|
||||
|
||||
|
||||
if (statValue.getClobOpenCount() > 0) {
|
||||
map.put("clobOpenCount", statValue.getClobOpenCount());
|
||||
}
|
||||
|
||||
|
||||
if (statValue.getBlobOpenCount() > 0) {
|
||||
map.put("blobOpenCount", statValue.getBlobOpenCount());
|
||||
}
|
||||
|
||||
|
||||
if (statValue.getSqlSkipCount() > 0) {
|
||||
map.put("sqlSkipCount", statValue.getSqlSkipCount());
|
||||
}
|
||||
|
||||
@@ -16,8 +16,15 @@
|
||||
package com.alibaba.druid.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.Properties;
|
||||
|
||||
public interface ValidConnectionChecker {
|
||||
|
||||
boolean isValidConnection(Connection c, String query, int validationQueryTimeout);
|
||||
|
||||
/**
|
||||
* @param properties
|
||||
* @since 0.2.21
|
||||
*/
|
||||
void configFromProperties(Properties properties);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.alibaba.druid.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author wenshao<szujobs@hotmail.com>
|
||||
* @since 0.2.21
|
||||
*/
|
||||
public class ValidConnectionCheckerAdapter implements ValidConnectionChecker {
|
||||
|
||||
@Override
|
||||
public boolean isValidConnection(Connection c, String query, int validationQueryTimeout) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configFromProperties(Properties properties) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+2
-1
@@ -21,6 +21,7 @@ import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
import com.alibaba.druid.pool.ValidConnectionChecker;
|
||||
import com.alibaba.druid.pool.ValidConnectionCheckerAdapter;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
@@ -28,7 +29,7 @@ import com.alibaba.druid.util.JdbcUtils;
|
||||
/**
|
||||
* A MSSQLValidConnectionChecker.
|
||||
*/
|
||||
public class MSSQLValidConnectionChecker implements ValidConnectionChecker, Serializable {
|
||||
public class MSSQLValidConnectionChecker extends ValidConnectionCheckerAdapter implements ValidConnectionChecker, Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
||||
+3
-2
@@ -25,12 +25,13 @@ import java.sql.Statement;
|
||||
|
||||
import com.alibaba.druid.pool.DruidPooledConnection;
|
||||
import com.alibaba.druid.pool.ValidConnectionChecker;
|
||||
import com.alibaba.druid.pool.ValidConnectionCheckerAdapter;
|
||||
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;
|
||||
|
||||
public class MySqlValidConnectionChecker implements ValidConnectionChecker, Serializable {
|
||||
public class MySqlValidConnectionChecker extends ValidConnectionCheckerAdapter implements ValidConnectionChecker, Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final Log LOG = LogFactory.getLog(MySqlValidConnectionChecker.class);
|
||||
@@ -79,7 +80,7 @@ public class MySqlValidConnectionChecker implements ValidConnectionChecker, Seri
|
||||
if (cause instanceof SQLException) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
LOG.warn("Unexpected error in ping", e);
|
||||
return false;
|
||||
} catch (Exception e) {
|
||||
|
||||
+20
-3
@@ -21,15 +21,17 @@ 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;
|
||||
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;
|
||||
|
||||
public class OracleValidConnectionChecker implements ValidConnectionChecker, Serializable {
|
||||
public class OracleValidConnectionChecker extends ValidConnectionCheckerAdapter implements ValidConnectionChecker, Serializable {
|
||||
|
||||
private static final long serialVersionUID = -2227528634302168877L;
|
||||
|
||||
@@ -43,13 +45,28 @@ public class OracleValidConnectionChecker implements ValidConnectionChecker, Ser
|
||||
try {
|
||||
clazz = JdbcUtils.loadDriverClass("oracle.jdbc.driver.OracleConnection");
|
||||
if (clazz != null) {
|
||||
ping = clazz.getMethod("pingDatabase", new Class[] { Integer.TYPE });
|
||||
ping = clazz.getMethod("pingDatabase", new Class[] { Integer.TYPE });
|
||||
} else {
|
||||
ping = null;
|
||||
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");
|
||||
if (property != null && property.length() > 0) {
|
||||
int value = Integer.parseInt(property);
|
||||
setTimeout(value);
|
||||
}
|
||||
}
|
||||
|
||||
public void setTimeout(int timeout) {
|
||||
params[0] = timeout;
|
||||
}
|
||||
|
||||
public boolean isValidConnection(Connection conn, String valiateQuery, int validationQueryTimeout) {
|
||||
|
||||
Reference in New Issue
Block a user