mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
ha
This commit is contained in:
@@ -15,6 +15,7 @@ import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.locks.Condition;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
@@ -59,6 +60,7 @@ public abstract class MultiDataSource extends DataSourceAdapter implements Multi
|
||||
|
||||
private boolean inited = false;
|
||||
protected final Lock lock = new ReentrantLock();
|
||||
protected final Condition notFull = lock.newCondition();
|
||||
|
||||
private ConcurrentMap<String, DataSourceHolder> dataSources = new ConcurrentHashMap<String, DataSourceHolder>();
|
||||
|
||||
@@ -76,6 +78,22 @@ public abstract class MultiDataSource extends DataSourceAdapter implements Multi
|
||||
|
||||
private Random random;
|
||||
|
||||
private int maxPoolSize;
|
||||
|
||||
private long activeCount = 0;
|
||||
|
||||
public int getMaxPoolSize() {
|
||||
return maxPoolSize;
|
||||
}
|
||||
|
||||
public void setMaxPoolSize(int maxPoolSize) throws SQLException {
|
||||
if (this.isIntited()) {
|
||||
throw new SQLException("dataSource inited");
|
||||
}
|
||||
|
||||
this.maxPoolSize = maxPoolSize;
|
||||
}
|
||||
|
||||
public ConfigLoader getConfigLoader() {
|
||||
return configLoader;
|
||||
}
|
||||
@@ -274,6 +292,10 @@ public abstract class MultiDataSource extends DataSourceAdapter implements Multi
|
||||
return properties;
|
||||
}
|
||||
|
||||
public long getActiveCount() {
|
||||
return activeCount;
|
||||
}
|
||||
|
||||
public void computeTotalWeight() {
|
||||
int totalWeight = 0;
|
||||
for (DataSourceHolder holder : this.dataSources.values()) {
|
||||
@@ -292,7 +314,32 @@ public abstract class MultiDataSource extends DataSourceAdapter implements Multi
|
||||
public Connection getConnection() throws SQLException {
|
||||
init();
|
||||
|
||||
return new MultiDataSourceConnection(this, createConnectionId());
|
||||
lock.lock();
|
||||
try {
|
||||
if (activeCount >= maxPoolSize) {
|
||||
notFull.await();
|
||||
}
|
||||
|
||||
MultiDataSourceConnection conn = new MultiDataSourceConnection(this, createConnectionId());
|
||||
|
||||
activeCount++;
|
||||
|
||||
return conn;
|
||||
} catch (InterruptedException e) {
|
||||
throw new SQLException("thread interrupted", e);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
protected void afterConnectionClosed(MultiDataSourceConnection conn) {
|
||||
lock.lock();
|
||||
try {
|
||||
activeCount--;
|
||||
notFull.signal();
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public MultiConnectionHolder getConnectionInternal(MultiDataSourceConnection multiConn, String sql)
|
||||
|
||||
@@ -49,11 +49,13 @@ public class MultiDataSourceConnection extends WrapperAdapter implements Connect
|
||||
|
||||
private Date connectedTime = null;
|
||||
|
||||
private boolean closed = false;
|
||||
|
||||
public MultiDataSourceConnection(MultiDataSource haDataSource, long id){
|
||||
this.haDataSource = haDataSource;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
|
||||
public DataSourceHolder getDataSourceHolder() {
|
||||
return dataSourceHolder;
|
||||
}
|
||||
@@ -148,15 +150,12 @@ public class MultiDataSourceConnection extends WrapperAdapter implements Connect
|
||||
if (conn != null) {
|
||||
conn.close();
|
||||
}
|
||||
this.closed = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isClosed() throws SQLException {
|
||||
if (conn != null) {
|
||||
return conn.isClosed();
|
||||
}
|
||||
|
||||
return false;
|
||||
return closed;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -27,27 +27,24 @@ public class CobarDataSource extends MultiDataSource {
|
||||
|
||||
private String password;
|
||||
|
||||
private long minEvictableIdleTimeMillis = 1000 * 60 * 3; // 3
|
||||
// minutes
|
||||
|
||||
private List<Filter> proxyFilters = new ArrayList<Filter>();
|
||||
private String filters;
|
||||
|
||||
private int maxPoolSize;
|
||||
|
||||
public CobarDataSource(){
|
||||
this.setFailureDetector(new CobarFailureDetecter());
|
||||
this.setFailureDetectPeriodMillis(DEFAULT_FAILURE_DETECT_PERRIOD_MILLIS);
|
||||
this.setConfigLoadPeriodMillis(DEFAULT_CONFIG_LOAD_PERRIOD_MILLIS);
|
||||
}
|
||||
|
||||
public int getMaxPoolSize() {
|
||||
return maxPoolSize;
|
||||
public long getMinEvictableIdleTimeMillis() {
|
||||
return minEvictableIdleTimeMillis;
|
||||
}
|
||||
|
||||
public void setMaxPoolSize(int maxPoolSize) throws SQLException {
|
||||
if (this.isIntited()) {
|
||||
throw new SQLException("dataSource inited");
|
||||
}
|
||||
|
||||
this.maxPoolSize = maxPoolSize;
|
||||
public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
|
||||
this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
@@ -124,6 +121,9 @@ public class CobarDataSource extends MultiDataSource {
|
||||
dataSource.setUrl(url);
|
||||
dataSource.setUsername(username);
|
||||
dataSource.setPassword(password);
|
||||
dataSource.setMaxActive(getMaxPoolSize());
|
||||
dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); // 3 minutes
|
||||
|
||||
if (filters != null && !filters.isEmpty()) {
|
||||
dataSource.setFilters(filters);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user