BatchPreparedStatementExecutor { public int[] executeBatch(final SQLStatementContext sqlStatementContext) throws SQLException { boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); SQLExecutorCallback<int[]> callback = new DefaultSQLExecutorCallback<int[]>(schemaContexts.getDatabaseType(), isExceptionThrown) { @Override protected int[] executeSQL(final String sql, final Statement statement, final ConnectionMode connectionMode) throws SQLException { return statement.executeBatch(); } }; List<int[]> results = sqlExecutor.execute(inputGroups, callback); return isNeedAccumulate( schemaContexts.getDefaultSchema().getRules().stream().filter(rule -> rule instanceof DataNodeRoutedRule).collect(Collectors.toList()), sqlStatementContext) ? accumulate(results) : results.get(0); } BatchPreparedStatementExecutor(final SchemaContexts schemaContexts, final SQLExecutor sqlExecutor); void init(final Collection<InputGroup<StatementExecuteUnit>> inputGroups); void addBatchForExecutionUnits(final Collection<ExecutionUnit> executionUnits); int[] executeBatch(final SQLStatementContext sqlStatementContext); List<Statement> getStatements(); List<List<Object>> getParameterSet(final Statement statement); void clear();  }
@Test public void assertNoPreparedStatement() throws SQLException { PreparedStatement preparedStatement = getPreparedStatement(); when(preparedStatement.executeBatch()).thenReturn(new int[] {0, 0}); setExecuteGroups(Collections.singletonList(preparedStatement)); assertThat(actual.executeBatch(sqlStatementContext), is(new int[] {0, 0})); }
@Test public void assertExecuteBatchForSinglePreparedStatementSuccess() throws SQLException { PreparedStatement preparedStatement = getPreparedStatement(); when(preparedStatement.executeBatch()).thenReturn(new int[] {10, 20}); setExecuteGroups(Collections.singletonList(preparedStatement)); assertThat(actual.executeBatch(sqlStatementContext), is(new int[] {10, 20})); verify(preparedStatement).executeBatch(); }
@Test public void assertExecuteBatchForMultiplePreparedStatementsSuccess() throws SQLException { PreparedStatement preparedStatement1 = getPreparedStatement(); PreparedStatement preparedStatement2 = getPreparedStatement(); when(preparedStatement1.executeBatch()).thenReturn(new int[] {10, 20}); when(preparedStatement2.executeBatch()).thenReturn(new int[] {20, 40}); setExecuteGroups(Arrays.asList(preparedStatement1, preparedStatement2)); assertThat(actual.executeBatch(sqlStatementContext), is(new int[] {30, 60})); verify(preparedStatement1).executeBatch(); verify(preparedStatement2).executeBatch(); }
@Test public void assertExecuteBatchForSinglePreparedStatementFailure() throws SQLException { PreparedStatement preparedStatement = getPreparedStatement(); SQLException ex = new SQLException(""); when(preparedStatement.executeBatch()).thenThrow(ex); setExecuteGroups(Collections.singletonList(preparedStatement)); assertThat(actual.executeBatch(sqlStatementContext), is(new int[] {0, 0})); verify(preparedStatement).executeBatch(); }
@Test public void assertExecuteBatchForMultiplePreparedStatementsFailure() throws SQLException { PreparedStatement preparedStatement1 = getPreparedStatement(); PreparedStatement preparedStatement2 = getPreparedStatement(); SQLException ex = new SQLException(""); when(preparedStatement1.executeBatch()).thenThrow(ex); when(preparedStatement2.executeBatch()).thenThrow(ex); setExecuteGroups(Arrays.asList(preparedStatement1, preparedStatement2)); assertThat(actual.executeBatch(sqlStatementContext), is(new int[] {0, 0})); verify(preparedStatement1).executeBatch(); verify(preparedStatement2).executeBatch(); }