Open Source Repository

Home /hibernate/hibernate-3.2.4.ga | Repository Home



org/hibernate/jdbc/BatchingBatcher.java
//$Id: BatchingBatcher.java 10040 2006-06-22 19:51:43Z [email protected] $
package org.hibernate.jdbc;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Interceptor;
import org.hibernate.StaleStateException;

/**
 * An implementation of the <tt>Batcher</tt> interface that
 * actually uses batching
 @author Gavin King
 */
public class BatchingBatcher extends AbstractBatcher {

  private int batchSize;
  private Expectation[] expectations;
  
  public BatchingBatcher(ConnectionManager connectionManager, Interceptor interceptor) {
    superconnectionManager, interceptor );
    expectations = new ExpectationgetFactory().getSettings().getJdbcBatchSize() ];
  }

  public void addToBatch(Expectation expectationthrows SQLException, HibernateException {
    if !expectation.canBeBatched() ) {
      throw new HibernateException"attempting to batch an operation which cannot be batched" );
    }
    PreparedStatement batchUpdate = getStatement();
    batchUpdate.addBatch();
    expectationsbatchSize++ = expectation;
    if batchSize == getFactory().getSettings().getJdbcBatchSize() ) {
      doExecuteBatchbatchUpdate );
    }
  }

  protected void doExecuteBatch(PreparedStatement psthrows SQLException, HibernateException {
    if batchSize == ) {
      log.debug"no batched statements to execute" );
    }
    else {
      if log.isDebugEnabled() ) {
        log.debug"Executing batch size: " + batchSize );
      }

      try {
        checkRowCountsps.executeBatch(), ps );
      }
      catch (RuntimeException re) {
        log.error"Exception executing batch: ", re );
        throw re;
      }
      finally {
        batchSize = 0;
      }

    }

  }

  private void checkRowCounts(int[] rowCounts, PreparedStatement psthrows SQLException, HibernateException {
    int numberOfRowCounts = rowCounts.length;
    if numberOfRowCounts != batchSize ) {
      log.warn"JDBC driver did not return the expected number of row counts" );
    }
    for int i = 0; i < numberOfRowCounts; i++ ) {
      expectations[i].verifyOutcomerowCounts[i], ps, i );
    }
  }

}