Example usage for org.springframework.jdbc.core BatchPreparedStatementSetter getBatchSize

List of usage examples for org.springframework.jdbc.core BatchPreparedStatementSetter getBatchSize

Introduction

In this page you can find the example usage for org.springframework.jdbc.core BatchPreparedStatementSetter getBatchSize.

Prototype

int getBatchSize();

Source Link

Document

Return the size of the batch.

Usage

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

private void doInPreparedStatementWithBpss(int sendBatchSize, int pssBatchSize) throws SQLException {
    BatchPreparedStatementSetter pss = mock(BatchPreparedStatementSetter.class);
    when(pss.getBatchSize()).thenReturn(pssBatchSize);

    BatchingPreparedStatementCallback psc = new BatchingPreparedStatementCallback(sendBatchSize, pss);

    int[] result = psc.doInPreparedStatement(this.ops);

    assertThat(result, matchesRowCounts(sendBatchSize, pssBatchSize));
    verifyPreparedStatementCalls(pssBatchSize, pss);
}

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 + "]");
    }/* www .jav  a2s  . 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);
}

From source file:org.springframework.jdbc.core.JdbcTemplate.java

public int[] batchUpdate(String sql, final BatchPreparedStatementSetter pss) throws DataAccessException {
    if (logger.isDebugEnabled()) {
        logger.debug("Executing SQL batch update [" + sql + "]");
    }//from  w w w  . java  2  s . c  om
    return (int[]) execute(sql, new PreparedStatementCallback() {
        public Object doInPreparedStatement(PreparedStatement ps) throws SQLException {
            int batchSize = pss.getBatchSize();
            DatabaseMetaData dbmd = ps.getConnection().getMetaData();
            try {
                boolean supportsBatchUpdates = false;
                try {
                    if (dbmd != null) {
                        if (dbmd.supportsBatchUpdates()) {
                            if (logger.isDebugEnabled()) {
                                logger.debug("Batch Updates supported for [" + dbmd.getDriverName() + " "
                                        + dbmd.getDriverVersion() + "]");
                            }
                            supportsBatchUpdates = true;
                        } else {
                            if (logger.isDebugEnabled()) {
                                logger.debug("Batch Updates are not supported for [" + dbmd.getDriverName()
                                        + " " + dbmd.getDriverVersion() + "]");
                            }
                        }
                    }
                } catch (AbstractMethodError ame) {
                    logger.warn("Driver does not support JDBC 2.0 method supportsBatchUpdatres ["
                            + dbmd.getDriverName() + " " + dbmd.getDriverVersion() + "]");
                }
                if (supportsBatchUpdates) {
                    for (int i = 0; i < batchSize; i++) {
                        pss.setValues(ps, i);
                        ps.addBatch();
                    }
                    return ps.executeBatch();
                } else {
                    int[] rowsAffected = new int[batchSize];
                    for (int i = 0; i < batchSize; i++) {
                        pss.setValues(ps, i);
                        rowsAffected[i] = ps.executeUpdate();
                    }
                    return rowsAffected;
                }
            } finally {
                if (pss instanceof ParameterDisposer) {
                    ((ParameterDisposer) pss).cleanupParameters();
                }
            }
        }
    });
}