git-svn-id: http://code.alibabatech.com/svn/druid/trunk@458 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
wenshao
2011-07-17 05:34:30 +00:00
parent d85c4b94f6
commit 91c276c92c
8 changed files with 125 additions and 75 deletions
@@ -2,7 +2,6 @@ package com.alibaba.druid.pool.ha;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
@@ -17,14 +16,14 @@ public class HADataSource extends MultiDataSource implements DataSource {
private final static Log LOG = LogFactory.getLog(HADataSource.class);
private AtomicInteger requestCount = new AtomicInteger();
private final AtomicInteger requestCount = new AtomicInteger();
private final AtomicInteger indexErrorCount = new AtomicInteger();
protected final List<DruidDataSource> notAvailableDatasources = new CopyOnWriteArrayList<DruidDataSource>();
public HADataSource() {
super(new ArrayList<DruidDataSource>());
public HADataSource(){
}
@Override
public Connection getConnection() throws SQLException {
int tryCount = 0;
@@ -32,15 +31,34 @@ public class HADataSource extends MultiDataSource implements DataSource {
for (;;) {
int size = dataSources.size();
if (size == 0) {
throw new SQLException("can not get connection, no availabe datasources");
}
int index = requestNumber % size;
DruidDataSource dataSource = dataSources.get(index);
DruidDataSource dataSource = null;
try {
// 处理并发时的错误
dataSource = dataSources.get(index);
} catch (Exception ex) {
indexErrorCount.incrementAndGet();
if (LOG.isDebugEnabled()) {
LOG.debug("getDataSource error, index : " + index, ex);
}
continue;
}
assert dataSource != null;
if (!dataSource.isEnable()) {
boolean removed = dataSources.remove(dataSource);
if (removed) {
notAvailableDatasources.add(dataSource);
}
continue;
}
Connection conn = null;
@@ -62,4 +80,8 @@ public class HADataSource extends MultiDataSource implements DataSource {
}
}
public long getIndexErrorCount() {
return indexErrorCount.get();
}
}