home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (BatchingStatementWrapper.java)

This example Groovy source code file (BatchingStatementWrapper.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

arraylist, batchingstatementwrapper, batchingstatementwrapper, groovyobjectsupport, jdbc, log, logger, logging, object, object, override, problem, problem, sql, sqlexception, sqlexception, statement, successfully, util

The Groovy BatchingStatementWrapper.java source code

 * Copyright 2003-2011 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.
package groovy.sql;

import groovy.lang.GroovyObjectSupport;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.codehaus.groovy.runtime.InvokerHelper;

import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

 * Class which delegates to a Statement but keeps track of a batch count size.
 * If the batch count reaches the predefined number, this Statement does an executeBatch()
 * automatically. If batchSize is zero, then no batching is performed.
public class BatchingStatementWrapper extends GroovyObjectSupport {
    private Statement delegate;
    protected int batchSize;
    protected int batchCount;
    protected Logger log;
    protected List<Integer> results;

    public BatchingStatementWrapper(Statement delegate, int batchSize, Logger log) {
        this.delegate = delegate;
        this.batchSize = batchSize;
        this.log = log;

    protected void reset() {
        batchCount = 0;
        results = new ArrayList<Integer>();

    public Object invokeMethod(String name, Object args) {
        return InvokerHelper.invokeMethod(delegate, name, args);

    public void addBatch(String sql) throws SQLException {
        if (batchCount == batchSize /* never true for batchSize of 0 */) {
            int[] result = delegate.executeBatch();
            batchCount = 0;

    public void clearBatch() throws SQLException {
        if (batchSize != 0) {

    public int[] executeBatch() throws SQLException {
        int[] lastResult = delegate.executeBatch();
        int[] result = new int[results.size()];
        for (int i = 0; i < results.size(); i++) {
            result[i] = results.get(i);
        return result;

    protected void processResult(int[] lastResult) {
        boolean foundError = false;
        for (int i : lastResult) {
            if (i == Statement.EXECUTE_FAILED) foundError = true;
        // A little bit of paranoid checking here? Most drivers will throw BatchUpdateException perhaps?
        if (batchCount != lastResult.length) {
            log.warning("Problem executing batch - expected result length of " + batchCount + " but got " + lastResult.length);
        } else if (foundError) {
            log.warning("Problem executing batch - at least one result failed in: " + DefaultGroovyMethods.toList(lastResult));
        } else {
            log.fine("Successfully executed batch with " + lastResult.length + " command(s)");

    public void close() throws SQLException {

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy BatchingStatementWrapper.java source code file:

new blog posts


Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.