Search in sources :

Example 11 with PreparedStatementExecutorWrapper

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

the class ShardingPreparedStatement method wrap.

private PreparedStatementExecutorWrapper wrap(final PreparedStatement preparedStatement, final SQLExecutionUnit sqlExecutionUnit) {
    Optional<PreparedStatementExecutorWrapper> wrapperOptional = Iterators.tryFind(cachedPreparedStatementWrappers.iterator(), new Predicate<PreparedStatementExecutorWrapper>() {

        @Override
        public boolean apply(final PreparedStatementExecutorWrapper input) {
            return Objects.equals(input.getPreparedStatement(), preparedStatement);
        }
    });
    if (wrapperOptional.isPresent()) {
        wrapperOptional.get().addBatchParameters(getParameters());
        return wrapperOptional.get();
    }
    PreparedStatementExecutorWrapper result = new PreparedStatementExecutorWrapper(preparedStatement, getParameters(), sqlExecutionUnit);
    cachedPreparedStatementWrappers.add(result);
    return result;
}
Also used : PreparedStatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper)

Example 12 with PreparedStatementExecutorWrapper

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

the class PreparedStatementExecutor method executeQuery.

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

            @Override
            public ResultSet execute(final PreparedStatementExecutorWrapper input) throws Exception {
                synchronized (input.getPreparedStatement().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) PreparedStatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper) ResultSet(java.sql.ResultSet)

Example 13 with PreparedStatementExecutorWrapper

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

the class PreparedStatementExecutor method executeUpdate.

/**
     * 执行SQL更新.
     * 
     * @return 更新数量
     */
public int executeUpdate() {
    Context context = MetricsContext.start("ShardingPreparedStatement-executeUpdate");
    eventPostman.postExecutionEvents();
    final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown();
    final Map<String, Object> dataMap = ExecutorDataMap.getDataMap();
    try {
        if (1 == preparedStatementExecutorWrappers.size()) {
            return executeUpdateInternal(preparedStatementExecutorWrappers.iterator().next(), isExceptionThrown, dataMap);
        }
        return executorEngine.execute(preparedStatementExecutorWrappers, new ExecuteUnit<PreparedStatementExecutorWrapper, Integer>() {

            @Override
            public Integer execute(final PreparedStatementExecutorWrapper input) throws Exception {
                synchronized (input.getPreparedStatement().getConnection()) {
                    return executeUpdateInternal(input, isExceptionThrown, dataMap);
                }
            }
        }, new MergeUnit<Integer, Integer>() {

            @Override
            public Integer merge(final List<Integer> results) {
                if (null == results) {
                    return 0;
                }
                int result = 0;
                for (Integer each : results) {
                    result += each;
                }
                return result;
            }
        });
    } finally {
        MetricsContext.stop(context);
    }
}
Also used : MetricsContext(com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext) Context(com.codahale.metrics.Timer.Context) PreparedStatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper) SQLException(java.sql.SQLException)

Example 14 with PreparedStatementExecutorWrapper

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

the class PreparedStatementExecutor method executeBatch.

/**
     * 执行批量接口.
     *
     * @return 每个
     * @param batchSize 批量执行语句总数
     */
public int[] executeBatch(final int batchSize) {
    Context context = MetricsContext.start("ShardingPreparedStatement-executeUpdate");
    eventPostman.postExecutionEvents();
    final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown();
    final Map<String, Object> dataMap = ExecutorDataMap.getDataMap();
    try {
        if (1 == preparedStatementExecutorWrappers.size()) {
            return executeBatchInternal(preparedStatementExecutorWrappers.iterator().next(), isExceptionThrown, dataMap);
        }
        return executorEngine.execute(preparedStatementExecutorWrappers, new ExecuteUnit<PreparedStatementExecutorWrapper, int[]>() {

            @Override
            public int[] execute(final PreparedStatementExecutorWrapper input) throws Exception {
                synchronized (input.getPreparedStatement().getConnection()) {
                    return executeBatchInternal(input, isExceptionThrown, dataMap);
                }
            }
        }, new MergeUnit<int[], int[]>() {

            @Override
            public int[] merge(final List<int[]> results) {
                if (null == results) {
                    return new int[] { 0 };
                }
                int[] result = new int[batchSize];
                int i = 0;
                for (PreparedStatementExecutorWrapper each : preparedStatementExecutorWrappers) {
                    for (Integer[] indices : each.getBatchIndices()) {
                        result[indices[0]] += results.get(i)[indices[1]];
                    }
                    i++;
                }
                return result;
            }
        });
    } finally {
        MetricsContext.stop(context);
    }
}
Also used : MetricsContext(com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext) Context(com.codahale.metrics.Timer.Context) PreparedStatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper) SQLException(java.sql.SQLException)

Example 15 with PreparedStatementExecutorWrapper

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

the class PreparedStatementExecutorTest method assertExecuteQueryForMultiplePreparedStatementsFailure.

@Test
public void assertExecuteQueryForMultiplePreparedStatementsFailure() throws SQLException {
    PreparedStatement preparedStatement1 = mock(PreparedStatement.class);
    PreparedStatementExecutorWrapper wrapper1 = createPreparedStatementExecutorWrapperForDQL(preparedStatement1, "ds_0");
    PreparedStatement preparedStatement2 = mock(PreparedStatement.class);
    PreparedStatementExecutorWrapper wrapper2 = createPreparedStatementExecutorWrapperForDQL(preparedStatement2, "ds_1");
    SQLException exp = new SQLException();
    when(preparedStatement1.executeQuery()).thenThrow(exp);
    when(preparedStatement2.executeQuery()).thenThrow(exp);
    when(preparedStatement1.getConnection()).thenReturn(mock(Connection.class));
    when(preparedStatement2.getConnection()).thenReturn(mock(Connection.class));
    PreparedStatementExecutor actual = new PreparedStatementExecutor(executorEngine, Arrays.asList(wrapper1, wrapper2));
    List<ResultSet> actualResultSets = actual.executeQuery();
    assertThat(actualResultSets, is(Arrays.asList((ResultSet) null, null)));
    verify(preparedStatement1).executeQuery();
    verify(preparedStatement2).executeQuery();
    verify(preparedStatement1).getConnection();
    verify(preparedStatement2).getConnection();
    verify(eventCaller, times(2)).verifyDataSource("ds_0");
    verify(eventCaller, times(2)).verifyDataSource("ds_1");
    verify(eventCaller, times(4)).verifySQL("SELECT * 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) PreparedStatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Aggregations

PreparedStatementExecutorWrapper (com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper)21 PreparedStatement (java.sql.PreparedStatement)15 Test (org.junit.Test)14 SQLException (java.sql.SQLException)9 Connection (java.sql.Connection)7 Context (com.codahale.metrics.Timer.Context)4 MetricsContext (com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext)4 ResultSet (java.sql.ResultSet)4 MergeContext (com.dangdang.ddframe.rdb.sharding.parser.result.merger.MergeContext)1 SQLExecutionUnit (com.dangdang.ddframe.rdb.sharding.router.SQLExecutionUnit)1 SQLRouteResult (com.dangdang.ddframe.rdb.sharding.router.SQLRouteResult)1 ArrayList (java.util.ArrayList)1