/** * Overridden version of {@code update} that adds the given statement * parameters to the queue rather than executing them immediately. * All other {@code update} methods of the SqlUpdate base class go * through this method and will thus behave similarly. * <p>You need to call {@code flush} to actually execute the batch. * If the specified batch size is reached, an implicit flush will happen; * you still need to finally call {@code flush} to flush all statements. * @param params array of parameter objects * @return the number of rows affected by the update (always -1, * meaning "not applicable", as the statement is not actually * executed by this method) * @see #flush */ @Override public int update(Object... params) throws DataAccessException { validateParameters(params); this.parameterQueue.add(params.clone()); if (this.parameterQueue.size() == this.batchSize) { if (logger.isDebugEnabled()) { logger.debug("Triggering auto-flush because queue reached batch size of " + this.batchSize); } flush(); } return -1; }
/** * Construct an update object with a given DataSource, SQL, * anonymous parameters and specifying the maximum number of rows * that may be affected. * @param ds the DataSource to use to obtain connections * @param sql the SQL statement to execute * @param types the SQL types of the parameters, as defined in the * {@code java.sql.Types} class * @param batchSize the number of statements that will trigger * an automatic intermediate flush * @see java.sql.Types */ public BatchSqlUpdate(DataSource ds, String sql, int[] types, int batchSize) { super(ds, sql, types); setBatchSize(batchSize); }
/** * Trigger any queued update operations to be added as a final batch. * @return an array of the number of rows affected by each statement */ public int[] flush() { if (this.parameterQueue.isEmpty()) { return new int[0]; } int[] rowsAffected = getJdbcTemplate().batchUpdate( resolveSql(), new BatchPreparedStatementSetter() { @Override public int getBatchSize() { return parameterQueue.size(); } @Override public void setValues(PreparedStatement ps, int index) throws SQLException { Object[] params = parameterQueue.removeFirst(); newPreparedStatementSetter(params).setValues(ps); } }); for (int rowCount : rowsAffected) { checkRowsAffected(rowCount); if (this.trackRowsAffected) { this.rowsAffected.add(rowCount); } } return rowsAffected; }
given(connection.getMetaData()).willReturn(mockDatabaseMetaData); BatchSqlUpdate update = new BatchSqlUpdate(dataSource, sql); update.declareParameter(new SqlParameter(Types.INTEGER)); if (flushThroughBatchSize) { update.setBatchSize(2); update.update(ids[0]); update.update(ids[1]); assertEquals(0, update.getQueueCount()); assertEquals(2, update.getRowsAffected().length); assertEquals(2, update.getQueueCount()); assertEquals(0, update.getRowsAffected().length); int[] actualRowsAffected = update.flush(); assertEquals(0, update.getQueueCount()); actualRowsAffected = update.getRowsAffected(); assertTrue("executed 2 updates", actualRowsAffected.length == 2); assertEquals(rowsAffected[0], actualRowsAffected[0]); assertEquals(rowsAffected[1], actualRowsAffected[1]); update.reset(); assertEquals(0, update.getRowsAffected().length);
/** * Trigger any queued update operations to be added as a final batch. * @return an array of the number of rows affected by each statement */ public int[] flush() { if (this.parameterQueue.isEmpty()) { return new int[0]; } int[] rowsAffected = getJdbcTemplate().batchUpdate( getSql(), new BatchPreparedStatementSetter() { public int getBatchSize() { return parameterQueue.size(); } public void setValues(PreparedStatement ps, int index) throws SQLException { Object[] params = parameterQueue.removeFirst(); newPreparedStatementSetter(params).setValues(ps); } }); for (int rowCount : rowsAffected) { checkRowsAffected(rowCount); if (this.trackRowsAffected) { this.rowsAffected.add(rowCount); } } return rowsAffected; }
/** * Trigger any queued update operations to be added as a final batch. * @return an array of the number of rows affected by each statement */ public int[] flush() { if (this.parameterQueue.isEmpty()) { return new int[0]; } int[] rowsAffected = getJdbcTemplate().batchUpdate( resolveSql(), new BatchPreparedStatementSetter() { @Override public int getBatchSize() { return parameterQueue.size(); } @Override public void setValues(PreparedStatement ps, int index) throws SQLException { Object[] params = parameterQueue.removeFirst(); newPreparedStatementSetter(params).setValues(ps); } }); for (int rowCount : rowsAffected) { checkRowsAffected(rowCount); if (this.trackRowsAffected) { this.rowsAffected.add(rowCount); } } return rowsAffected; }
@Override public void setValues(PreparedStatement ps, int index) throws SQLException { Object[] params = parameterQueue.removeFirst(); newPreparedStatementSetter(params).setValues(ps); } });
public void setValues(PreparedStatement ps, int index) throws SQLException { Object[] params = parameterQueue.removeFirst(); newPreparedStatementSetter(params).setValues(ps); } });
/** * Overridden version of {@code update} that adds the given statement * parameters to the queue rather than executing them immediately. * All other {@code update} methods of the SqlUpdate base class go * through this method and will thus behave similarly. * <p>You need to call {@code flush} to actually execute the batch. * If the specified batch size is reached, an implicit flush will happen; * you still need to finally call {@code flush} to flush all statements. * @param params array of parameter objects * @return the number of rows affected by the update (always -1, * meaning "not applicable", as the statement is not actually * executed by this method) * @see #flush */ @Override public int update(Object... params) throws DataAccessException { validateParameters(params); this.parameterQueue.add(params.clone()); if (this.parameterQueue.size() == this.batchSize) { if (logger.isDebugEnabled()) { logger.debug("Triggering auto-flush because queue reached batch size of " + this.batchSize); } flush(); } return -1; }
/** * Construct an update object with a given DataSource, SQL, * anonymous parameters and specifying the maximum number of rows * that may be affected. * @param ds DataSource to use to obtain connections * @param sql SQL statement to execute * @param types SQL types of the parameters, as defined in the * {@code java.sql.Types} class * @param batchSize the number of statements that will trigger * an automatic intermediate flush * @see java.sql.Types */ public BatchSqlUpdate(DataSource ds, String sql, int[] types, int batchSize) { super(ds, sql, types); setBatchSize(batchSize); }
@Override public void setValues(PreparedStatement ps, int index) throws SQLException { Object[] params = parameterQueue.removeFirst(); newPreparedStatementSetter(params).setValues(ps); } });
/** * Overridden version of {@code update} that adds the given statement * parameters to the queue rather than executing them immediately. * All other {@code update} methods of the SqlUpdate base class go * through this method and will thus behave similarly. * <p>You need to call {@code flush} to actually execute the batch. * If the specified batch size is reached, an implicit flush will happen; * you still need to finally call {@code flush} to flush all statements. * @param params array of parameter objects * @return the number of rows affected by the update (always -1, * meaning "not applicable", as the statement is not actually * executed by this method) * @see #flush */ @Override public int update(Object... params) throws DataAccessException { validateParameters(params); this.parameterQueue.add(params.clone()); if (this.parameterQueue.size() == this.batchSize) { if (logger.isDebugEnabled()) { logger.debug("Triggering auto-flush because queue reached batch size of " + this.batchSize); } flush(); } return -1; }
/** * Construct an update object with a given DataSource, SQL, * anonymous parameters and specifying the maximum number of rows * that may be affected. * @param ds the DataSource to use to obtain connections * @param sql the SQL statement to execute * @param types the SQL types of the parameters, as defined in the * {@code java.sql.Types} class * @param batchSize the number of statements that will trigger * an automatic intermediate flush * @see java.sql.Types */ public BatchSqlUpdate(DataSource ds, String sql, int[] types, int batchSize) { super(ds, sql, types); setBatchSize(batchSize); }