mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
支持HA
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@458 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user