Search in sources :

Example 6 with MasterSlaveRuleConfiguration

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);
}
Also used : MasterSlaveRuleConfiguration(io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration) HashMap(java.util.HashMap) TestDataSource(io.shardingjdbc.core.fixture.TestDataSource) MasterSlaveDataSource(io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource) TestDataSource(io.shardingjdbc.core.fixture.TestDataSource) DataSource(javax.sql.DataSource) MasterSlaveDataSource(io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource) BeforeClass(org.junit.BeforeClass)

Example 7 with MasterSlaveRuleConfiguration

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();
    }
}
Also used : MasterSlaveRuleConfiguration(io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration) HashMap(java.util.HashMap) Connection(java.sql.Connection) MasterSlaveConnection(io.shardingjdbc.core.jdbc.core.connection.MasterSlaveConnection) DataSource(javax.sql.DataSource) TestDataSource(io.shardingjdbc.core.fixture.TestDataSource) Test(org.junit.Test)

Example 8 with MasterSlaveRuleConfiguration

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();
}
Also used : MasterSlaveRuleConfiguration(io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration) HashMap(java.util.HashMap) Connection(java.sql.Connection) MasterSlaveConnection(io.shardingjdbc.core.jdbc.core.connection.MasterSlaveConnection) DataSource(javax.sql.DataSource) TestDataSource(io.shardingjdbc.core.fixture.TestDataSource) Test(org.junit.Test)

Example 9 with MasterSlaveRuleConfiguration

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());
}
Also used : MasterSlaveRuleConfiguration(io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration) HashMap(java.util.HashMap) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 10 with MasterSlaveRuleConfiguration

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());
}
Also used : MasterSlaveRuleConfiguration(io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration) HashMap(java.util.HashMap) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Aggregations

MasterSlaveRuleConfiguration (io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration)18 Test (org.junit.Test)12 HashMap (java.util.HashMap)7 DataSource (javax.sql.DataSource)7 TestDataSource (io.shardingjdbc.core.fixture.TestDataSource)5 MasterSlaveDataSource (io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource)3 YamlMasterSlaveRuleConfiguration (io.shardingjdbc.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration)3 ShardingRuleConfiguration (io.shardingjdbc.core.api.config.ShardingRuleConfiguration)2 MasterSlaveConnection (io.shardingjdbc.core.jdbc.core.connection.MasterSlaveConnection)2 OrchestrationFacade (io.shardingjdbc.orchestration.internal.OrchestrationFacade)2 ConfigurationService (io.shardingjdbc.orchestration.internal.config.ConfigurationService)2 YamlOrchestrationMasterSlaveRuleConfiguration (io.shardingjdbc.orchestration.yaml.YamlOrchestrationMasterSlaveRuleConfiguration)2 Connection (java.sql.Connection)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 RoundRobinMasterSlaveLoadBalanceAlgorithm (io.shardingjdbc.core.api.algorithm.masterslave.RoundRobinMasterSlaveLoadBalanceAlgorithm)1 LinkedList (java.util.LinkedList)1 BeforeClass (org.junit.BeforeClass)1