public int[] withBatch(int batchSize, Closure closure) throws SQLException {
        Connection connection = createConnection();
        BatchingStatementWrapper statement = null;
        boolean savedWithinBatch = withinBatch;
        try {
            withinBatch = true;
            statement = new BatchingStatementWrapper(createStatement(connection), batchSize, LOG);
            closure.call(statement);
            return statement.executeBatch();
        } catch (SQLException e) {
            LOG.warning("Error during batch execution: " + e.getMessage());
            throw e;
        } finally {
            closeResources(statement);
            closeResources(connection);
            withinBatch = savedWithinBatch;
        }
    }
public int [ ] withBatch ( int batchSize , Closure closure ) throws SQLException { Connection connection = createConnection ( ) ; BatchingStatementWrapper statement = null ; boolean savedWithinBatch = withinBatch ; try { withinBatch = true ; statement = new BatchingStatementWrapper ( createStatement ( connection ) , batchSize , LOG ) ; closure . call ( statement ) ; return statement . executeBatch ( ) ; } catch ( SQLException e ) { LOG . warning ( "Error during batch execution: " + e . getMessage ( ) ) ; throw e ; } finally { closeResources ( statement ) ; closeResources ( connection ) ; withinBatch = savedWithinBatch ; } }
Performs the closure (containing batch operations) within a batch using a given batch size.
<p>
After every <code>batchSize</code> <code>addBatch(sqlBatchOperation)</code>
operations, automatically calls an <code>executeBatch()</code> operation to "chunk" up the database operations
into partitions. Though not normally needed, you can also explicitly call <code>executeBatch()</code> which
after executing the current batch, resets the batch count back to zero.
<p>
The closure will be called with a single argument; the database statement
(actually a <code>BatchingStatementWrapper</code> helper object)
associated with this batch.
<p>
Use it like this for batchSize of 20:
<pre>
def updateCounts = sql.withBatch(20) { stmt {@code ->}
stmt.addBatch("insert into TABLENAME ...")
stmt.addBatch("insert into TABLENAME ...")
stmt.addBatch("insert into TABLENAME ...")
...
}
</pre>
For integrity and performance reasons, you may wish to consider executing your batch command(s) within a transaction:
<pre>
sql.withTransaction {
def result1 = sql.withBatch { ... }
...
}
</pre>

@param batchSize partition the batch into batchSize pieces, i.e. after batchSize
<code>addBatch()</code> invocations, call <code>executeBatch()</code> automatically;
0 means manual calls to executeBatch are required
@param closure   the closure containing batch and optionally other statements
@return an array of update counts containing one element for each
command in the batch.  The elements of the array are ordered according
to the order in which commands were added to the batch.
@throws SQLException if a database access error occurs,
or this method is called on a closed <code>Statement</code>, or the
driver does not support batch statements. Throws {@link java.sql.BatchUpdateException}
(a subclass of <code>SQLException</code>) if one of the commands sent to the
database fails to execute properly or attempts to return a result set.
@see #withBatch(Closure)
@see BatchingStatementWrapper
@see Statement