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"));
}
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));
}
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);
}
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");
}
}
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"));
}
Aggregations