com.atlassian.crowd.util.persistence.hibernate.batch
Class AbstractBatchProcessor

java.lang.Object
  extended by com.atlassian.crowd.util.persistence.hibernate.batch.AbstractBatchProcessor
All Implemented Interfaces:
BatchProcessor
Direct Known Subclasses:
Hibernate3BatchProcessor, Hibernate4BatchProcessor

public abstract class AbstractBatchProcessor
extends Object
implements BatchProcessor

Threadsafe batch processor.

This processor is essentially a heavyweight generic DAO for processing batched operations over a collection of entities.

The batchSize defaults to batchSize and can be manually set (via Spring, for example) using the appropriate setter method. The batchSize should match the hibernate.jdbc.batch_size property defined in the Hibernate configuration.

Each batch operation is first divided into smaller sets of batchSize. If there is an error in processing the batch, the batched JDBC call is rolled-back and the batch is processed individually.

This mechanism ensures very fast (JDBC-batched) inserts and updates and follows it up with a fail-over retry for the failing batches. Callback methods are provided to allow you include the session and transaction management you desire. The processing flow is: call beforeProcessCollection() For each item in the collection Add the item to the batch collection If the batch collection size >= the batch size call beforeProcessBatch() For each item in the batch collection perform the operation on the item call afterProcessBatch() or rollbackProcessBatch() if an error occured clear the batch collection If there are unprocessed items in the Batch Collcetion call beforeProcessBatch() For each item in the batch collection perform the operation on the item call afterProcessBatch() or rollbackProcessBatch() if an error occured call beforeProcessCollection() If an error occured during the processing of a batch collection then: For each item in the batch collection call beforeProcessIndividual() perform the operation on the item call afterProcessIndividual() or rollbackProcessIndividual() if an error occured

NOTE 1: Do not use this if you're database is not transactional, *stab* MySQL ISAM.

Author:
Shihab Hamid, Matthew Jensen

Constructor Summary
AbstractBatchProcessor()
           
 
Method Summary
protected abstract  void afterProcessBatch()
          Called after successfully processing each batch.
protected abstract  void afterProcessCollection()
          Called when processing the collection has completed successfully.
protected abstract  void afterProcessIndividual()
          Called after successully processing an item individually.
protected abstract  void beforeProcessBatch()
          Called before processing each batch.
protected abstract  void beforeProcessCollection()
          Called before the collection is processed into a batch.
protected abstract  void beforeProcessIndividual()
          Called before processing an individual item.
<E extends Serializable>
BatchResult<E>
execute(HibernateOperation op, Collection<E> objects)
          The set is first divided into smaller sets of batchSize.
protected abstract  void rollbackProcessBatch()
          Called after processing each batch where an exception was encountered.
protected abstract  void rollbackProcessIndividual()
          Called after processing an individual item where an exception was encountered.
 void setBatchSize(int batchSize)
          The batchSize value should be the same as the hibernate.jdbc.batch_size Hibernate property.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractBatchProcessor

public AbstractBatchProcessor()
Method Detail

execute

public final <E extends Serializable> BatchResult<E> execute(HibernateOperation op,
                                                             Collection<E> objects)
The set is first divided into smaller sets of batchSize. Each batchSet is added via a batched JDBC call (using Hibernate's batching mechanism). If there is an error in processing the batch, the batched JDBC call is rolled-back and the batchSet is processed individually.

This mechanism ensures very fast (JDBC-batched) inserts and updates and follows it up with a fail-over retry for the failing batches.

NOTE: do not use this if you're database is not transactional, *stab* MySQL ISAM.

Specified by:
execute in interface BatchProcessor
Parameters:
op - Hibernate operation to perform (eg. replicate, saveOrUpdate).
objects - set of DirectoryEntity objectst to batch add.
Returns:
batch result.

setBatchSize

public void setBatchSize(int batchSize)
The batchSize value should be the same as the hibernate.jdbc.batch_size Hibernate property.

Parameters:
batchSize - batch size used to group batches.

beforeProcessCollection

protected abstract void beforeProcessCollection()
Called before the collection is processed into a batch. Can be used to start session or transaction that will cover the execution of the entire collection which could involve multiple batches.


afterProcessCollection

protected abstract void afterProcessCollection()
Called when processing the collection has completed successfully.


beforeProcessBatch

protected abstract void beforeProcessBatch()
Called before processing each batch. Can be used to start session or transaction that will cover this particular batch.


afterProcessBatch

protected abstract void afterProcessBatch()
Called after successfully processing each batch.


rollbackProcessBatch

protected abstract void rollbackProcessBatch()
Called after processing each batch where an exception was encountered.


beforeProcessIndividual

protected abstract void beforeProcessIndividual()
Called before processing an individual item. This method will be called if the batch failed and the processor falls back to processing each item seperately.


afterProcessIndividual

protected abstract void afterProcessIndividual()
Called after successully processing an item individually.


rollbackProcessIndividual

protected abstract void rollbackProcessIndividual()
Called after processing an individual item where an exception was encountered.



Copyright © 2013 Atlassian. All Rights Reserved.