Search in sources :

Example 1 with ShardingProperties

use of io.shardingjdbc.core.constant.ShardingProperties 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 2 with ShardingProperties

use of io.shardingjdbc.core.constant.ShardingProperties in project sharding-jdbc by shardingjdbc.

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

Example 3 with ShardingProperties

use of io.shardingjdbc.core.constant.ShardingProperties in project sharding-jdbc by shardingjdbc.

the class ShardingDataSource method renew.

/**
 * Renew sharding data source.
 *
 * @param newDataSourceMap new data source map
 * @param newShardingRule new sharding rule
 * @param newProps new sharding properties
 */
public void renew(final Map<String, DataSource> newDataSourceMap, final ShardingRule newShardingRule, final Properties newProps) {
    ShardingProperties newShardingProperties = new ShardingProperties(null == newProps ? new Properties() : newProps);
    int originalExecutorSize = shardingProperties.getValue(ShardingPropertiesConstant.EXECUTOR_SIZE);
    int newExecutorSize = newShardingProperties.getValue(ShardingPropertiesConstant.EXECUTOR_SIZE);
    if (originalExecutorSize != newExecutorSize) {
        ExecutorEngine originalExecutorEngine = executorEngine;
        executorEngine = new ExecutorEngine(newExecutorSize);
        originalExecutorEngine.close();
    }
    boolean newShowSQL = newShardingProperties.getValue(ShardingPropertiesConstant.SQL_SHOW);
    shardingProperties = newShardingProperties;
    shardingContext = new ShardingContext(newDataSourceMap, newShardingRule, getDatabaseType(), executorEngine, newShowSQL);
}
Also used : ShardingProperties(io.shardingjdbc.core.constant.ShardingProperties) ExecutorEngine(io.shardingjdbc.core.executor.ExecutorEngine) Properties(java.util.Properties) ShardingProperties(io.shardingjdbc.core.constant.ShardingProperties) ShardingContext(io.shardingjdbc.core.jdbc.core.ShardingContext)

Example 4 with ShardingProperties

use of io.shardingjdbc.core.constant.ShardingProperties in project sharding-jdbc by shardingjdbc.

the class YamlShardingIntegrateTest method assertWithDataSource.

@Test
public void assertWithDataSource() throws SQLException, URISyntaxException, IOException, ReflectiveOperationException {
    File yamlFile = new File(YamlShardingIntegrateTest.class.getResource(filePath).toURI());
    DataSource dataSource;
    if (hasDataSource) {
        dataSource = ShardingDataSourceFactory.createDataSource(yamlFile);
    } else {
        dataSource = ShardingDataSourceFactory.createDataSource(Maps.asMap(Sets.newHashSet("db0", "db1"), new Function<String, DataSource>() {

            @Override
            public DataSource apply(final String key) {
                return createDataSource(key);
            }
        }), yamlFile);
    }
    if (filePath.contains("WithProps.yaml")) {
        Field field = dataSource.getClass().getDeclaredField("shardingProperties");
        if (!field.isAccessible()) {
            field.setAccessible(true);
        }
        ShardingProperties shardingProperties = (ShardingProperties) field.get(dataSource);
        assertTrue((Boolean) shardingProperties.getValue(ShardingPropertiesConstant.SQL_SHOW));
    }
    Map<String, Object> configMap = new ConcurrentHashMap<>();
    configMap.put("key1", "value1");
    assertThat(ConfigMapContext.getInstance().getShardingConfig(), is(configMap));
    try (Connection conn = dataSource.getConnection();
        Statement stm = conn.createStatement()) {
        stm.execute(String.format("INSERT INTO t_order(user_id,status) values(%d, %s)", 10, "'insert'"));
        stm.executeQuery("SELECT o.*, i.* FROM T_order o JOIN T_order_item i ON o.order_id = i.order_id");
        stm.executeQuery("SELECT * FROM config");
    }
}
Also used : Field(java.lang.reflect.Field) ShardingProperties(io.shardingjdbc.core.constant.ShardingProperties) Statement(java.sql.Statement) Connection(java.sql.Connection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) File(java.io.File) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 5 with ShardingProperties

use of io.shardingjdbc.core.constant.ShardingProperties 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

ShardingProperties (io.shardingjdbc.core.constant.ShardingProperties)6 Test (org.junit.Test)5 ShardingDataSource (io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource)4 ShardingContext (io.shardingjdbc.core.jdbc.core.ShardingContext)3 Field (java.lang.reflect.Field)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 DataSource (javax.sql.DataSource)3 HashMap (java.util.HashMap)2 BasicDataSource (org.apache.commons.dbcp2.BasicDataSource)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 ExecutorEngine (io.shardingjdbc.core.executor.ExecutorEngine)1 SpringShardingDataSource (io.shardingjdbc.spring.datasource.SpringShardingDataSource)1 File (java.io.File)1 Connection (java.sql.Connection)1 Statement (java.sql.Statement)1 Properties (java.util.Properties)1