add dynamic change maxActive support

This commit is contained in:
wenshao
2012-08-17 07:34:33 +08:00
parent 798ad92edb
commit c3014c357d
11 changed files with 332 additions and 122 deletions
@@ -95,81 +95,81 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
*/
public static final long DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS = 1000L * 60L * 30L;
protected boolean defaultAutoCommit = true;
protected Boolean defaultReadOnly;
protected Integer defaultTransactionIsolation;
protected String defaultCatalog = null;
protected volatile boolean defaultAutoCommit = true;
protected volatile Boolean defaultReadOnly;
protected volatile Integer defaultTransactionIsolation;
protected volatile String defaultCatalog = null;
protected String name;
protected String username;
protected String password;
protected String jdbcUrl;
protected String driverClass;
protected Properties connectionProperties = new Properties();
protected volatile String username;
protected volatile String password;
protected volatile String jdbcUrl;
protected volatile String driverClass;
protected volatile Properties connectionProperties = new Properties();
protected PasswordCallback passwordCallback;
protected NameCallback userCallback;
protected volatile PasswordCallback passwordCallback;
protected volatile NameCallback userCallback;
protected ConnectionFactory connectionFactory;
protected int initialSize = DEFAULT_INITIAL_SIZE;
protected int maxActive = DEFAULT_MAX_ACTIVE_SIZE;
protected int minIdle = DEFAULT_MIN_IDLE;
protected int maxIdle = DEFAULT_MAX_IDLE;
protected long maxWait = DEFAULT_MAX_WAIT;
protected volatile int initialSize = DEFAULT_INITIAL_SIZE;
protected volatile int maxActive = DEFAULT_MAX_ACTIVE_SIZE;
protected volatile int minIdle = DEFAULT_MIN_IDLE;
protected volatile int maxIdle = DEFAULT_MAX_IDLE;
protected volatile long maxWait = DEFAULT_MAX_WAIT;
protected String validationQuery = DEFAULT_VALIDATION_QUERY;
protected int validationQueryTimeout = -1;
private boolean testOnBorrow = DEFAULT_TEST_ON_BORROW;
private boolean testOnReturn = DEFAULT_TEST_ON_RETURN;
private boolean testWhileIdle = DEFAULT_WHILE_IDLE;
protected boolean poolPreparedStatements = false;
protected boolean sharePreparedStatements = false;
protected int maxPoolPreparedStatementPerConnectionSize = 10;
protected volatile String validationQuery = DEFAULT_VALIDATION_QUERY;
protected volatile int validationQueryTimeout = -1;
private volatile boolean testOnBorrow = DEFAULT_TEST_ON_BORROW;
private volatile boolean testOnReturn = DEFAULT_TEST_ON_RETURN;
private volatile boolean testWhileIdle = DEFAULT_WHILE_IDLE;
protected volatile boolean poolPreparedStatements = false;
protected volatile boolean sharePreparedStatements = false;
protected volatile int maxPoolPreparedStatementPerConnectionSize = 10;
protected boolean inited = false;
protected volatile boolean inited = false;
protected PrintWriter logWriter = new PrintWriter(
System.out);
protected List<Filter> filters = new CopyOnWriteArrayList<Filter>();
protected ExceptionSorter exceptionSorter = null;
protected volatile ExceptionSorter exceptionSorter = null;
protected Driver driver;
protected int queryTimeout;
protected int transactionQueryTimeout;
protected volatile int queryTimeout;
protected volatile int transactionQueryTimeout;
protected long createErrorCount;
protected long createTimespan;
protected int maxWaitThreadCount = -1;
protected volatile int maxWaitThreadCount = -1;
protected boolean accessToUnderlyingConnectionAllowed = true;
protected volatile boolean accessToUnderlyingConnectionAllowed = true;
protected long timeBetweenEvictionRunsMillis = DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS;
protected volatile long timeBetweenEvictionRunsMillis = DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS;
protected int numTestsPerEvictionRun = DEFAULT_NUM_TESTS_PER_EVICTION_RUN;
protected volatile int numTestsPerEvictionRun = DEFAULT_NUM_TESTS_PER_EVICTION_RUN;
protected long minEvictableIdleTimeMillis = DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS;
protected volatile long minEvictableIdleTimeMillis = DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS;
protected boolean removeAbandoned;
protected volatile boolean removeAbandoned;
protected long removeAbandonedTimeoutMillis = 300 * 1000;
protected volatile long removeAbandonedTimeoutMillis = 300 * 1000;
protected boolean logAbandoned;
protected volatile boolean logAbandoned;
protected int maxOpenPreparedStatements = -1;
protected volatile int maxOpenPreparedStatements = -1;
protected List<String> connectionInitSqls;
protected volatile List<String> connectionInitSqls;
protected String dbType;
protected volatile String dbType;
protected long timeBetweenConnectErrorMillis = DEFAULT_TIME_BETWEEN_CONNECT_ERROR_MILLIS;
protected volatile long timeBetweenConnectErrorMillis = DEFAULT_TIME_BETWEEN_CONNECT_ERROR_MILLIS;
protected ValidConnectionChecker validConnectionChecker = null;
protected volatile ValidConnectionChecker validConnectionChecker = null;
protected final AtomicLong errorCount = new AtomicLong();
protected final AtomicLong dupCloseCount = new AtomicLong();
@@ -777,9 +777,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
return poolPreparedStatements;
}
public void setPoolPreparedStatements(boolean poolPreparedStatements) {
this.poolPreparedStatements = poolPreparedStatements;
}
public abstract void setPoolPreparedStatements(boolean value);
public long getMaxWait() {
return maxWait;
@@ -837,13 +835,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
return maxActive;
}
public void setMaxActive(int maxActive) {
if (inited) {
throw new UnsupportedOperationException();
}
this.maxActive = maxActive;
}
public abstract void setMaxActive(int maxActive);
public String getUsername() {
return username;
@@ -974,6 +966,10 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
connectionFactory = createConnectionFactory();
}
public ConnectionFactory getConnectionFactory() {
return connectionFactory;
}
protected ConnectionFactory createConnectionFactory() throws SQLException {
return new DruidPoolConnectionFactory(this);
}
@@ -1219,6 +1215,14 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
abstract void incrementCreateCount();
public Connection createPhysicalConnection(String url, Properties info) throws SQLException {
if (getProxyFilters().size() == 0) {
return getDriver().connect(url, info);
}
return new FilterChainImpl(this).connection_connect(info);
}
public static class DruidPoolConnectionFactory implements ConnectionFactory {
private final String url;
@@ -1286,7 +1290,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
long startNano = System.nanoTime();
try {
conn = connect();
conn = dataSource.createPhysicalConnection(url, info);
if (conn == null) {
throw new SQLException("connect error, url " + url);
@@ -1321,16 +1325,6 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
return conn;
}
public Connection connect() throws SQLException {
Connection conn;
if (dataSource.getProxyFilters().size() != 0) {
conn = new FilterChainImpl(dataSource).connection_connect(info);
} else {
conn = dataSource.getDriver().connect(url, info);
}
return conn;
}
public void initConnection(Connection conn) throws SQLException {
conn.setAutoCommit(dataSource.isDefaultAutoCommit());
if (dataSource.getDefaultReadOnly() != null) {
@@ -76,54 +76,54 @@ import com.alibaba.druid.util.JdbcUtils;
*/
public class DruidDataSource extends DruidAbstractDataSource implements DruidDataSourceMBean, ManagedDataSource, Referenceable, Closeable, Cloneable, ConnectionPoolDataSource {
private final static Log LOG = LogFactory.getLog(DruidDataSource.class);
private final static Log LOG = LogFactory.getLog(DruidDataSource.class);
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;
private final Condition notEmpty = lock.newCondition();
private final Condition empty = lock.newCondition();
private final Condition notEmpty = lock.newCondition();
private final Condition empty = lock.newCondition();
// stats
private long connectCount = 0L;
private long closeCount = 0L;
private final AtomicLong connectErrorCount = new AtomicLong();
private long recycleCount = 0L;
private long createConnectionCount = 0L;
private long destroyCount = 0L;
private long removeAbandonedCount = 0L;
private long notEmptyWaitCount = 0L;
private long notEmptySignalCount = 0L;
private long notEmptyWaitNanos = 0L;
private long connectCount = 0L;
private long closeCount = 0L;
private final AtomicLong connectErrorCount = new AtomicLong();
private long recycleCount = 0L;
private long createConnectionCount = 0L;
private long destroyCount = 0L;
private long removeAbandonedCount = 0L;
private long notEmptyWaitCount = 0L;
private long notEmptySignalCount = 0L;
private long notEmptyWaitNanos = 0L;
private int activePeak = 0;
private long activePeakTime = 0;
private int poolingPeak = 0;
private long poolingPeakTime = 0;
private int activePeak = 0;
private long activePeakTime = 0;
private int poolingPeak = 0;
private long poolingPeakTime = 0;
// store
private ConnectionHolder[] connections;
private int poolingCount = 0;
private int activeCount = 0;
private long discardCount = 0;
private int notEmptyWaitThreadCount = 0;
private int notEmptyWaitThreadPeak = 0;
private volatile ConnectionHolder[] connections;
private int poolingCount = 0;
private int activeCount = 0;
private long discardCount = 0;
private int notEmptyWaitThreadCount = 0;
private int notEmptyWaitThreadPeak = 0;
// threads
private CreateConnectionThread createConnectionThread;
private DestroyConnectionThread destoryConnectionThread;
private CreateConnectionThread createConnectionThread;
private DestroyConnectionThread destoryConnectionThread;
private final CountDownLatch initedLatch = new CountDownLatch(2);
private final CountDownLatch initedLatch = new CountDownLatch(2);
private boolean enable = true;
private boolean enable = true;
private boolean resetStatEnable = true;
private boolean resetStatEnable = true;
private String initStackTrace;
private String initStackTrace;
private boolean closed = false;
private long closeTimeMillis = -1L;
private boolean closed = false;
private long closeTimeMillis = -1L;
private JdbcDataSourceStat dataSourceStat;
private JdbcDataSourceStat dataSourceStat;
public DruidDataSource(){
}
@@ -216,6 +216,19 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
}
public void setPoolPreparedStatements(boolean value) {
if (this.poolPreparedStatements == value) {
return;
}
if (!inited) {
this.poolPreparedStatements = value;
return;
}
if (LOG.isInfoEnabled()) {
LOG.info("set poolPreparedStatements " + this.poolPreparedStatements + " -> " + value);
}
lock.lock();
try {
if (this.poolPreparedStatements && (!value)) {
@@ -230,7 +243,36 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
connection.getStatementPool().getMap().clear();
}
}
super.setPoolPreparedStatements(value);
} finally {
lock.unlock();
}
}
public void setMaxActive(int maxActive) {
if (this.maxActive == maxActive) {
return;
}
if (!inited) {
this.maxActive = maxActive;
return;
}
if (LOG.isInfoEnabled()) {
LOG.info("set maxActive " + this.maxActive + " -> " + maxActive);
}
lock.lock();
try {
int allCount = this.poolingCount + this.activeCount;
if (maxActive > allCount) {
this.connections = Arrays.copyOf(this.connections, maxActive);
} else {
this.connections = Arrays.copyOf(this.connections, allCount);
}
this.maxActive = maxActive;
} finally {
lock.unlock();
}
@@ -54,4 +54,13 @@ public class JakartaCommonsLoggingImpl implements com.alibaba.druid.support.logg
errorCount = 0;
}
@Override
public boolean isInfoEnabled() {
return log.isInfoEnabled();
}
@Override
public void info(String msg) {
log.info(msg);
}
}
@@ -53,4 +53,14 @@ public class Jdk14LoggingImpl implements Log {
public void resetStat() {
errorCount = 0;
}
@Override
public boolean isInfoEnabled() {
return log.isLoggable(Level.INFO);
}
@Override
public void info(String msg) {
log.info(msg);
}
}
@@ -4,19 +4,23 @@ public interface Log {
boolean isDebugEnabled();
void error(String s, Throwable e);
void error(String msg, Throwable e);
void error(String s);
void error(String msg);
public void debug(String s);
boolean isInfoEnabled();
public void debug(String s, Throwable e);
void info(String msg);
public void warn(String s);
void debug(String msg);
void warn(String s, Throwable e);
void debug(String msg, Throwable e);
void warn(String msg);
void warn(String msg, Throwable e);
int getErrorCount();
void resetStat();
}
@@ -5,8 +5,8 @@ import org.apache.log4j.Logger;
public class Log4jImpl implements Log {
private Logger log;
private int errorCount;
private int errorCount;
public Log4jImpl(Class<?> clazz){
log = Logger.getLogger(clazz);
@@ -39,7 +39,7 @@ public class Log4jImpl implements Log {
public void warn(String s, Throwable e) {
log.warn(s, e);
}
public int getErrorCount() {
return errorCount;
}
@@ -48,4 +48,14 @@ public class Log4jImpl implements Log {
public void resetStat() {
errorCount = 0;
}
@Override
public boolean isInfoEnabled() {
return log.isInfoEnabled();
}
@Override
public void info(String msg) {
log.info(msg);
}
}
@@ -41,4 +41,14 @@ public class NoLoggingImpl implements Log {
public void resetStat() {
errorCount = 0;
}
@Override
public boolean isInfoEnabled() {
return false;
}
@Override
public void info(String s) {
}
}