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@466 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -20,11 +20,11 @@ public class HADataSource extends MultiDataSource implements DataSource {
|
||||
|
||||
private final AtomicInteger indexErrorCount = new AtomicInteger();
|
||||
|
||||
protected ArrayList<DruidDataSource> dataSources;
|
||||
protected ArrayList<DruidDataSource> dataSources = new ArrayList<DruidDataSource>();
|
||||
protected final List<DruidDataSource> notAvailableDatasources = new CopyOnWriteArrayList<DruidDataSource>();
|
||||
|
||||
public HADataSource(){
|
||||
dataSources = new ArrayList<DruidDataSource>();
|
||||
|
||||
}
|
||||
|
||||
public List<DruidDataSource> getDataSources() {
|
||||
@@ -40,7 +40,15 @@ public class HADataSource extends MultiDataSource implements DataSource {
|
||||
return new MultiDataSourceConnection(this, createConnectionId());
|
||||
}
|
||||
|
||||
public Connection getConnectionInternal(int connectionId, String sql) throws SQLException {
|
||||
protected int indexFor(MultiDataSourceConnection multiDataSourceConnection, String sql) {
|
||||
int size = dataSources.size();
|
||||
int connectionId = (int) multiDataSourceConnection.getId();
|
||||
|
||||
return connectionId % size;
|
||||
}
|
||||
|
||||
public Connection getConnectionInternal(MultiDataSourceConnection multiDataSourceConnection, String sql) throws SQLException {
|
||||
|
||||
int tryCount = 0;
|
||||
|
||||
for (;;) {
|
||||
@@ -50,7 +58,7 @@ public class HADataSource extends MultiDataSource implements DataSource {
|
||||
throw new SQLException("can not get connection, no availabe datasources");
|
||||
}
|
||||
|
||||
int index = connectionId % size;
|
||||
int index = indexFor(multiDataSourceConnection, sql);
|
||||
|
||||
DruidDataSource dataSource = null;
|
||||
|
||||
|
||||
@@ -40,5 +40,5 @@ public abstract class MultiDataSource extends DataSourceAdapter {
|
||||
@Override
|
||||
public abstract Connection getConnection() throws SQLException;
|
||||
|
||||
public abstract Connection getConnectionInternal(int connectionId, String sql) throws SQLException;
|
||||
public abstract Connection getConnectionInternal(MultiDataSourceConnection conn, String sql) throws SQLException;
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ public class MultiDataSourceConnection extends WrapperAdapter implements Connect
|
||||
|
||||
public void checkConnection(String sql) throws SQLException {
|
||||
if (conn == null) {
|
||||
conn = haDataSource.getConnectionInternal(id, sql);
|
||||
conn = haDataSource.getConnectionInternal(this, sql);
|
||||
}
|
||||
|
||||
if (autoCommit != null) {
|
||||
|
||||
Reference in New Issue
Block a user