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@456 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -6,24 +6,48 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import com.alibaba.druid.logging.Log;
|
||||
import com.alibaba.druid.logging.LogFactory;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
public class HADataSource extends MultiDataSource implements DataSource {
|
||||
|
||||
private AtomicInteger requestCount = new AtomicInteger();
|
||||
private final static Log LOG = LogFactory.getLog(HADataSource.class);
|
||||
|
||||
private AtomicInteger requestCount = new AtomicInteger();
|
||||
|
||||
@Override
|
||||
public Connection getConnection() throws SQLException {
|
||||
int requestNumber = requestCount.getAndIncrement();
|
||||
int size = dataSources.size();
|
||||
int index = requestNumber % size;
|
||||
int tryCount = 0;
|
||||
for (;;) {
|
||||
int requestNumber = requestCount.getAndIncrement();
|
||||
int size = dataSources.size();
|
||||
int index = requestNumber % size;
|
||||
|
||||
DruidDataSource dataSource = dataSources.get(index);
|
||||
Connection conn = dataSource.getConnection();
|
||||
DruidDataSource dataSource = dataSources.get(index);
|
||||
Connection conn = null;
|
||||
|
||||
return conn;
|
||||
try {
|
||||
tryCount++;
|
||||
conn = dataSource.getConnection();
|
||||
} catch (SQLException ex) {
|
||||
LOG.error("getConnection error", ex);
|
||||
|
||||
if (tryCount >= size) {
|
||||
throw ex;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (conn != null) {
|
||||
if (!dataSource.isTestOnBorrow()) {
|
||||
dataSource.testConnection(conn);
|
||||
}
|
||||
}
|
||||
|
||||
return conn;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user