mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
ha
This commit is contained in:
@@ -31,7 +31,7 @@ import com.alibaba.druid.pool.ha.valid.DefaultDataSourceFailureDetecter;
|
||||
import com.alibaba.druid.proxy.jdbc.DataSourceProxy;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public abstract class MultiDataSource extends DataSourceAdapter implements MultiDataSourceMBean, DataSourceProxy {
|
||||
public class MultiDataSource extends DataSourceAdapter implements MultiDataSourceMBean, DataSourceProxy {
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(MultiDataSource.class);
|
||||
|
||||
@@ -78,7 +78,7 @@ public abstract class MultiDataSource extends DataSourceAdapter implements Multi
|
||||
|
||||
private Random random;
|
||||
|
||||
private int maxPoolSize;
|
||||
private int maxPoolSize = 50;
|
||||
|
||||
private long activeCount = 0;
|
||||
|
||||
@@ -319,7 +319,7 @@ public abstract class MultiDataSource extends DataSourceAdapter implements Multi
|
||||
if (activeCount >= maxPoolSize) {
|
||||
notFull.await();
|
||||
}
|
||||
|
||||
|
||||
MultiDataSourceConnection conn = new MultiDataSourceConnection(this, createConnectionId());
|
||||
|
||||
activeCount++;
|
||||
@@ -341,6 +341,10 @@ public abstract class MultiDataSource extends DataSourceAdapter implements Multi
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public int produceRandomNumber() {
|
||||
return random.nextInt(totalWeight);
|
||||
}
|
||||
|
||||
public MultiConnectionHolder getConnectionInternal(MultiDataSourceConnection multiConn, String sql)
|
||||
throws SQLException {
|
||||
@@ -349,7 +353,7 @@ public abstract class MultiDataSource extends DataSourceAdapter implements Multi
|
||||
|
||||
final int MAX_RETRY = 10;
|
||||
for (int i = 0; i < MAX_RETRY; ++i) {
|
||||
int randomNumber = random.nextInt(totalWeight);
|
||||
int randomNumber = produceRandomNumber();
|
||||
DataSourceHolder first = null;
|
||||
|
||||
boolean needRetry = false;
|
||||
|
||||
@@ -147,10 +147,16 @@ public class MultiDataSourceConnection extends WrapperAdapter implements Connect
|
||||
|
||||
@Override
|
||||
public void close() throws SQLException {
|
||||
if (closed) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (conn != null) {
|
||||
conn.close();
|
||||
}
|
||||
this.closed = true;
|
||||
|
||||
haDataSource.afterConnectionClosed(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -29,4 +29,10 @@ public interface MultiDataSourceMBean {
|
||||
long getFailureDetectPeriodMillis();
|
||||
|
||||
long getConfigLoadPeriodMillis();
|
||||
|
||||
long getActiveCount();
|
||||
|
||||
int getMaxPoolSize();
|
||||
|
||||
int produceRandomNumber();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.alibaba.druid.bvt.pool.ha;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.pool.ha.MultiDataSource;
|
||||
|
||||
|
||||
public class MultiDataSourceTest extends TestCase {
|
||||
public void test_0 () throws Exception {
|
||||
MultiDataSource multiDataSource = new MultiDataSource();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user