Search in sources :

Example 1 with ShardingContext

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

the class ShardingConnectionTest method setUp.

@Before
public void setUp() {
    ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
    TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration();
    tableRuleConfig.setLogicTable("test");
    shardingRuleConfig.getTableRuleConfigs().add(tableRuleConfig);
    Map<String, DataSource> dataSourceMap = new HashMap<>(1, 1);
    dataSourceMap.put(DS_NAME, masterSlaveDataSource);
    ShardingContext shardingContext = new ShardingContext(dataSourceMap, new ShardingRule(shardingRuleConfig, dataSourceMap.keySet()), null, null, false);
    connection = new ShardingConnection(shardingContext);
}
Also used : ShardingRuleConfiguration(io.shardingjdbc.core.api.config.ShardingRuleConfiguration) HashMap(java.util.HashMap) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) TableRuleConfiguration(io.shardingjdbc.core.api.config.TableRuleConfiguration) TestDataSource(io.shardingjdbc.core.fixture.TestDataSource) DataSource(javax.sql.DataSource) MasterSlaveDataSource(io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource) ShardingContext(io.shardingjdbc.core.jdbc.core.ShardingContext) Before(org.junit.Before)

Example 2 with ShardingContext

use of io.shardingjdbc.core.jdbc.core.ShardingContext 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 ShardingContext

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

the class ShardingDataSourceFactoryTest method getShardingRule.

private ShardingRule getShardingRule(final DataSource dataSource) throws NoSuchFieldException, IllegalAccessException {
    Field field = dataSource.getClass().getDeclaredField("shardingContext");
    field.setAccessible(true);
    return ((ShardingContext) field.get(dataSource)).getShardingRule();
}
Also used : Field(java.lang.reflect.Field) ShardingContext(io.shardingjdbc.core.jdbc.core.ShardingContext)

Example 4 with ShardingContext

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

the class AbstractSQLTest method getDataSourceMap.

private static Map<String, DataSource> getDataSourceMap(final ShardingDataSource shardingDataSource) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    Field field = shardingDataSource.getClass().getDeclaredField("shardingContext");
    field.setAccessible(true);
    ShardingContext shardingContext = (ShardingContext) field.get(shardingDataSource);
    return shardingContext.getDataSourceMap();
}
Also used : Field(java.lang.reflect.Field) ShardingContext(io.shardingjdbc.core.jdbc.core.ShardingContext)

Example 5 with ShardingContext

use of io.shardingjdbc.core.jdbc.core.ShardingContext 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)

Aggregations

ShardingContext (io.shardingjdbc.core.jdbc.core.ShardingContext)7 Field (java.lang.reflect.Field)4 ShardingProperties (io.shardingjdbc.core.constant.ShardingProperties)3 DataSource (javax.sql.DataSource)3 ShardingDataSource (io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 BasicDataSource (org.apache.commons.dbcp2.BasicDataSource)2 Test (org.junit.Test)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 ShardingRuleConfiguration (io.shardingjdbc.core.api.config.ShardingRuleConfiguration)1 TableRuleConfiguration (io.shardingjdbc.core.api.config.TableRuleConfiguration)1 SQLType (io.shardingjdbc.core.constant.SQLType)1 ExecutorEngine (io.shardingjdbc.core.executor.ExecutorEngine)1 StatementExecutor (io.shardingjdbc.core.executor.type.statement.StatementExecutor)1 StatementUnit (io.shardingjdbc.core.executor.type.statement.StatementUnit)1 TestDataSource (io.shardingjdbc.core.fixture.TestDataSource)1 ShardingConnection (io.shardingjdbc.core.jdbc.core.connection.ShardingConnection)1 MasterSlaveDataSource (io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource)1 DALStatement (io.shardingjdbc.core.parsing.parser.sql.dal.DALStatement)1 InsertStatement (io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement)1