OPEN - BATCH-554: *StepFactoryBeans should have a list of exceptions that do and don't cause rollback.

added txValidExceptionClasses property to SkipLimitStepFactoryBean (passed to ItemSkipPolicyItemHandler#doNotRethrowExceptionClasses)
This commit is contained in:
robokaso
2008-04-29 10:32:44 +00:00
parent 203f4a0e4d
commit c944b66195
4 changed files with 105 additions and 14 deletions

View File

@@ -38,7 +38,13 @@ import org.springframework.transaction.support.TransactionSynchronizationManager
/**
* {@link ItemHandler} that implements skip behavior. It delegates to
* {@link #setItemSkipPolicy(ItemSkipPolicy)} to decide whether skip should be called or not.
* {@link #setItemSkipPolicy(ItemSkipPolicy)} to decide whether skip should be
* called or not.
*
* When exception is skipped on read it is *not* re-thrown (does not cause tx
* rollback). Skipped exception on write is re-thrown by default (causes tx
* rollback) unless the exception class is included in
* {@link #setDoNotRethrowExceptionClasses(Class[])}.
*
* If exception is thrown while reading the item, skip is called on the
* {@link ItemReader}. If exception is thrown while writing the item, skip is
@@ -63,6 +69,8 @@ public class ItemSkipPolicyItemHandler extends SimpleItemHandler {
private Map skippedExceptions = new HashMap();
private Class[] doNotRethrowExceptionClasses = new Class[] {};
private ItemKeyGenerator defaultItemKeyGenerator = new ItemKeyGenerator() {
public Object getKey(Object item) {
return item;
@@ -230,12 +238,27 @@ public class ItemSkipPolicyItemHandler extends SimpleItemHandler {
// roll back
addSkippedException(key, e);
logger.debug("Added item to skip list; key=" + key);
// return without re-throwing if exception shouldn't cause
// rollback
if (!shouldRethrow(e)) {
return;
}
}
// always re-throw exception on write
// re-throw exception on write by default
throw e;
}
}
private boolean shouldRethrow(Exception e) {
for (int i = 0; i < doNotRethrowExceptionClasses.length; i++) {
if (doNotRethrowExceptionClasses[i].isAssignableFrom(e.getClass())) {
return false;
}
}
return true;
}
public void mark() throws MarkFailedException {
super.mark();
clearSkippedExceptions();
@@ -298,4 +321,12 @@ public class ItemSkipPolicyItemHandler extends SimpleItemHandler {
}
}
/**
* doNotRethrowExceptionClasses will not be re-thrown when skipped.
* @param doNotRethrowExceptionClasses empty by default
*/
public void setDoNotRethrowExceptionClasses(Class[] doNotRethrowExceptionClasses) {
this.doNotRethrowExceptionClasses = doNotRethrowExceptionClasses;
}
}

View File

@@ -33,6 +33,8 @@ public class SkipLimitStepFactoryBean extends SimpleStepFactoryBean {
private Class[] skippableExceptionClasses = new Class[] { Exception.class };
private Class[] fatalExceptionClasses = new Class[] { Error.class };
private Class[] txValidExceptionClasses = new Class[] {};
private ItemKeyGenerator itemKeyGenerator;
@@ -125,6 +127,17 @@ public class SkipLimitStepFactoryBean extends SimpleStepFactoryBean {
public void setSkipCacheCapacity(int skipCacheCapacity) {
this.skipCacheCapacity = skipCacheCapacity;
}
/**
* Skippable txValidExceptionClasses will *not* cause transaction rollback.
*
* @param txValidExceptionClasses empty by default
*
* @see #setSkippableExceptionClasses(Class[])
*/
public void setTxValidExceptionClasses(Class[] txValidExceptionClasses) {
this.txValidExceptionClasses = txValidExceptionClasses;
}
/**
* Uses the {@link #setSkipLimit(int)} value to configure item handler and and
@@ -148,6 +161,7 @@ public class SkipLimitStepFactoryBean extends SimpleStepFactoryBean {
LimitCheckingItemSkipPolicy limitCheckingSkipPolicy = new LimitCheckingItemSkipPolicy(skipLimit, Arrays
.asList(skippableExceptionClasses), fatalExceptionList);
itemHandler.setItemSkipPolicy(limitCheckingSkipPolicy);
itemHandler.setDoNotRethrowExceptionClasses(txValidExceptionClasses);
this.itemSkipPolicy = limitCheckingSkipPolicy;
SimpleLimitExceptionHandler exceptionHandler = new SimpleLimitExceptionHandler(skipLimit);
exceptionHandler.setExceptionClasses(skippableExceptionClasses);