Skip to content

Commit

Permalink
for #85 refactor logic data source name
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed May 31, 2016
1 parent a4db1e7 commit efbce58
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ public final class MasterSlaveDataSourceFactory {
/**
* 创建读写分离数据源.
*
* @param logicDataSourceName 逻辑数据源名称
* @param name 读写分离数据源名称
* @param masterDataSource 主节点数据源
* @param slaveDataSource 从节点数据源
* @param otherSlaveDataSources 其他从节点数据源
* @return 读写分离数据源
*/
public static DataSource createDataSource(final String logicDataSourceName, final DataSource masterDataSource, final DataSource slaveDataSource, final DataSource... otherSlaveDataSources) {
return new MasterSlaveDataSource(logicDataSourceName, masterDataSource, Lists.asList(slaveDataSource, otherSlaveDataSources));
public static DataSource createDataSource(final String name, final DataSource masterDataSource, final DataSource slaveDataSource, final DataSource... otherSlaveDataSources) {
return new MasterSlaveDataSource(name, masterDataSource, Lists.asList(slaveDataSource, otherSlaveDataSources));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public final class RoundRobinSlaveLoadBalanceStrategy implements SlaveLoadBalanc
private static final ConcurrentHashMap<String, AtomicInteger> COUNT_MAP = new ConcurrentHashMap<>();

@Override
public DataSource getDataSource(final String logicDataSource, final List<DataSource> slaveDataSources) {
AtomicInteger count = COUNT_MAP.containsKey(logicDataSource) ? COUNT_MAP.get(logicDataSource) : new AtomicInteger(0);
COUNT_MAP.putIfAbsent(logicDataSource, count);
public DataSource getDataSource(final String name, final List<DataSource> slaveDataSources) {
AtomicInteger count = COUNT_MAP.containsKey(name) ? COUNT_MAP.get(name) : new AtomicInteger(0);
COUNT_MAP.putIfAbsent(name, count);
count.compareAndSet(slaveDataSources.size(), 0);
return slaveDataSources.get(count.getAndIncrement() % slaveDataSources.size());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public interface SlaveLoadBalanceStrategy {
/**
* 根据负载均衡策略获取从库数据源.
*
* @param logicDataSource 逻辑数据源名称
* @param name 读写分离数据源名称
* @param slaveDataSources 从库数据源列表
* @return 选中的从库数据源
*/
DataSource getDataSource(final String logicDataSource, final List<DataSource> slaveDataSources);
DataSource getDataSource(final String name, final List<DataSource> slaveDataSources);
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected Boolean initialValue() {
}
};

private final String logicDataSourceName;
private final String name;

private final DataSource masterDataSource;

Expand All @@ -64,7 +64,7 @@ public DataSource getDataSource(final SQLStatementType sqlStatementType) {
WAS_UPDATED.set(true);
return masterDataSource;
}
return slaveLoadBalanceStrategy.getDataSource(logicDataSourceName, slaveDataSources);
return slaveLoadBalanceStrategy.getDataSource(name, slaveDataSources);
}

String getDatabaseProductName() throws SQLException {
Expand Down

0 comments on commit efbce58

Please sign in to comment.