Search in sources :

Example 26 with ShardingDataSource

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

the class MasterSlaveNamespaceTest 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)

Example 27 with ShardingDataSource

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

the class MasterSlaveNamespaceTest 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 28 with ShardingDataSource

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

the class ShardingNamespaceTest method assertDefaultActualDataNodes.

@Test
public void assertDefaultActualDataNodes() {
    ShardingDataSource multiTableRulesDataSource = this.applicationContext.getBean("multiTableRulesDataSource", ShardingDataSource.class);
    Object shardingContext = FieldValueUtil.getFieldValue(multiTableRulesDataSource, "shardingContext", true);
    ShardingRule shardingRule = (ShardingRule) FieldValueUtil.getFieldValue(shardingContext, "shardingRule");
    assertThat(shardingRule.getTableRules().size(), is(2));
    Iterator<TableRule> tableRules = shardingRule.getTableRules().iterator();
    TableRule orderRule = tableRules.next();
    assertThat(orderRule.getActualDataNodes().size(), is(2));
    assertTrue(orderRule.getActualDataNodes().contains(new DataNode("dbtbl_0", "t_order")));
    assertTrue(orderRule.getActualDataNodes().contains(new DataNode("dbtbl_1", "t_order")));
    TableRule orderItemRule = tableRules.next();
    assertThat(orderItemRule.getActualDataNodes().size(), is(2));
    assertTrue(orderItemRule.getActualDataNodes().contains(new DataNode("dbtbl_0", "t_order_item")));
    assertTrue(orderItemRule.getActualDataNodes().contains(new DataNode("dbtbl_1", "t_order_item")));
}
Also used : BindingTableRule(io.shardingjdbc.core.rule.BindingTableRule) TableRule(io.shardingjdbc.core.rule.TableRule) ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) SpringShardingDataSource(io.shardingjdbc.spring.datasource.SpringShardingDataSource) DataNode(io.shardingjdbc.core.rule.DataNode) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) Test(org.junit.Test)

Example 29 with ShardingDataSource

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

the class ShardingNamespaceTest 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) SpringShardingDataSource(io.shardingjdbc.spring.datasource.SpringShardingDataSource) HashMap(java.util.HashMap) Map(java.util.Map)

Example 30 with ShardingDataSource

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

the class ShardingNamespaceTest 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) SpringShardingDataSource(io.shardingjdbc.spring.datasource.SpringShardingDataSource) HashMap(java.util.HashMap) ShardingProperties(io.shardingjdbc.core.constant.ShardingProperties) Test(org.junit.Test)

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