Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you 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, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package groovy.sql;
 
 
 import java.util.List;
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<Integerresults;
 
     public BatchingStatementWrapper(Statement delegateint batchSizeLogger log) {
         this. = delegate;
         this. = batchSize;
         this. = log;
         reset();
     }
 
     protected void reset() {
          = 0;
          = new ArrayList<Integer>();
     }
 
     @Override
     public Object invokeMethod(String nameObject args) {
         return InvokerHelper.invokeMethod(nameargs);
     }
 
     public void addBatch(String sqlthrows SQLException {
         .addBatch(sql);
         ++;
         if ( ==  /* never true for batchSize of 0 */) {
             int[] result = .executeBatch();
             processResult(result);
              = 0;
         }
     }
 
     public void clearBatch() throws SQLException {
         if ( != 0) {
             reset();
         }
         .clearBatch();
     }
 
     public int[] executeBatch() throws SQLException {
         int[] lastResult = .executeBatch();
         processResult(lastResult);
         int[] result = new int[.size()];
         for (int i = 0; i < .size(); i++) {
             result[i] = .get(i);
         }
         reset();
         return result;
     }
 
     protected void processResult(int[] lastResult) {
         boolean foundError = false;
         for (int i : lastResult) {
             if (i == .foundError = true;
             .add(i);
         }
         // A little bit of paranoid checking here? Most drivers will throw BatchUpdateException perhaps?
         if ( != lastResult.length) {
             .warning("Problem executing batch - expected result length of " +  + " but got " + lastResult.length);
         } else if (foundError) {
             .warning("Problem executing batch - at least one result failed in: " + DefaultGroovyMethods.toList(lastResult));
         } else {
            .fine("Successfully executed batch with " + lastResult.length + " command(s)");
        }
    }
    public void close() throws SQLException {
        .close();
    }
New to GrepCode? Check out our FAQ X