2

Execution Governors and Limits documentation says that

Maximum execution time for each Apex transaction is 10 minutes for both Synchronous and Asynchronous

To understand this I executed the below code in anonymous apex window :

Long startTime = System.now().getTime();
Long maxLimit = 9*60*1000; //I am taking max limit as 9 mins.
while(System.now().getTime() - startTime <= maxLimit ){  
}

I get this error :

enter image description here

What is one use case where we don't run into CPU time limit(or other limits) but reach execution time limit?

1 Answer 1

8

The ten minute limit is pretty hard to reach in normal conditions, but you need to remember that executing Apex Code is only one part of a transaction, while the ten minute limit applies to everything in the transaction, literally in wall-clock time. For example, it's possible to roll back transactions partway using the DML retry mechanism; while the CPU time can be harvested back because of retries, the limit towards the ten minutes would still apply.

One "easy" way to see this is to write a complicated query with sub-queries and expensive formulas against an object with millions of records in a Batchable's start method. If there's a lot of database time used, this could theoretically cause the Batchable class to fail. That's why it's preferable to query only the fields you need, and then query the remainder of the fields and any sub-queries in the execute method.

Realistically, you'll probably never see this error, but you should know that it can occur. In those cases, all you can do is try again; this would usually resolve the issue. One theoretical point where you could hit this limit is if you consistently use row locks in a way that causes maximum lock timeout (~10 seconds), which would theoretically allow you to construct a transaction that runs for an hour or longer if not for this limit. To avoid that situation, remember to always lock parents before children, and do so in record Id order, when possible.

6
  • Or you can easily replicate this by running few successive callouts in a transaction that's take a minute or more to complete. Commented May 7, 2018 at 7:43
  • 1
    @PranayJaiswal Yes, but that only gets you as far as two minutes, still eight minutes away from being able to trigger the ten minute rule. It's rather hard to intentionally engineer a way to trigger this rule.
    – sfdcfox
    Commented May 7, 2018 at 12:02
  • I meant in a loop do few callouts and each callout taking 120 secs.. Bamm 10min transactions limit broken. Commented May 7, 2018 at 12:55
  • 2
    @PranayJaiswal You'd like to think so, but the 120 second limit is per transaction, not per callout. About the best that you could do would be to design two processes that fight over row-level record locks, since record locks take up to 10 seconds to acquire before they fail, and you can retry them.
    – sfdcfox
    Commented May 7, 2018 at 12:59
  • @sfdcfox As per docs, we can make 100 callouts in a transaction. But if 120 second limit applies to all callouts in a transaction, it means, I can do only one callout of 120 second in a transaction.. Is that right?
    – javanoob
    Commented May 7, 2018 at 14:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .