In the trailhead lesson "Use Batch Apex" the final line states:
Use extreme care if you are planning to invoke a batch job from a trigger. You must be able to guarantee that the trigger won't add more batch jobs than the limit.
As far as I can see, the limit for concurrent batch processes is 5.
However when I run the following code in order to try and run more than 5 batch processes concurrently, it appears to automatically queue jobs so that the max number running at any time is 5. So - my question - what do you have to do to add more batch jobs than the limit in a trigger and break it?
public class BatchTest {
public static void triggerTest(){
List<String> stringList = new List<String>{'hello'};
for(integer i = 0; i < 8; i++){
Integer runningBatchJobs = [SELECT count() FROM AsyncApexJob WHERE JobType = 'BatchApex' AND status IN ('Queued', 'Processing', 'Preparing')];
BatchTestBatch batchObject = new BatchTestBatch(stringList);
Id batchId = Database.executeBatch(batchObject, 1);
}
} }
batchClass :
global class BatchTestBatch implements Database.Batchable<String>{
private List<String> idList;
public BatchTestBatch(List<String> newIdList){
idList = newIdList;
}
global Iterable<String> start(Database.BatchableContext bc){
return idList;
}
global void execute(Database.BatchableContext bc, List<String> scope){
for(integer i = 0; i < 1; i++){
test__c[] myTest = [SELECT name FROM test__c];
}
Integer runningBatchJobs = [SELECT count() FROM AsyncApexJob WHERE JobType = 'BatchApex' AND status IN ('Queued', 'Processing', 'Preparing')];
System.debug(runningBatchJobs);
System.debug('hi');
}
global void finish(Database.BatchableContext bc){
System.debug(' finished');
}}