Example usage for org.springframework.jdbc.core InterruptibleBatchPreparedStatementSetter isBatchExhausted

List of usage examples for org.springframework.jdbc.core InterruptibleBatchPreparedStatementSetter isBatchExhausted

Introduction

In this page you can find the example usage for org.springframework.jdbc.core InterruptibleBatchPreparedStatementSetter isBatchExhausted.

Prototype

boolean isBatchExhausted(int i);

Source Link

Document

Return whether the batch is complete, that is, whether there were no additional values added during the last setValues call.

Usage

From source file:com.github.ferstl.spring.jdbc.oracle.BatchingPreparedStatementCallbackTest.java

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/*from  ww  w.  j a v a 2  s . co  m*/
        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);
}

From source file:com.github.ferstl.spring.jdbc.oracle.BatchingPreparedStatementCallback.java

private void executeUpdate(OraclePreparedStatement ops, InterruptibleBatchPreparedStatementSetter ipss,
        List<Integer> rowCounts) throws SQLException {

    ops.setExecuteBatch(this.sendBatchSize);
    int i = 0;// w w w .  j  av a  2s.c o m
    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());
    }

}

From source file:cc.tooyoung.common.db.JdbcTemplate.java

@SuppressWarnings("unchecked")
public int[] batchUpdate(String sql, final BatchPreparedStatementSetter pss) throws DataAccessException {
    if (ApiLogger.isTraceEnabled()) {
        ApiLogger.trace("Executing SQL batch update [" + sql + "]");
    }//from w  w w .j  av  a 2  s  .  c om

    return (int[]) execute(sql, new PreparedStatementCallback() {
        public Object doInPreparedStatement(PreparedStatement ps) throws SQLException {
            try {
                int batchSize = pss.getBatchSize();
                InterruptibleBatchPreparedStatementSetter ipss = (pss instanceof InterruptibleBatchPreparedStatementSetter
                        ? (InterruptibleBatchPreparedStatementSetter) pss
                        : null);
                if (JdbcUtils.supportsBatchUpdates(ps.getConnection())) {
                    for (int i = 0; i < batchSize; i++) {
                        pss.setValues(ps, i);
                        if (ipss != null && ipss.isBatchExhausted(i)) {
                            break;
                        }
                        ps.addBatch();
                    }
                    return ps.executeBatch();
                } else {
                    List rowsAffected = new ArrayList();
                    for (int i = 0; i < batchSize; i++) {
                        pss.setValues(ps, i);
                        if (ipss != null && ipss.isBatchExhausted(i)) {
                            break;
                        }
                        rowsAffected.add(new Integer(ps.executeUpdate()));
                    }
                    int[] rowsAffectedArray = new int[rowsAffected.size()];
                    for (int i = 0; i < rowsAffectedArray.length; i++) {
                        rowsAffectedArray[i] = ((Integer) rowsAffected.get(i)).intValue();
                    }
                    return rowsAffectedArray;
                }
            } finally {
                if (pss instanceof ParameterDisposer) {
                    ((ParameterDisposer) pss).cleanupParameters();
                }
            }
        }
    }, true);
}