add dynamic config support

This commit is contained in:
wenshao
2012-08-17 09:53:00 +08:00
parent 464b8e28a6
commit 951ea5168b
11 changed files with 199 additions and 55 deletions
@@ -108,7 +108,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
protected volatile String password;
protected volatile String jdbcUrl;
protected volatile String driverClass;
protected volatile Properties connectionProperties = new Properties();
protected volatile Properties connectProperties = new Properties();
protected volatile PasswordCallback passwordCallback;
protected volatile NameCallback userCallback;
@@ -511,7 +511,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
throw new UnsupportedOperationException();
}
connectionProperties.put(name, value);
connectProperties.put(name, value);
}
public Collection<String> getConnectionInitSqls() {
@@ -882,24 +882,14 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
}
public Properties getConnectProperties() {
return connectionProperties;
return connectProperties;
}
public void setConnectProperties(Properties connectionProperties) {
if (inited) {
throw new UnsupportedOperationException();
}
this.connectionProperties = connectionProperties;
}
public abstract void setConnectProperties(Properties properties);
public void setConnectionProperties(String connectionProperties) {
if (inited) {
throw new UnsupportedOperationException();
}
if (connectionProperties == null) {
this.connectionProperties.clear();
if (connectionProperties == null || connectionProperties.trim().length() == 0) {
setConnectProperties(null);
return;
}
@@ -919,7 +909,8 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
}
}
}
this.connectionProperties = properties;
setConnectProperties(properties);
}
public String getUrl() {
@@ -1554,7 +1545,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
to.password = this.password;
to.jdbcUrl = this.jdbcUrl;
to.driverClass = this.driverClass;
to.connectionProperties = this.connectionProperties;
to.connectProperties = this.connectProperties;
to.passwordCallback = this.passwordCallback;
to.userCallback = this.userCallback;
to.initialSize = this.initialSize;
@@ -285,6 +285,20 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
lock.unlock();
}
}
public void setConnectProperties(Properties properties) {
if (properties == null) {
properties = new Properties();
}
if (inited) {
if (!this.connectProperties.equals(properties)) {
LOG.info("connectProperties changed : " + this.connectProperties + " -> " + properties);
}
}
this.connectProperties = properties;
}
public boolean isInited() {
return this.inited;
@@ -420,8 +434,11 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
throw new SQLException(e.getMessage(), e);
} finally {
inited = true;
lock.unlock();
if (LOG.isInfoEnabled()) {
LOG.info("{dataSource-" + this.getID() + "} inited");
}
}
}
@@ -742,7 +759,9 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
if (pooledConnection.isTraceEnable()) {
ActiveConnectionTraceInfo oldInfo = activeConnections.remove(pooledConnection);
if (oldInfo == null) {
LOG.warn("remove abandonded failed. activeConnections.size " + activeConnections.size());
if (LOG.isWarnEnabled()) {
LOG.warn("remove abandonded failed. activeConnections.size " + activeConnections.size());
}
}
}
@@ -897,7 +916,9 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
lock.unlock();
}
LOG.warn("dataSource " + this.getID() + " closed");
if (LOG.isInfoEnabled()) {
LOG.info("{dataSource-" + this.getID() + "} closed");
}
}
void incrementCreateCount() {
@@ -1328,11 +1349,11 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
}
public String getProperties() {
if (this.connectionProperties == null) {
if (this.connectProperties == null) {
return null;
}
Properties properties = new Properties(this.connectionProperties);
Properties properties = new Properties(this.connectProperties);
if (properties.contains("password")) {
properties.put("password", "******");
}
@@ -42,7 +42,9 @@ public class MSSQLValidConnectionChecker implements ValidConnectionChecker, Seri
stmt.execute(QUERY);
return true;
} catch (SQLException e) {
LOG.warn("warning: connection validation failed for current managed connection.");
if (LOG.isWarnEnabled()) {
LOG.warn("warning: connection validation failed for current managed connection.");
}
return false;
} finally {
JdbcUtils.close(stmt);
@@ -8,6 +8,8 @@ public class JakartaCommonsLoggingImpl implements com.alibaba.druid.support.logg
private Log log;
private int errorCount;
private int warnCount;
private int infoCount;
public JakartaCommonsLoggingImpl(Class<?> clazz){
log = LogFactory.getLog(clazz);
@@ -37,21 +39,29 @@ public class JakartaCommonsLoggingImpl implements com.alibaba.druid.support.logg
public void warn(String s) {
log.warn(s);
warnCount++;
}
@Override
public void warn(String s, Throwable e) {
log.warn(s, e);
warnCount++;
}
@Override
public int getWarnCount() {
return warnCount;
}
public int getErrorCount() {
return errorCount;
}
@Override
public void resetStat() {
errorCount = 0;
warnCount = 0;
infoCount = 0;
}
@Override
@@ -62,5 +72,16 @@ public class JakartaCommonsLoggingImpl implements com.alibaba.druid.support.logg
@Override
public void info(String msg) {
log.info(msg);
infoCount++;
}
@Override
public int getInfoCount() {
return infoCount;
}
@Override
public boolean isWarnEnabled() {
return log.isWarnEnabled();
}
}
@@ -6,8 +6,10 @@ import java.util.logging.Logger;
public class Jdk14LoggingImpl implements Log {
private Logger log;
private int errorCount;
private int errorCount;
private int warnCount;
private int infoCount;
public Jdk14LoggingImpl(Class<?> clazz){
log = Logger.getLogger(clazz.toString());
@@ -37,21 +39,29 @@ public class Jdk14LoggingImpl implements Log {
public void warn(String s) {
log.log(Level.WARNING, s);
warnCount++;
}
@Override
public void warn(String s, Throwable e) {
log.log(Level.WARNING, s, e);
warnCount++;
}
@Override
public int getWarnCount() {
return warnCount;
}
public int getErrorCount() {
return errorCount;
}
@Override
public void resetStat() {
errorCount = 0;
warnCount = 0;
infoCount = 0;
}
@Override
@@ -62,5 +72,16 @@ public class Jdk14LoggingImpl implements Log {
@Override
public void info(String msg) {
log.info(msg);
infoCount++;
}
@Override
public int getInfoCount() {
return infoCount;
}
@Override
public boolean isWarnEnabled() {
return log.isLoggable(Level.WARNING);
}
}
@@ -16,11 +16,17 @@ public interface Log {
void debug(String msg, Throwable e);
boolean isWarnEnabled();
void warn(String msg);
void warn(String msg, Throwable e);
int getErrorCount();
int getWarnCount();
int getInfoCount();
void resetStat();
}
@@ -1,5 +1,6 @@
package com.alibaba.druid.support.logging;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
public class Log4jImpl implements Log {
@@ -7,6 +8,8 @@ public class Log4jImpl implements Log {
private Logger log;
private int errorCount;
private int warnCount;
private int infoCount;
public Log4jImpl(Class<?> clazz){
log = Logger.getLogger(clazz);
@@ -34,10 +37,17 @@ public class Log4jImpl implements Log {
public void warn(String s) {
log.warn(s);
warnCount++;
}
public void warn(String s, Throwable e) {
log.warn(s, e);
warnCount++;
}
@Override
public int getWarnCount() {
return warnCount;
}
public int getErrorCount() {
@@ -47,6 +57,8 @@ public class Log4jImpl implements Log {
@Override
public void resetStat() {
errorCount = 0;
warnCount = 0;
infoCount = 0;
}
@Override
@@ -56,6 +68,18 @@ public class Log4jImpl implements Log {
@Override
public void info(String msg) {
infoCount++;
log.info(msg);
}
@Override
public boolean isWarnEnabled() {
return log.isEnabledFor(Level.WARN);
}
@Override
public int getInfoCount() {
return infoCount;
}
}
@@ -2,7 +2,9 @@ package com.alibaba.druid.support.logging;
public class NoLoggingImpl implements Log {
private int infoCount;
private int errorCount;
private int warnCount;
public NoLoggingImpl(Class<?> clazz){
}
@@ -26,20 +28,28 @@ public class NoLoggingImpl implements Log {
}
public void warn(String s) {
warnCount++;
}
@Override
public void warn(String s, Throwable e) {
warnCount++;
}
public int getErrorCount() {
return errorCount;
}
@Override
public int getWarnCount() {
return warnCount;
}
@Override
public void resetStat() {
errorCount = 0;
warnCount = 0;
infoCount = 0;
}
@Override
@@ -49,6 +59,15 @@ public class NoLoggingImpl implements Log {
@Override
public void info(String s) {
infoCount++;
}
@Override
public boolean isWarnEnabled() {
return false;
}
public int getInfoCount() {
return infoCount;
}
}