Search in sources :

Example 6 with MasterSlaveDataSource

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));
        }
    }
}
Also used : MasterSlaveDataSource(io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource) ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) DataSource(javax.sql.DataSource) MasterSlaveDataSource(io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource)

Example 7 with 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));
}
Also used : MasterSlaveDataSource(io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource) DataSource(javax.sql.DataSource) MasterSlaveDataSource(io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 8 with MasterSlaveDataSource

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;
}
Also used : HashMap(java.util.HashMap) MasterSlaveDataSource(io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource) DataSource(javax.sql.DataSource) ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) MasterSlaveDataSource(io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource)

Example 9 with MasterSlaveDataSource

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;
}
Also used : MasterSlaveDataSource(io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource) Connection(java.sql.Connection) LinkedList(java.util.LinkedList) DataSource(javax.sql.DataSource) NamedDataSource(io.shardingjdbc.core.jdbc.core.datasource.NamedDataSource) MasterSlaveDataSource(io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource)

Aggregations

MasterSlaveDataSource (io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource)9 DataSource (javax.sql.DataSource)9 ShardingDataSource (io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource)3 HashMap (java.util.HashMap)3 TestDataSource (io.shardingjdbc.core.fixture.TestDataSource)2 NamedDataSource (io.shardingjdbc.core.jdbc.core.datasource.NamedDataSource)2 Connection (java.sql.Connection)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 BasicDataSource (org.apache.commons.dbcp2.BasicDataSource)2 Test (org.junit.Test)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 MasterSlaveRuleConfiguration (io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration)1 ShardingRuleConfiguration (io.shardingjdbc.core.api.config.ShardingRuleConfiguration)1 TableRuleConfiguration (io.shardingjdbc.core.api.config.TableRuleConfiguration)1 ShardingContext (io.shardingjdbc.core.jdbc.core.ShardingContext)1 ShardingRule (io.shardingjdbc.core.rule.ShardingRule)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 Before (org.junit.Before)1 BeforeClass (org.junit.BeforeClass)1