Execution Confusion in Batch class Apex

断了今生、忘了曾经 提交于 2019-12-10 22:43:53

问题


I am calling a batch class instance and after completing the batch, I am calling two other batch class instances. The finish() method for first batch class is

public void finish(Database.BatchableContext BC)
{
    List<Event__c> events = [SELECT Id FROM Event__c];
    delete events;

    System.debug('Executing finish');
    for (CalendarSettings__c c: [SELECT Id, Name, CalendarId__c,
                                       CalendarQuery__c, FieldToDisplay__c
                                FROM CalendarSettings__c])
    {
        System.debug('Calendar Id is' + c.CalendarId__c);
        BatchPublicCampaignsToGoogle bjob = new BatchPublicCampaignsToGoogle(
                c.CalendarQuery__c, c.CalendarId__c, c.FieldToDisplay__c);
        Database.executeBatch(bjob,9);
    }

}

The problem I am facing is I am expecting that Batch class BatchPublicCampaignsToGoogle will be called two times but it is called only one time. The loop is running for two times (I analysed debug log). Why it is called only one time?


回答1:


Chaining Batch Jobs was introduced as a feature in Winter 13. From the release notes:

Starting a Batch Job from Another Batch Job

You can now start a batch job from another batch job by calling Database.executeBatch from the finish method of the batch class. This allows you to link your batch jobs and create a chain of jobs. Note that the governor limits of batch jobs still apply. This change applies to batch Apex saved using Salesforce.com API version 26.0 and later. Previously, with Apex saved using Salesforce.com API version 25.0 and earlier, you couldn’t call Database.executeBatch from within any batch Apex method. The version used is the version of the running batch class that starts another batch job. If the finish method in the running batch class calls a method in a helper class to start the batch job, the Salesforce.com API version of the helper class doesn't matter. Source

So for Apex classes using API version 26.0 and later you can chain one batch job onto the end of one that just finished. You can't branch out and start multiple batch jobs when one finishes.



来源:https://stackoverflow.com/questions/15595406/execution-confusion-in-batch-class-apex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!