I have a batch process that I'd like to completely halt if an exception is encountered. According to the Apex docs on Using Batch Apex, Database.BatchableContext.getJobID
:
Returns the ID of the
AsyncApexJob
object associated with this batch job as a string. Use this method to track the progress of records in the batch job. You can also use this ID with theSystem.abortJob
method.
I've setup my execute
method as follows:
global void execute(Database.BatchableContext BC, List<sObject> scope) {
try {
// do some work, maybe throw exceptions
}
catch (Exception e) {
system.debug('Exception in DataMigration_Batch.execute(): ' + e);
system.debug(e.getStackTraceString());
// cancel import
string importJobId = BC.getJobId(); // docs state this is string;
// also tried declaring as id
system.debug('importJobId: ' + importJobId);
System.abortJob(importJobId);
throw new DataMigrationException(e.getMessage());
}
}
If I encounter an exception in the first batch, I see the debug messages, and the new exception is thrown at then and of the catch block. But the Batch job is NOT cancelled. In developer console I see each batch's log, and I see a log for the finish()
method (to which I've added debugging output to be certain it's running). In the Apex Jobs setup UI page, I see the job in completed status with all batches enumerated.
When starting the job I log the Job ID, and I've compared the value to the value logged in my exception handler which is then passed to abortJob()
. They are the same.
I have seen multiple posts online (e.g., SF Developer Forums, this SFSE post) that show abortJob()
being called from within execute()
, and I can find nothing in the docs to suggest that it should not work. But I've run a job with 39 failing batches, each of which called abortJob()
, and still the job ran to the finish()
method, and did not abort.
Note that while it is my understanding that abortJob()
should prevent finish()
from running, I don't care if finish()
runs or not; I just want to stop running execute()
once an individual batch fails.