}
@Override
public Connection getConnection(MultiDataSourceConnection connectionProxy, String sql) throws SQLException {
MultiDataSource multiDataSource = connectionProxy.getHaDataSource();
int tryCount = 0;
for (;;) {
int size = multiDataSource.getDataSources().size();
long connectionId = (int) connectionProxy.getId();
if (size == 0) {
throw new SQLException("can not get connection, no availabe datasources");
}
int index = (int) (connectionId % size);
DruidDataSource dataSource = null;
try {
// 处理并发时的错误
dataSource = multiDataSource.getDataSources().get(index);
} catch (Exception ex) {
indexErrorCount.incrementAndGet();
if (LOG.isDebugEnabled()) {
LOG.debug("getDataSource error, index : " + index, ex);
}
continue;
}
assert dataSource != null;
if (!dataSource.isEnable()) {
multiDataSource.handleNotAwailableDatasource(dataSource);
continue;
}
Connection conn = null;