use of io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration 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.api.config.MasterSlaveRuleConfiguration in project sharding-jdbc by shardingjdbc.
the class MasterSlaveDataSourceTest method assertGetDatabaseProductNameWhenDataBaseProductNameDifferent.
@Test(expected = IllegalStateException.class)
public void assertGetDatabaseProductNameWhenDataBaseProductNameDifferent() throws SQLException {
DataSource masterDataSource = mock(DataSource.class);
DataSource slaveDataSource = mock(DataSource.class);
Connection masterConnection = mockConnection("MySQL");
final Connection slaveConnection = mockConnection("H2");
Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
dataSourceMap.put("masterDataSource", masterDataSource);
dataSourceMap.put("slaveDataSource", slaveDataSource);
when(masterDataSource.getConnection()).thenReturn(masterConnection);
when(slaveDataSource.getConnection()).thenReturn(slaveConnection);
MasterSlaveRuleConfiguration masterSlaveRuleConfig = new MasterSlaveRuleConfiguration("ds", "masterDataSource", Collections.singletonList("slaveDataSource"));
try {
((MasterSlaveDataSource) MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, masterSlaveRuleConfig, Collections.<String, Object>emptyMap())).getDatabaseType();
} finally {
verify(masterConnection).close();
verify(slaveConnection).close();
}
}
use of io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration in project sharding-jdbc by shardingjdbc.
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);
Map<String, DataSource> dataSourceMap = new HashMap<>(3, 1);
dataSourceMap.put("masterDataSource", masterDataSource);
dataSourceMap.put("slaveDataSource1", slaveDataSource1);
dataSourceMap.put("slaveDataSource2", slaveDataSource2);
assertThat(((MasterSlaveDataSource) MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, new MasterSlaveRuleConfiguration("ds", "masterDataSource", Arrays.asList("slaveDataSource1", "slaveDataSource2")), Collections.<String, Object>emptyMap())).getDatabaseType(), is(DatabaseType.H2));
verify(masterConnection).close();
verify(slaveConnection1).close();
verify(slaveConnection2).close();
}
use of io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration in project sharding-jdbc by shardingjdbc.
the class ShardingDataSourceTest method assertGetDatabaseProductNameForMasterSlave.
@Test
public void assertGetDatabaseProductNameForMasterSlave() throws SQLException {
final DataSource dataSource1 = mockDataSource("H2");
DataSource masterDataSource = mockDataSource("H2");
DataSource slaveDataSource = mockDataSource("H2");
Map<String, DataSource> slaveDataSourceMap = new HashMap<>(2, 1);
slaveDataSourceMap.put("masterDataSource", masterDataSource);
slaveDataSourceMap.put("slaveDataSource", slaveDataSource);
MasterSlaveDataSource dataSource2 = (MasterSlaveDataSource) MasterSlaveDataSourceFactory.createDataSource(slaveDataSourceMap, new MasterSlaveRuleConfiguration("ds", "masterDataSource", Collections.singletonList("slaveDataSource")), Collections.<String, Object>emptyMap());
DataSource dataSource3 = mockDataSource("H2");
Map<String, DataSource> dataSourceMap = new HashMap<>(3, 1);
dataSourceMap.put("ds1", dataSource1);
dataSourceMap.put("ds2", dataSource2);
dataSourceMap.put("ds3", dataSource3);
assertDatabaseProductName(dataSourceMap, dataSource1.getConnection(), dataSource2.getDataSourceMap().get("masterDataSource").getConnection(), dataSource2.getDataSourceMap().get("slaveDataSource").getConnection());
}
use of io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration in project sharding-jdbc by shardingjdbc.
the class ShardingDataSourceTest method assertGetDatabaseProductNameWhenDataBaseProductNameDifferentForMasterSlave.
@Test(expected = IllegalStateException.class)
public void assertGetDatabaseProductNameWhenDataBaseProductNameDifferentForMasterSlave() throws SQLException {
final DataSource dataSource1 = mockDataSource("MySQL");
DataSource masterDataSource = mockDataSource("H2");
DataSource slaveDataSource = mockDataSource("H2");
Map<String, DataSource> masterSlaveDataSourceMap = new HashMap<>(2, 1);
masterSlaveDataSourceMap.put("masterDataSource", masterDataSource);
masterSlaveDataSourceMap.put("slaveDataSource", slaveDataSource);
MasterSlaveDataSource dataSource2 = (MasterSlaveDataSource) MasterSlaveDataSourceFactory.createDataSource(masterSlaveDataSourceMap, new MasterSlaveRuleConfiguration("ds", "masterDataSource", Collections.singletonList("slaveDataSource")), Collections.<String, Object>emptyMap());
Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
dataSourceMap.put("ds1", dataSource1);
dataSourceMap.put("ds2", dataSource2);
assertDatabaseProductName(dataSourceMap, dataSource1.getConnection(), dataSource2.getDataSourceMap().get("masterDataSource").getConnection(), dataSource2.getDataSourceMap().get("slaveDataSource").getConnection());
}
Aggregations