mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
ha
This commit is contained in:
@@ -20,6 +20,7 @@ import javax.management.ObjectName;
|
||||
|
||||
import com.alibaba.druid.filter.Filter;
|
||||
import com.alibaba.druid.pool.DataSourceAdapter;
|
||||
import com.alibaba.druid.pool.ha.config.ConfigLoader;
|
||||
import com.alibaba.druid.pool.ha.valid.DataSourceFailureDetecter;
|
||||
import com.alibaba.druid.pool.ha.valid.DefaultDataSourceFailureDetecter;
|
||||
import com.alibaba.druid.proxy.jdbc.DataSourceProxy;
|
||||
@@ -27,31 +28,43 @@ import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public abstract class MultiDataSource extends DataSourceAdapter implements MultiDataSourceMBean, DataSourceProxy {
|
||||
|
||||
private Properties properties = new Properties();
|
||||
private Properties properties = new Properties();
|
||||
|
||||
private final AtomicLong connectionIdSeed = new AtomicLong();
|
||||
private final AtomicLong statementIdSeed = new AtomicLong();
|
||||
private final AtomicLong resultSetIdSeed = new AtomicLong();
|
||||
private final AtomicLong transactionIdSeed = new AtomicLong();
|
||||
private final AtomicLong connectionIdSeed = new AtomicLong();
|
||||
private final AtomicLong statementIdSeed = new AtomicLong();
|
||||
private final AtomicLong resultSetIdSeed = new AtomicLong();
|
||||
private final AtomicLong transactionIdSeed = new AtomicLong();
|
||||
|
||||
protected DataSourceFailureDetecter validDataSourceChecker = new DefaultDataSourceFailureDetecter();
|
||||
private long validDataSourceCheckPeriodMillis = 3000;
|
||||
protected DataSourceFailureDetecter validDataSourceChecker = new DefaultDataSourceFailureDetecter();
|
||||
private long validDataSourceCheckPeriodMillis = 3000;
|
||||
|
||||
private int schedulerThreadCount = 3;
|
||||
private ScheduledExecutorService scheduler;
|
||||
private int schedulerThreadCount = 3;
|
||||
private ScheduledExecutorService scheduler;
|
||||
|
||||
private boolean inited = false;
|
||||
private final Lock lock = new ReentrantLock();
|
||||
private boolean inited = false;
|
||||
protected final Lock lock = new ReentrantLock();
|
||||
|
||||
private ConcurrentMap<String, DataSourceHolder> dataSources = new ConcurrentHashMap<String, DataSourceHolder>();
|
||||
|
||||
private ObjectName objectName;
|
||||
private ObjectName objectName;
|
||||
|
||||
private List<Filter> filters = new ArrayList<Filter>();
|
||||
private List<Filter> filters = new ArrayList<Filter>();
|
||||
|
||||
private boolean enable;
|
||||
private boolean enable;
|
||||
|
||||
private String name;
|
||||
private String name;
|
||||
|
||||
private long totalWeight = 0;
|
||||
|
||||
private ConfigLoader configLoader;
|
||||
|
||||
public ConfigLoader getConfigLoader() {
|
||||
return configLoader;
|
||||
}
|
||||
|
||||
public void setConfigLoader(ConfigLoader configLoader) {
|
||||
this.configLoader = configLoader;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
if (name == null) {
|
||||
@@ -68,6 +81,10 @@ public abstract class MultiDataSource extends DataSourceAdapter implements Multi
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public boolean isIntited() {
|
||||
return this.inited;
|
||||
}
|
||||
|
||||
public boolean isEnable() {
|
||||
return enable;
|
||||
}
|
||||
@@ -88,7 +105,7 @@ public abstract class MultiDataSource extends DataSourceAdapter implements Multi
|
||||
|
||||
}
|
||||
|
||||
public void init() {
|
||||
public void init() throws SQLException {
|
||||
if (inited) {
|
||||
return;
|
||||
}
|
||||
@@ -99,6 +116,8 @@ public abstract class MultiDataSource extends DataSourceAdapter implements Multi
|
||||
return;
|
||||
}
|
||||
|
||||
initInternal();
|
||||
|
||||
scheduler = Executors.newScheduledThreadPool(schedulerThreadCount);
|
||||
scheduler.scheduleAtFixedRate(new FailureDetectTask(), validDataSourceCheckPeriodMillis,
|
||||
validDataSourceCheckPeriodMillis, TimeUnit.MILLISECONDS);
|
||||
@@ -110,6 +129,10 @@ public abstract class MultiDataSource extends DataSourceAdapter implements Multi
|
||||
}
|
||||
}
|
||||
|
||||
protected void initInternal() throws SQLException {
|
||||
|
||||
}
|
||||
|
||||
public void resetStat() {
|
||||
|
||||
}
|
||||
@@ -167,10 +190,28 @@ public abstract class MultiDataSource extends DataSourceAdapter implements Multi
|
||||
public Map<String, DataSourceHolder> getDataSources() {
|
||||
return dataSources;
|
||||
}
|
||||
|
||||
public DataSourceHolder getDataSourceHolder(String name) {
|
||||
return dataSources.get(name);
|
||||
}
|
||||
|
||||
public void addDataSource(String name, DataSourceHolder dataSourceHolder) {
|
||||
dataSources.put(name, dataSourceHolder);
|
||||
|
||||
this.totalWeight += dataSourceHolder.getWeight();
|
||||
}
|
||||
|
||||
public Properties getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
public void computeTotalWeight() {
|
||||
int totalWeight = 0;
|
||||
for (DataSourceHolder holder : this.dataSources.values()) {
|
||||
totalWeight += holder.getWeight();
|
||||
}
|
||||
this.totalWeight = totalWeight;
|
||||
}
|
||||
|
||||
public Connection getConnection() throws SQLException {
|
||||
init();
|
||||
@@ -178,11 +219,12 @@ public abstract class MultiDataSource extends DataSourceAdapter implements Multi
|
||||
return new MultiDataSourceConnection(this, createConnectionId());
|
||||
}
|
||||
|
||||
public abstract MultiConnectionHolder getConnectionInternal(MultiDataSourceConnection conn, String sql) throws SQLException;
|
||||
public abstract MultiConnectionHolder getConnectionInternal(MultiDataSourceConnection conn, String sql)
|
||||
throws SQLException;
|
||||
|
||||
public void handleNotAwailableDatasource(DataSourceHolder dataSourceHolder) {
|
||||
}
|
||||
|
||||
|
||||
public String[] getDataSourceNames() {
|
||||
return this.dataSources.keySet().toArray(new String[this.dataSources.size()]);
|
||||
}
|
||||
@@ -221,14 +263,14 @@ public abstract class MultiDataSource extends DataSourceAdapter implements Multi
|
||||
public long createTransactionId() {
|
||||
return transactionIdSeed.incrementAndGet();
|
||||
}
|
||||
|
||||
|
||||
public boolean restartDataSource(String name) {
|
||||
DataSourceHolder holder = this.getDataSources().get(name);
|
||||
if (holder != null) {
|
||||
holder.restart();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user