Search in sources :

Example 31 with ShardingDataSource

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

the class ShardingNamespaceTest 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) SpringShardingDataSource(io.shardingjdbc.spring.datasource.SpringShardingDataSource) ShardingRule(io.shardingjdbc.core.rule.ShardingRule)

Example 32 with ShardingDataSource

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

the class SpringBootShardingTest method assertWithShardingDataSource.

@Test
public void assertWithShardingDataSource() throws NoSuchFieldException, IllegalAccessException {
    assertTrue(dataSource instanceof ShardingDataSource);
    Field field = ShardingDataSource.class.getDeclaredField("shardingContext");
    field.setAccessible(true);
    ShardingContext shardingContext = (ShardingContext) field.get(dataSource);
    for (DataSource each : shardingContext.getDataSourceMap().values()) {
        assertThat(((BasicDataSource) each).getMaxTotal(), is(16));
    }
    assertTrue(shardingContext.isShowSQL());
    Map<String, Object> configMap = new ConcurrentHashMap<>();
    configMap.put("key1", "value1");
    assertThat(ConfigMapContext.getInstance().getShardingConfig(), is(configMap));
    Field propertiesField = ShardingDataSource.class.getDeclaredField("shardingProperties");
    propertiesField.setAccessible(true);
    ShardingProperties shardingProperties = (ShardingProperties) propertiesField.get(dataSource);
    assertTrue((Boolean) shardingProperties.getValue(ShardingPropertiesConstant.SQL_SHOW));
    assertThat((Integer) shardingProperties.getValue(ShardingPropertiesConstant.EXECUTOR_SIZE), is(100));
}
Also used : Field(java.lang.reflect.Field) ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) ShardingProperties(io.shardingjdbc.core.constant.ShardingProperties) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ShardingContext(io.shardingjdbc.core.jdbc.core.ShardingContext) ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource) DataSource(javax.sql.DataSource) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

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