mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
HAPoolTest
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@816 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -149,6 +149,10 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isInited() {
|
||||
return this.inited;
|
||||
}
|
||||
|
||||
private void init() throws SQLException {
|
||||
if (inited) {
|
||||
|
||||
@@ -9,12 +9,16 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import com.alibaba.druid.logging.Log;
|
||||
import com.alibaba.druid.logging.LogFactory;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.pool.ha.balance.Balancer;
|
||||
import com.alibaba.druid.pool.ha.balance.RoundRobinBlancer;
|
||||
|
||||
public class HADataSource extends MultiDataSource implements DataSource {
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(HADataSource.class);
|
||||
|
||||
protected ArrayList<DruidDataSource> dataSources = new ArrayList<DruidDataSource>();
|
||||
protected final List<DruidDataSource> notAvailableDatasources = new CopyOnWriteArrayList<DruidDataSource>();
|
||||
|
||||
@@ -28,9 +32,15 @@ public class HADataSource extends MultiDataSource implements DataSource {
|
||||
return Collections.unmodifiableList(dataSources);
|
||||
}
|
||||
|
||||
public void setDataSources(List<DruidDataSource> dataSources) {
|
||||
public synchronized void setDataSources(List<DruidDataSource> dataSources) {
|
||||
this.dataSources = new ArrayList<DruidDataSource>(dataSources);
|
||||
}
|
||||
|
||||
public synchronized void addDataSource(DruidDataSource dataSource) {
|
||||
ArrayList<DruidDataSource> newList = new ArrayList<DruidDataSource>(dataSources);
|
||||
newList.add(dataSource);
|
||||
this.dataSources = newList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection() throws SQLException {
|
||||
@@ -56,4 +66,21 @@ public class HADataSource extends MultiDataSource implements DataSource {
|
||||
}
|
||||
}
|
||||
|
||||
public void close() {
|
||||
for (DruidDataSource item : dataSources) {
|
||||
try {
|
||||
item.close();
|
||||
} catch (Exception ex) {
|
||||
LOG.error("close dataSource error", ex);
|
||||
}
|
||||
}
|
||||
for (DruidDataSource item : notAvailableDatasources) {
|
||||
try {
|
||||
item.close();
|
||||
} catch (Exception ex) {
|
||||
LOG.error("close dataSource error", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ public class RoundRobinBlancer implements Balancer {
|
||||
|
||||
assert dataSource != null;
|
||||
|
||||
if (!dataSource.isEnable()) {
|
||||
if (dataSource.isInited() && !dataSource.isEnable()) {
|
||||
multiDataSource.handleNotAwailableDatasource(dataSource);
|
||||
continue;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user