Search in sources :

Example 1 with ShardingDataSource

use of io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource in project sharding-jdbc by shardingjdbc.

the class AbstractShardingJDBCDatabaseAndTableTest method initShardingDataSources.

@Before
public void initShardingDataSources() throws SQLException {
    if (!getShardingDataSources().isEmpty()) {
        return;
    }
    Map<DatabaseType, Map<String, DataSource>> dataSourceMap = createDataSourceMap();
    for (Map.Entry<DatabaseType, Map<String, DataSource>> entry : dataSourceMap.entrySet()) {
        final ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration();
        orderTableRuleConfig.setLogicTable("t_order");
        List<String> orderActualDataNodes = new LinkedList<>();
        for (String dataSourceName : entry.getValue().keySet()) {
            orderActualDataNodes.add(dataSourceName + ".t_order_${0..1}");
        }
        orderTableRuleConfig.setActualDataNodes(Joiner.on(",").join(orderActualDataNodes));
        shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);
        TableRuleConfiguration orderItemTableRuleConfig = new TableRuleConfiguration();
        orderItemTableRuleConfig.setLogicTable("t_order_item");
        List<String> orderItemActualDataNodes = new LinkedList<>();
        for (String dataSourceName : entry.getValue().keySet()) {
            orderItemActualDataNodes.add(dataSourceName + ".t_order_item_${0..1}");
        }
        orderItemTableRuleConfig.setActualDataNodes(Joiner.on(",").join(orderItemActualDataNodes));
        orderItemTableRuleConfig.setKeyGeneratorColumnName("item_id");
        orderItemTableRuleConfig.setKeyGenerator(new IncrementKeyGenerator());
        shardingRuleConfig.getTableRuleConfigs().add(orderItemTableRuleConfig);
        TableRuleConfiguration configTableRuleConfig = new TableRuleConfiguration();
        configTableRuleConfig.setLogicTable("t_config");
        shardingRuleConfig.getTableRuleConfigs().add(configTableRuleConfig);
        shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
        shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", new PreciseOrderShardingAlgorithm(), new RangeOrderShardingAlgorithm()));
        shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new StandardShardingStrategyConfiguration("user_id", new PreciseOrderShardingAlgorithm(), new RangeOrderShardingAlgorithm()));
        ShardingRule shardingRule = new ShardingRule(shardingRuleConfig, entry.getValue().keySet());
        getShardingDataSources().put(entry.getKey(), new ShardingDataSource(entry.getValue(), shardingRule));
    }
}
Also used : DatabaseType(io.shardingjdbc.core.constant.DatabaseType) ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) IncrementKeyGenerator(io.shardingjdbc.core.fixture.IncrementKeyGenerator) LinkedList(java.util.LinkedList) StandardShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration) PreciseOrderShardingAlgorithm(io.shardingjdbc.core.fixture.PreciseOrderShardingAlgorithm) ShardingRuleConfiguration(io.shardingjdbc.core.api.config.ShardingRuleConfiguration) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) Map(java.util.Map) TableRuleConfiguration(io.shardingjdbc.core.api.config.TableRuleConfiguration) RangeOrderShardingAlgorithm(io.shardingjdbc.core.fixture.RangeOrderShardingAlgorithm) Before(org.junit.Before)

Example 2 with ShardingDataSource

use of io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource in project sharding-jdbc by shardingjdbc.

the class OrchestrationShardingMasterSlaveNamespaceTest method getShardingRule.

private ShardingRule getShardingRule() {
    ShardingDataSource shardingDataSource = this.applicationContext.getBean("defaultShardingDataSource", ShardingDataSource.class);
    Object shardingContext = FieldValueUtil.getFieldValue(shardingDataSource, "shardingContext", true);
    return (ShardingRule) FieldValueUtil.getFieldValue(shardingContext, "shardingRule");
}
Also used : ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) ShardingRule(io.shardingjdbc.core.rule.ShardingRule)

Example 3 with ShardingDataSource

use of io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource in project sharding-jdbc by shardingjdbc.

the class OrchestrationShardingNamespaceTest method getDataSourceMap.

