use of io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource in project sharding-jdbc by shardingjdbc.
the class OrchestrationFacade method reviseShardingRuleConfigurationForMasterSlave.
private void reviseShardingRuleConfigurationForMasterSlave(final Map<String, DataSource> dataSourceMap, final ShardingRuleConfiguration shardingRuleConfig) {
for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
if (entry.getValue() instanceof MasterSlaveDataSource) {
MasterSlaveDataSource masterSlaveDataSource = (MasterSlaveDataSource) entry.getValue();
shardingRuleConfig.getMasterSlaveRuleConfigs().add(getMasterSlaveRuleConfiguration(masterSlaveDataSource));
}
}
}
use of io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource in project sharding-jdbc by shardingjdbc.
the class OrchestrationSpringBootMasterSlaveTest method assertWithMasterSlaveDataSource.
@Test
public void assertWithMasterSlaveDataSource() {
assertTrue(dataSource instanceof MasterSlaveDataSource);
for (DataSource each : ((MasterSlaveDataSource) dataSource).getAllDataSources().values()) {
assertThat(((BasicDataSource) each).getMaxTotal(), is(16));
}
Map<String, Object> configMap = new ConcurrentHashMap<>();
configMap.put("key1", "value1");
assertThat(ConfigMapContext.getInstance().getMasterSlaveConfig(), is(configMap));
}
use of io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource in project sharding-jdbc by shardingjdbc.
the class AbstractShardingMasterSlaveTest method getMasterSlaveDataSourceMap.
// TODO use MasterSlaveRuleConfiguration to generate data source map
private Map<String, DataSource> getMasterSlaveDataSourceMap(final Entry<DatabaseType, Map<String, DataSource>> each) throws SQLException {
Map<String, DataSource> masterSlaveDataSourceMap = each.getValue();
MasterSlaveDataSource masterSlaveDs0 = getMasterSlaveDataSource(masterSlaveDataSourceMap, "ms_0", "dataSource_master_0", "dataSource_slave_0");
MasterSlaveDataSource masterSlaveDs1 = getMasterSlaveDataSource(masterSlaveDataSourceMap, "ms_1", "dataSource_master_1", "dataSource_slave_1");
MasterSlaveDataSource masterSlaveDs2 = getMasterSlaveDataSource(masterSlaveDataSourceMap, "ms_2", "dataSource_master_2", "dataSource_slave_2");
MasterSlaveDataSource masterSlaveDs3 = getMasterSlaveDataSource(masterSlaveDataSourceMap, "ms_3", "dataSource_master_3", "dataSource_slave_3");
MasterSlaveDataSource masterSlaveDs4 = getMasterSlaveDataSource(masterSlaveDataSourceMap, "ms_4", "dataSource_master_4", "dataSource_slave_4");
MasterSlaveDataSource masterSlaveDs5 = getMasterSlaveDataSource(masterSlaveDataSourceMap, "ms_5", "dataSource_master_5", "dataSource_slave_5");
MasterSlaveDataSource masterSlaveDs6 = getMasterSlaveDataSource(masterSlaveDataSourceMap, "ms_6", "dataSource_master_6", "dataSource_slave_6");
MasterSlaveDataSource masterSlaveDs7 = getMasterSlaveDataSource(masterSlaveDataSourceMap, "ms_7", "dataSource_master_7", "dataSource_slave_7");
MasterSlaveDataSource masterSlaveDs8 = getMasterSlaveDataSource(masterSlaveDataSourceMap, "ms_8", "dataSource_master_8", "dataSource_slave_8");
MasterSlaveDataSource masterSlaveDs9 = getMasterSlaveDataSource(masterSlaveDataSourceMap, "ms_9", "dataSource_master_9", "dataSource_slave_9");
Map<String, DataSource> result = new HashMap<>(10);
result.put("ms_0", masterSlaveDs0);
result.put("ms_1", masterSlaveDs1);
result.put("ms_2", masterSlaveDs2);
result.put("ms_3", masterSlaveDs3);
result.put("ms_4", masterSlaveDs4);
result.put("ms_5", masterSlaveDs5);
result.put("ms_6", masterSlaveDs6);
result.put("ms_7", masterSlaveDs7);
result.put("ms_8", masterSlaveDs8);
result.put("ms_9", masterSlaveDs9);
return result;
}
use of io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource in project sharding-jdbc by shardingjdbc.
the class ShardingConnection method getConnectionsForDDL.
/**
* Get database connections via data source name for DDL.
*
* <p>Non-Master-slave connection will return actual connection</p>
* <p>Master-slave connection will return actual master connections</p>
*
* @param dataSourceName data source name
* @return all database connections via data source name for DDL
* @throws SQLException SQL exception
*/
// TODO Return value is Connection because will support multiple master datasources in future.
public Collection<Connection> getConnectionsForDDL(final String dataSourceName) throws SQLException {
DataSource dataSource = shardingContext.getDataSourceMap().get(dataSourceName);
Preconditions.checkState(null != dataSource, "Missing the rule of %s in DataSourceRule", dataSourceName);
Map<String, DataSource> dataSources;
if (dataSource instanceof MasterSlaveDataSource) {
dataSources = ((MasterSlaveDataSource) dataSource).getMasterDataSource();
} else {
dataSources = new HashMap<>(1, 1);
dataSources.put(dataSourceName, dataSource);
}
Collection<Connection> result = new LinkedList<>();
for (Entry<String, DataSource> entry : dataSources.entrySet()) {
Connection connection = getCachedConnections().containsKey(entry.getKey()) ? getCachedConnections().get(entry.getKey()) : entry.getValue().getConnection();
replayMethodsInvocation(connection);
getCachedConnections().put(entry.getKey(), connection);
result.add(connection);
}
return result;
}
Aggregations