Search in sources :

Example 6 with PreparedStatementExecutorWrapper

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

the class PreparedStatementExecutorTest method assertExecuteForMultiplePreparedStatementsSuccessWithDML.

@Test
public void assertExecuteForMultiplePreparedStatementsSuccessWithDML() throws SQLException {
    PreparedStatement preparedStatement1 = mock(PreparedStatement.class);
    PreparedStatementExecutorWrapper wrapper1 = createPreparedStatementExecutorWrapperForDML(preparedStatement1, "ds_0");
    PreparedStatement preparedStatement2 = mock(PreparedStatement.class);
    PreparedStatementExecutorWrapper wrapper2 = createPreparedStatementExecutorWrapperForDML(preparedStatement2, "ds_1");
    when(preparedStatement1.execute()).thenReturn(false);
    when(preparedStatement2.execute()).thenReturn(false);
    when(preparedStatement1.getConnection()).thenReturn(mock(Connection.class));
    when(preparedStatement2.getConnection()).thenReturn(mock(Connection.class));
    PreparedStatementExecutor actual = new PreparedStatementExecutor(executorEngine, Arrays.asList(wrapper1, wrapper2));
    assertFalse(actual.execute());
    verify(preparedStatement1).execute();
    verify(preparedStatement2).execute();
    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("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_SUCCESS);
    verify(eventCaller, times(0)).verifyException(null);
}
Also used : PreparedStatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 7 with PreparedStatementExecutorWrapper

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

the class PreparedStatementExecutorTest method assertExecuteQueryForSinglePreparedStatementFailure.

@Test
public void assertExecuteQueryForSinglePreparedStatementFailure() throws SQLException {
    PreparedStatement preparedStatement = mock(PreparedStatement.class);
    PreparedStatementExecutorWrapper wrapper = createPreparedStatementExecutorWrapperForDQL(preparedStatement, "ds_0");
    SQLException exp = new SQLException();
    when(preparedStatement.executeQuery()).thenThrow(exp);
    PreparedStatementExecutor actual = new PreparedStatementExecutor(executorEngine, Collections.singleton(wrapper));
    assertThat(actual.executeQuery(), is(Collections.singletonList((ResultSet) null)));
    verify(preparedStatement).executeQuery();
    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_FAILURE);
    verify(eventCaller).verifyException(exp);
}
Also used : SQLException(java.sql.SQLException) PreparedStatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test)

Example 8 with PreparedStatementExecutorWrapper

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

the class ShardingPreparedStatement method routeSQL.

private List<PreparedStatementExecutorWrapper> routeSQL() throws SQLException {
    List<PreparedStatementExecutorWrapper> result = new ArrayList<>();
    SQLRouteResult sqlRouteResult = preparedSQLRouter.route(getParameters());
    MergeContext mergeContext = sqlRouteResult.getMergeContext();
    setMergeContext(mergeContext);
    setGeneratedKeyContext(sqlRouteResult.getGeneratedKeyContext());
    for (SQLExecutionUnit each : sqlRouteResult.getExecutionUnits()) {
        PreparedStatement preparedStatement = (PreparedStatement) getStatement(getShardingConnection().getConnection(each.getDataSource(), sqlRouteResult.getSqlStatementType()), each.getSql());
        replayMethodsInvocation(preparedStatement);
        getParameters().replayMethodsInvocation(preparedStatement);
        result.add(wrap(preparedStatement, each));
    }
    return result;
}
Also used : SQLRouteResult(com.dangdang.ddframe.rdb.sharding.router.SQLRouteResult) PreparedStatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper) ArrayList(java.util.ArrayList) SQLExecutionUnit(com.dangdang.ddframe.rdb.sharding.router.SQLExecutionUnit) PreparedStatement(java.sql.PreparedStatement) MergeContext(com.dangdang.ddframe.rdb.sharding.parser.result.merger.MergeContext)

Example 9 with PreparedStatementExecutorWrapper

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

the class PreparedStatementExecutor method execute.

/**
     * 执行SQL请求.
     * 
     * @return true表示执行DQL, false表示执行的DML
     */
public boolean execute() {
    Context context = MetricsContext.start("ShardingPreparedStatement-execute");
    eventPostman.postExecutionEvents();
    final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown();
    final Map<String, Object> dataMap = ExecutorDataMap.getDataMap();
    try {
        if (1 == preparedStatementExecutorWrappers.size()) {
            PreparedStatementExecutorWrapper preparedStatementExecutorWrapper = preparedStatementExecutorWrappers.iterator().next();
            return executeInternal(preparedStatementExecutorWrapper, isExceptionThrown, dataMap);
        }
        List<Boolean> result = executorEngine.execute(preparedStatementExecutorWrappers, new ExecuteUnit<PreparedStatementExecutorWrapper, Boolean>() {

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

Example 10 with PreparedStatementExecutorWrapper

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

the class ShardingPreparedStatement method addBatch.

@Override
public void addBatch() throws SQLException {
    try {
        for (PreparedStatementExecutorWrapper each : routeSQL()) {
            each.getPreparedStatement().addBatch();
            each.mapBatchIndex(batchIndex);
        }
        batchIndex++;
        getGeneratedKeyContext().addRow();
    } finally {
        resetBatch();
    }
}
Also used : PreparedStatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper)

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