public class TestCpuTimeout implements Database.Batchable<sObject> {
public Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator('SELECT Id FROM Account LIMIT 200');
}
public void execute(Database.BatchableContext bc, List<sObject> scope) {
scope[0].put('NumberOfEmployees', 100);
update scope[0];
System.debug('Account updated: ' + scope[0].Id);
List<String> largeData = new List<String>();
for (Integer i = 0; i< 10000; i++) largeData.add('Test');
for (Integer i = 0; i < 500;1000; i++) {
// Not calling Limits.getCpuTime() seems to cause this batch job to silently fail.
// When there is a silent failure, any record updates do not get rolled back.
//System.debug(Limits.getCpuTime()); // This causes a CPU timeout.
//long l = Limits.getCpuTime(); // This causes a CPU timeout.
//System.debug('test'); // This does not cause a CPU timeout. This silently aborts. Record updates are not rolled back.
String data = JSON.serialize(largeData);
}
}
public void finish(Database.BatchableContext bc) { }
}
This may be related to this Known Issue, but if so, this issue has gone unfixed for over three months.
Update: This issue seems to still reproduce in Summer '17 sandboxes, so it is still not fixed as of 5/15/17.