FileDocCategorySizeDatePackage
BatchingBatcher.javaAPI DocHibernate 3.2.52102Thu Jun 22 14:51:44 BST 2006org.hibernate.jdbc

BatchingBatcher

public class BatchingBatcher extends AbstractBatcher
An implementation of the Batcher interface that actually uses batching
author
Gavin King

Fields Summary
private int
batchSize
private Expectation[]
expectations
Constructors Summary
public BatchingBatcher(ConnectionManager connectionManager, org.hibernate.Interceptor interceptor)

		super( connectionManager, interceptor );
		expectations = new Expectation[ getFactory().getSettings().getJdbcBatchSize() ];
	
Methods Summary
public voidaddToBatch(Expectation expectation)

		if ( !expectation.canBeBatched() ) {
			throw new HibernateException( "attempting to batch an operation which cannot be batched" );
		}
		PreparedStatement batchUpdate = getStatement();
		batchUpdate.addBatch();
		expectations[ batchSize++ ] = expectation;
		if ( batchSize == getFactory().getSettings().getJdbcBatchSize() ) {
			doExecuteBatch( batchUpdate );
		}
	
private voidcheckRowCounts(int[] rowCounts, java.sql.PreparedStatement ps)

		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].verifyOutcome( rowCounts[i], ps, i );
		}
	
protected voiddoExecuteBatch(java.sql.PreparedStatement ps)

		if ( batchSize == 0 ) {
			log.debug( "no batched statements to execute" );
		}
		else {
			if ( log.isDebugEnabled() ) {
				log.debug( "Executing batch size: " + batchSize );
			}

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

		}