Search in sources :

Example 6 with ShardingContext

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

the class ShardingStatement method generateExecutor.

private StatementExecutor generateExecutor(final String sql) throws SQLException {
    clearPrevious();
    ShardingContext shardingContext = connection.getShardingContext();
    routeResult = new StatementRoutingEngine(shardingContext.getShardingRule(), shardingContext.getDatabaseType(), shardingContext.isShowSQL()).route(sql);
    Collection<StatementUnit> statementUnits = new LinkedList<>();
    for (SQLExecutionUnit each : routeResult.getExecutionUnits()) {
        Collection<Connection> connections;
        SQLType sqlType = routeResult.getSqlStatement().getType();
        if (SQLType.DDL == sqlType) {
            connections = connection.getConnectionsForDDL(each.getDataSource());
        } else {
            connections = Collections.singletonList(connection.getConnection(each.getDataSource(), routeResult.getSqlStatement().getType()));
        }
        for (Connection connection : connections) {
            Statement statement = connection.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
            replayMethodsInvocation(statement);
            statementUnits.add(new StatementUnit(each, statement));
            routedStatements.add(statement);
        }
    }
    return new StatementExecutor(connection.getShardingContext().getExecutorEngine(), routeResult.getSqlStatement().getType(), statementUnits);
}
Also used : DQLStatement(io.shardingjdbc.core.parsing.parser.sql.dql.DQLStatement) SelectStatement(io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement) InsertStatement(io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement) DALStatement(io.shardingjdbc.core.parsing.parser.sql.dal.DALStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ShardingConnection(io.shardingjdbc.core.jdbc.core.connection.ShardingConnection) StatementRoutingEngine(io.shardingjdbc.core.routing.StatementRoutingEngine) SQLExecutionUnit(io.shardingjdbc.core.routing.SQLExecutionUnit) SQLType(io.shardingjdbc.core.constant.SQLType) StatementExecutor(io.shardingjdbc.core.executor.type.statement.StatementExecutor) StatementUnit(io.shardingjdbc.core.executor.type.statement.StatementUnit) LinkedList(java.util.LinkedList) ShardingContext(io.shardingjdbc.core.jdbc.core.ShardingContext)

Example 7 with ShardingContext

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

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