Search in sources :

Example 11 with StatementExecutorWrapper

use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.

the class ShardingStatement method generateExecutor.

private StatementExecutor generateExecutor(final String sql) throws SQLException {
    StatementExecutor result = new StatementExecutor(shardingConnection.getShardingContext().getExecutorEngine());
    SQLRouteResult sqlRouteResult = shardingConnection.getShardingContext().getSqlRouteEngine().route(sql);
    generatedKeyContext = sqlRouteResult.getGeneratedKeyContext();
    mergeContext = sqlRouteResult.getMergeContext();
    for (SQLExecutionUnit each : sqlRouteResult.getExecutionUnits()) {
        Statement statement = getStatement(shardingConnection.getConnection(each.getDataSource(), sqlRouteResult.getSqlStatementType()), each.getSql());
        replayMethodsInvocation(statement);
        result.addStatement(new StatementExecutorWrapper(statement, each));
    }
    return result;
}
Also used : SQLRouteResult(com.dangdang.ddframe.rdb.sharding.router.SQLRouteResult) Statement(java.sql.Statement) SQLExecutionUnit(com.dangdang.ddframe.rdb.sharding.router.SQLExecutionUnit) StatementExecutor(com.dangdang.ddframe.rdb.sharding.executor.StatementExecutor) StatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper)

Example 12 with StatementExecutorWrapper

use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.

the class StatementExecutor method execute.

private boolean execute(final Executor executor) {
    Context context = MetricsContext.start("ShardingStatement-execute");
    eventPostman.postExecutionEvents();
    final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown();
    final Map<String, Object> dataMap = ExecutorDataMap.getDataMap();
    try {
        if (1 == statementExecutorWrappers.size()) {
            return executeInternal(executor, statementExecutorWrappers.iterator().next(), isExceptionThrown, dataMap);
        }
        List<Boolean> result = executorEngine.execute(statementExecutorWrappers, new ExecuteUnit<StatementExecutorWrapper, Boolean>() {

            @Override
            public Boolean execute(final StatementExecutorWrapper input) throws Exception {
                synchronized (input.getStatement().getConnection()) {
                    return executeInternal(executor, input, isExceptionThrown, dataMap);
                }
            }
        });
        return (null == result || result.isEmpty()) ? false : result.get(0);
    } finally {
        MetricsContext.stop(context);
    }
}
Also used : MetricsContext(com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext) Context(com.codahale.metrics.Timer.Context) SQLException(java.sql.SQLException) StatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper)

Example 13 with StatementExecutorWrapper

use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.

the class StatementExecutor method executeQuery.

/**
     * 执行SQL查询.
     * 
     * @return 结果集列表
     */
public List<ResultSet> executeQuery() {
    Context context = MetricsContext.start("ShardingStatement-executeQuery");
    eventPostman.postExecutionEvents();
    final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown();
    final Map<String, Object> dataMap = ExecutorDataMap.getDataMap();
    List<ResultSet> result;
    try {
        if (1 == statementExecutorWrappers.size()) {
            return Collections.singletonList(executeQueryInternal(statementExecutorWrappers.iterator().next(), isExceptionThrown, dataMap));
        }
        result = executorEngine.execute(statementExecutorWrappers, new ExecuteUnit<StatementExecutorWrapper, ResultSet>() {

            @Override
            public ResultSet execute(final StatementExecutorWrapper input) throws Exception {
                synchronized (input.getStatement().getConnection()) {
                    return executeQueryInternal(input, isExceptionThrown, dataMap);
                }
            }
        });
    } finally {
        MetricsContext.stop(context);
    }
    return result;
}
Also used : MetricsContext(com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext) Context(com.codahale.metrics.Timer.Context) ResultSet(java.sql.ResultSet) StatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper)

Example 14 with StatementExecutorWrapper

use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.

the class StatementExecutorTest method assertExecuteForMultipleStatementsFailureWithDML.

@Test
public void assertExecuteForMultipleStatementsFailureWithDML() throws SQLException {
    Statement statement1 = mock(Statement.class);
    Statement statement2 = mock(Statement.class);
    SQLException exp = new SQLException();
    when(statement1.execute(DELETE_FROM_DUAL)).thenThrow(exp);
    when(statement2.execute(DELETE_FROM_DUAL)).thenThrow(exp);
    when(statement1.getConnection()).thenReturn(mock(Connection.class));
    when(statement2.getConnection()).thenReturn(mock(Connection.class));
    StatementExecutor actual = new StatementExecutor(executorEngine);
    StatementExecutorWrapper wrapper1 = createStatementExecutorWrapperForDML(statement1, "ds_0");
    actual.addStatement(wrapper1);
    StatementExecutorWrapper wrapper2 = createStatementExecutorWrapperForDML(statement2, "ds_1");
    actual.addStatement(wrapper2);
    assertFalse(actual.execute());
    verify(statement1).execute(DELETE_FROM_DUAL);
    verify(statement2).execute(DELETE_FROM_DUAL);
    verify(statement1).getConnection();
    verify(statement2).getConnection();
    verify(eventCaller, times(2)).verifyDataSource("ds_0");
    verify(eventCaller, times(2)).verifyDataSource("ds_1");
    verify(eventCaller, times(4)).verifySQL(DELETE_FROM_DUAL);
    verify(eventCaller, times(4)).verifyParameters(Collections.emptyList());
    verify(eventCaller, times(2)).verifyEventExecutionType(EventExecutionType.BEFORE_EXECUTE);
    verify(eventCaller, times(2)).verifyEventExecutionType(EventExecutionType.EXECUTE_FAILURE);
    verify(eventCaller, times(2)).verifyException(exp);
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) StatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper) Test(org.junit.Test)

Example 15 with StatementExecutorWrapper

use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.

the class StatementExecutorTest method assertExecuteForSingleStatementWithDQL.

@Test
public void assertExecuteForSingleStatementWithDQL() throws SQLException {
    Statement statement = mock(Statement.class);
    StatementExecutorWrapper wrapper = createStatementExecutorWrapperForDQL(statement, "ds_0");
    when(statement.execute(SELECT_FROM_DUAL)).thenReturn(true);
    StatementExecutor actual = new StatementExecutor(executorEngine);
    actual.addStatement(wrapper);
    assertTrue(actual.execute());
    verify(statement).execute(SELECT_FROM_DUAL);
    verify(eventCaller, times(2)).verifyDataSource("ds_0");
    verify(eventCaller, times(2)).verifySQL(SELECT_FROM_DUAL);
    verify(eventCaller, times(2)).verifyParameters(Collections.emptyList());
    verify(eventCaller).verifyEventExecutionType(EventExecutionType.BEFORE_EXECUTE);
    verify(eventCaller).verifyEventExecutionType(EventExecutionType.EXECUTE_SUCCESS);
    verify(eventCaller, times(0)).verifyException(null);
}
Also used : Statement(java.sql.Statement) StatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper) Test(org.junit.Test)

Aggregations

StatementExecutorWrapper (com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper)24 Statement (java.sql.Statement)21 Test (org.junit.Test)20 SQLException (java.sql.SQLException)8 Connection (java.sql.Connection)7 ResultSet (java.sql.ResultSet)4 Context (com.codahale.metrics.Timer.Context)3 MetricsContext (com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext)3 StatementExecutor (com.dangdang.ddframe.rdb.sharding.executor.StatementExecutor)1 SQLExecutionUnit (com.dangdang.ddframe.rdb.sharding.router.SQLExecutionUnit)1 SQLRouteResult (com.dangdang.ddframe.rdb.sharding.router.SQLRouteResult)1