use of io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource in project sharding-jdbc by shardingjdbc.
the class ShardingConnection method getConnection.
/**
* Get database connection via data source name.
*
* @param dataSourceName data source name
* @param sqlType SQL type
* @return all database connections via data source name
* @throws SQLException SQL exception
*/
public Connection getConnection(final String dataSourceName, final SQLType sqlType) throws SQLException {
if (getCachedConnections().containsKey(dataSourceName)) {
return getCachedConnections().get(dataSourceName);
}
DataSource dataSource = shardingContext.getDataSourceMap().get(dataSourceName);
Preconditions.checkState(null != dataSource, "Missing the rule of %s in DataSourceRule", dataSourceName);
String realDataSourceName;
if (dataSource instanceof MasterSlaveDataSource) {
NamedDataSource namedDataSource = ((MasterSlaveDataSource) dataSource).getDataSource(sqlType);
realDataSourceName = namedDataSource.getName();
if (getCachedConnections().containsKey(realDataSourceName)) {
return getCachedConnections().get(realDataSourceName);
}
dataSource = namedDataSource.getDataSource();
} else {
realDataSourceName = dataSourceName;
}
Connection result = dataSource.getConnection();
getCachedConnections().put(realDataSourceName, result);
replayMethodsInvocation(result);
return result;
}
use of io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource in project sharding-jdbc by shardingjdbc.
the class SpringBootMasterSlaveTest 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 OrchestrationFacade method getActualDataSourceMapForMasterSlave.
private Map<String, DataSource> getActualDataSourceMapForMasterSlave(final Map<String, DataSource> dataSourceMap) {
Map<String, DataSource> result = new LinkedHashMap<>();
for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
if (entry.getValue() instanceof MasterSlaveDataSource) {
MasterSlaveDataSource masterSlaveDataSource = (MasterSlaveDataSource) entry.getValue();
result.putAll(masterSlaveDataSource.getAllDataSources());
} else {
result.put(entry.getKey(), entry.getValue());
}
}
return result;
}
use of io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource in project sharding-jdbc by shardingjdbc.
the class ShardingConnectionTest method init.
@BeforeClass
public static void init() throws SQLException {
DataSource masterDataSource = new TestDataSource("test_ds_master");
DataSource slaveDataSource = new TestDataSource("test_ds_slave");
Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
dataSourceMap.put("test_ds_master", masterDataSource);
dataSourceMap.put("test_ds_slave", slaveDataSource);
masterSlaveDataSource = new MasterSlaveDataSource(dataSourceMap, new MasterSlaveRuleConfiguration("test_ds", "test_ds_master", Collections.singletonList("test_ds_slave")), Collections.<String, Object>emptyMap());
((TestDataSource) slaveDataSource).setThrowExceptionWhenClosing(true);
}
use of io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource in project sharding-jdbc by shardingjdbc.
the class ShardingConnectionTest method setUp.
@Before
public void setUp() {
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration();
tableRuleConfig.setLogicTable("test");
shardingRuleConfig.getTableRuleConfigs().add(tableRuleConfig);
Map<String, DataSource> dataSourceMap = new HashMap<>(1, 1);
dataSourceMap.put(DS_NAME, masterSlaveDataSource);
ShardingContext shardingContext = new ShardingContext(dataSourceMap, new ShardingRule(shardingRuleConfig, dataSourceMap.keySet()), null, null, false);
connection = new ShardingConnection(shardingContext);
}
Aggregations