@Override public int[] doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException { OraclePreparedStatement ops = ps.unwrap(OraclePreparedStatement.class); int batchSize = this.pss.getBatchSize(); // Don't use an int[] array here because instances of InterruptibleBatchPreparedStatementSetter // might return Integer.MAX_VALUE as batch size. List<Integer> rowCounts = new ArrayList<>(); if (this.pss instanceof InterruptibleBatchPreparedStatementSetter) { InterruptibleBatchPreparedStatementSetter ipss = (InterruptibleBatchPreparedStatementSetter) this.pss; executeUpdate(ops, ipss, rowCounts); } else { int sizeOfCompleteBatches = (batchSize / this.sendBatchSize) * this.sendBatchSize; int sizeOfLastBatch = batchSize % this.sendBatchSize; executeUpdate(ops, rowCounts, 0, sizeOfCompleteBatches); executeUpdate(ops, rowCounts, sizeOfCompleteBatches, sizeOfCompleteBatches + sizeOfLastBatch); } return toIntArray(rowCounts); }
private void executeUpdate(OraclePreparedStatement ops, InterruptibleBatchPreparedStatementSetter ipss, List<Integer> rowCounts) throws SQLException { ops.setExecuteBatch(this.sendBatchSize); int i = 0; while (i < ipss.getBatchSize()) { ipss.setValues(ops, i); if (ipss.isBatchExhausted(i)) { break; } rowCounts.add(ops.executeUpdate()); i++; } if (i > 0 && i % this.sendBatchSize != 0) { rowCounts.set(rowCounts.size() - 1, ops.sendBatch()); } }
private void doInPreparedStatementWithIpss(int sendBatchSize, final int effectiveBatchSize, int pssBatchSize) throws SQLException { InterruptibleBatchPreparedStatementSetter ipss = mock(InterruptibleBatchPreparedStatementSetter.class); when(ipss.getBatchSize()).thenReturn(pssBatchSize); when(ipss.isBatchExhausted(anyInt())).thenAnswer(new Answer<Boolean>() { @Override public Boolean answer(InvocationOnMock invocation) throws Throwable { return effectiveBatchSize <= (int) invocation.getArguments()[0]; } }); BatchingPreparedStatementCallback psc = new BatchingPreparedStatementCallback(sendBatchSize, ipss); int[] result = psc.doInPreparedStatement(this.ops); int usedBatchSize = effectiveBatchSize < pssBatchSize ? effectiveBatchSize : pssBatchSize; assertThat(result, matchesRowCounts(sendBatchSize, usedBatchSize)); verifyPreparedStatementCalls(usedBatchSize, ipss); }