title: have uncommitted work pending. Please commit or rollback before calling out
body: <p>i have a batch class </p>
  
  <pre><code> public class BatchCreateGCalendars implements Database.Batchable&lt;SObject&gt;, Database.Stateful, Database.AllowsCallouts {
  
          private String query;
          private String pageToken ;
          private String accessToken;
          private String CalendarId;
  
          public BatchCreateGCalendars(){
  
              this.query = 'Select Id, Name, CalendarId__c from CalendarSettings__c'; // Query to get the CalendardID (Google ID)
  
          }
  
          public Database.Querylocator start(Database.BatchableContext BC){
  
              return Database.getQueryLocator(query);
  
          }
  
          public void execute(Database.BatchableContext BC, List&lt;sObject&gt; scope){
  
              GoogleForce__c gApp           = GoogleForce__c.getAll().values()[0];        
              GCalendarUtil.Calendar cal    = new GCalendarUtil.Calendar();
              GCalendarUtil.GResponse gResp = new GCalendarUtil.GResponse();  
  
  
              String endPoint = GCalendarUtil.CALENDAR_BASE + GCalendarUtil.CAL_URL; // Calendar Endpoint
  
              // Iterate over the records, delete each calendar and create the calendar again.
  
              for(CalendarSettings__c c : (List&lt;CalendarSettings__c&gt;)scope){
  
  
  
                  String delEndpoint = endpoint + '/' + c.CalendarId__c;
  
                  if(gApp.ExpiresIn__c &lt; system.now()){
                      gResp               = GCalendarUtil.getNewToken(gApp);
                      accessToken         = gResp.access_token;
                      gApp.AccessToken__c = gResp.access_token;
                      gApp.ExpiresIn__c   = System.now().addSeconds(gResp.expires_in);
  
                  }else{
                      accessToken = gApp.AccessToken__c;
                      System.debug('Calendar Id  is '+ c.CalendarId__c);
                  }
                String re=  GCalendarUtil.doApiCall(null,'GET','https://www.googleapis.com/calendar/v3/calendars/'+c.CalendarId__c+'/events',accessToken);
  
              re = re.replaceAll('"end":','"end1":');
      re = re.replaceAll('"dateTime":','"dateTime1":');  
                System.debug('response is' +re);
                 JSON2Apex1 aa = JSON2Apex1.parse(re);
  
                 List&lt;JSON2Apex1.Items&gt; ii = aa.items ;
                          System.debug('next token is'+ aa.nextPageToken);
                 List&lt;String&gt; event_id =new List&lt;String&gt;();
                 if(ii!= null){
                 for(JSON2Apex1.Items i: ii){
                 event_id.add(i.id);
                 }
                 }
             for(String s:event_id)
             {GCalendarUtil.doApiCall(null,'DELETE','https://www.googleapis.com/calendar/v3/calendars/'+c.CalendarId__c+'/events/'+s,accessToken);
  
             }
         pageToken = aa.nextPageToken;
         CalendarId=c.CalendarId__c;
  
                 if(pageToken!=null)
                 deleteEvents(accessToken , pageToken,c.CalendarId__c);
  
              }
  
              update gApp;
          }
  
          /*
              After the batch job is finished, trigger the other batch jobs where the Campaigns are sent as Events to the calendars. 
              Use the CalendarQuery field on the each calendarSettings record.
          */
  
          public void finish(Database.BatchableContext BC){   
  
          BatchDeleteEvents bjob1 = new BatchDeleteEvents(CalendarId,accessToken);
                  Database.executeBatch(bjob1,9); 
  
  
              for(CalendarSettings__c c  : [Select Id, Name, CalendarQuery__c, CalendarId__c,FieldToDisplay__c from CalendarSettings__c WHERE Name IN ('Public Calendar', 'Internal marketing Calendar')]){
  
                  BatchPublicCampaignsToGoogle bjob = new BatchPublicCampaignsToGoogle(c.CalendarQuery__c,c.CalendarId__c,c.FieldToDisplay__c);
                  Database.executeBatch(bjob,9); // This is set to process 9 records, allowing 1 extra callout for refresh token in case needed.
  
              }
  
  
          }
  
          public static void deleteEvents(String accessToken,String pageToken,String CalendarId){
           List&lt;Event__c&gt; event_id =new List&lt;Event__c&gt;();
  
          while(pageToken != null)
      {   String re = GCalendarUtil.doApiCall(null,'GET','https://www.googleapis.com/calendar/v3/calendars/'+CalendarId+'/events?pageToken='+pageToken,accessToken);
                      System.debug('next page response is'+ re);
                       re = re.replaceAll('"end":','"end1":');
      re = re.replaceAll('"dateTime":','"dateTime1":');  
        JSON2Apex1 aa = JSON2Apex1.parse(re);
         List&lt;JSON2Apex1.Items&gt; ii = aa.items ;
        System.debug('size of ii'+ii.size());
        pageToken = aa.nextPageToken ;
  
                 if(ii!= null){
                 for(JSON2Apex1.Items i: ii){
                 event_id.add(new Event__c(name = i.id));
                 }
                 }
  
  
  
       }   
       insert event_id;
  
  
          }
  
      }
  </code></pre>
  
  <p>in this code  i am getting error at this line </p>
  
  <pre><code>String re=  GCalendarUtil.doApiCall(null,'GET','https://www.googleapis.com/calendar/v3/calendars/'+c.CalendarId__c+'/events',accessToken);
  </code></pre>
  
  <p>error is </p>
  
  <pre><code>You have uncommitted work pending. Please commit or rollback before calling out
  </code></pre>
  
  <p>why i am facing this error please help and one more doubt is  when i call this code in finish method </p>
  
  <pre><code> BatchDeleteEvents bjob1 = new BatchDeleteEvents(CalendarId,accessToken);
                      Database.executeBatch(bjob1,9); 
  </code></pre>
  
  <p>it will execute this code synchronously na means all objects in batch of 9 processed and then control will come on this code </p>
  
  <pre><code>BatchPublicCampaignsToGoogle bjob = new BatchPublicCampaignsToGoogle(c.CalendarQuery__c,c.CalendarId__c,c.FieldToDisplay__c);
                      Database.executeBatch(bjob,9);
  </code></pre>