@SuppressWarnings("unchecked")
private Map<String, DataSource> getDataSourceMap(final String shardingDataSourceName) {
    ShardingDataSource shardingDataSource = this.applicationContext.getBean(shardingDataSourceName, ShardingDataSource.class);
    Object shardingContext = FieldValueUtil.getFieldValue(shardingDataSource, "shardingContext", true);
    return (Map) FieldValueUtil.getFieldValue(shardingContext, "dataSourceMap");
}
Also used : ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with ShardingDataSource

use of io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource in project sharding-jdbc by shardingjdbc.

the class OrchestrationShardingNamespaceTest method assertPropsDataSource.

@Test
public void assertPropsDataSource() {
    ShardingDataSource shardingDataSource = this.applicationContext.getBean("propsDataSource", ShardingDataSource.class);
    Map<String, Object> configMap = new HashMap<>();
    configMap.put("key1", "value1");
    assertThat(ConfigMapContext.getInstance().getShardingConfig(), is(configMap));
    Object shardingContext = FieldValueUtil.getFieldValue(shardingDataSource, "shardingContext", true);
    assertTrue((boolean) FieldValueUtil.getFieldValue(shardingContext, "showSQL"));
    ShardingProperties shardingProperties = (ShardingProperties) FieldValueUtil.getFieldValue(shardingDataSource, "shardingProperties", true);
    boolean showSql = shardingProperties.getValue(ShardingPropertiesConstant.SQL_SHOW);
    assertTrue(showSql);
    int executorSize = shardingProperties.getValue(ShardingPropertiesConstant.EXECUTOR_SIZE);
    assertThat(executorSize, is(10));
    assertNull(ShardingPropertiesConstant.findByKey("foo"));
}
Also used : ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) HashMap(java.util.HashMap) ShardingProperties(io.shardingjdbc.core.constant.ShardingProperties) Test(org.junit.Test)

Example 5 with ShardingDataSource

use of io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource in project sharding-jdbc by shardingjdbc.

the class OrchestrationShardingNamespaceTest method getShardingRule.

private ShardingRule getShardingRule(final String shardingDataSourceName) {
    ShardingDataSource shardingDataSource = this.applicationContext.getBean(shardingDataSourceName, ShardingDataSource.class);
    Object shardingContext = FieldValueUtil.getFieldValue(shardingDataSource, "shardingContext", true);
    return (ShardingRule) FieldValueUtil.getFieldValue(shardingContext, "shardingRule");
}
Also used : ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) ShardingRule(io.shardingjdbc.core.rule.ShardingRule)

Aggregations

ShardingDataSource (io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource)32 Map (java.util.Map)20 DatabaseType (io.shardingjdbc.core.constant.DatabaseType)16 Test (org.junit.Test)15 ShardingRule (io.shardingjdbc.core.rule.ShardingRule)14 HintShardingValueHelper (io.shardingjdbc.core.integrate.type.sharding.hint.helper.HintShardingValueHelper)8 ShardingRuleConfiguration (io.shardingjdbc.core.api.config.ShardingRuleConfiguration)7 TableRuleConfiguration (io.shardingjdbc.core.api.config.TableRuleConfiguration)7 AbstractShardingDatabaseOnlyWithHintTest (io.shardingjdbc.core.integrate.type.sharding.hint.base.AbstractShardingDatabaseOnlyWithHintTest)6 HashMap (java.util.HashMap)6 SQLShardingRule (io.shardingjdbc.core.integrate.jaxb.SQLShardingRule)5 ShardingConnection (io.shardingjdbc.core.jdbc.core.connection.ShardingConnection)5 Connection (java.sql.Connection)5 PreparedStatement (java.sql.PreparedStatement)5 StandardShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration)4 ShardingProperties (io.shardingjdbc.core.constant.ShardingProperties)4 SpringShardingDataSource (io.shardingjdbc.spring.datasource.SpringShardingDataSource)4 LinkedList (java.util.LinkedList)4 DataSource (javax.sql.DataSource)4 AbstractRoutingDatabaseOnlyWithHintTest (io.shardingjdbc.core.integrate.type.sharding.hint.base.AbstractRoutingDatabaseOnlyWithHintTest)3