use of javax.sql.DataSource in project sharding-jdbc by dangdangdotcom.
the class MasterSlaveDataSourceTest method assertGetDatabaseProductName.
@Test
public void assertGetDatabaseProductName() throws SQLException {
DataSource masterDataSource = mock(DataSource.class);
DataSource slaveDataSource1 = mock(DataSource.class);
DataSource slaveDataSource2 = mock(DataSource.class);
Connection masterConnection = mockConnection("H2");
Connection slaveConnection1 = mockConnection("H2");
Connection slaveConnection2 = mockConnection("H2");
when(masterDataSource.getConnection()).thenReturn(masterConnection);
when(slaveDataSource1.getConnection()).thenReturn(slaveConnection1);
when(slaveDataSource2.getConnection()).thenReturn(slaveConnection2);
assertThat(((MasterSlaveDataSource) MasterSlaveDataSourceFactory.createDataSource("ds", masterDataSource, slaveDataSource1, slaveDataSource2)).getDatabaseProductName(), is("H2"));
verify(masterConnection).close();
verify(slaveConnection1).close();
verify(slaveConnection2).close();
}
use of javax.sql.DataSource in project sharding-jdbc by dangdangdotcom.
the class ShardingConnectionTest method setUp.
@Before
public void setUp() {
Map<String, DataSource> dataSourceMap = new HashMap<>(1);
dataSourceMap.put(DS_NAME, MASTER_SLAVE_DATA_SOURCE);
DataSourceRule dataSourceRule = new DataSourceRule(dataSourceMap);
ShardingRule rule = new ShardingRule.ShardingRuleBuilder().dataSourceRule(dataSourceRule).tableRules(Collections.singleton(new TableRule.TableRuleBuilder("test").dataSourceRule(dataSourceRule).build())).build();
ShardingContext sc = new ShardingContext(rule, null, null);
connection = new ShardingConnection(sc);
}
use of javax.sql.DataSource in project sharding-jdbc by dangdangdotcom.
the class ShardingDataSourceTest method createShardingDataSource.
private ShardingDataSource createShardingDataSource(final DataSource dataSource) {
Map<String, DataSource> dataSourceMap = new HashMap<>(1);
dataSourceMap.put("ds", dataSource);
DataSourceRule dataSourceRule = new DataSourceRule(dataSourceMap);
TableRule tableRule = TableRule.builder("logicTable").actualTables(Arrays.asList("table_0", "table_1", "table_2")).dataSourceRule(dataSourceRule).build();
return new ShardingDataSource(ShardingRule.builder().dataSourceRule(dataSourceRule).tableRules(Collections.singletonList(tableRule)).build());
}
use of javax.sql.DataSource in project sharding-jdbc by dangdangdotcom.
the class DatabaseTest method setRouteRuleContext.
@Before
public void setRouteRuleContext() {
Map<String, DataSource> dataSourceMap = new HashMap<>();
dataSourceMap.put("ds_0", null);
dataSourceMap.put("ds_1", null);
DataSourceRule dataSourceRule = new DataSourceRule(dataSourceMap);
shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule).databaseShardingStrategy(new DatabaseShardingStrategy(new OrderDatabaseShardingAlgorithm())).build();
}
use of javax.sql.DataSource in project sharding-jdbc by dangdangdotcom.
the class YamlIntegratedTest method testWithDataSource.
@Test
public void testWithDataSource() throws SQLException, URISyntaxException, IOException {
File yamlFile = new File(YamlIntegratedTest.class.getResource(filePath).toURI());
DataSource dataSource;
if (hasDataSource) {
dataSource = new YamlShardingDataSource(yamlFile);
} else {
dataSource = new YamlShardingDataSource(Maps.asMap(Sets.newHashSet("db0", "db1"), new Function<String, DataSource>() {
@Override
public DataSource apply(final String key) {
return createDataSource(key);
}
}), yamlFile);
}
try (Connection conn = dataSource.getConnection();
Statement stm = conn.createStatement()) {
stm.executeQuery("SELECT * FROM t_order");
stm.executeQuery("SELECT * FROM t_order_item");
stm.executeQuery("SELECT * FROM config");
}
}
Aggregations