9

I encounter an UNABLE_TO_LOCK_ROW error in a batch job where I do an upsert on a custom object. The problem is that I am not able to guarantee that a particular record is only upserted in one single batch, i.e. a record may (and will) be upserted multiple times during the execution of the batch job.

If the execution of batch jobs is strictly sequential, i have to look elsewhere for the cause of the lock error. If execute() methods of the same batch job can run in parallel, however, this may be the cause of the lock error and may even lead to serious logical errors (e.g. getting two records from two upserts instead of just one if they were run sequentially).

So my question is: Am i guaranteed that batches from one instance of a batch job are always processed strictly sequentially?

1
  • I believe execute() is run in the same order as your List returned from your start() method (assuming you are using Database.getQueryLocator) i.e. in the order of your Order By statement on your SOQL query Commented Jul 5, 2013 at 10:55

2 Answers 2

12

Yes, currently 'execute' methods run sequentially, though the documentation does not make it perfectly clear on this. In the future Salesforce may enable parallel running, though I'd expect this to be enabled via a annotation or interface marker on the class. So hopefully we would get the chance to opt into it or not.

So it maybe possible your users are running your job again and thus in parallel over the same data, or indeed other activities as you suspect.

Note: Also this comment from the Batch Apex docs, which might be important to you.

Batches of records are not guaranteed to execute in the order they are received from the start method.

6
  • 1
    It seems Salesforce are loathed to 'guarantee' anything where Batch apex is concerned Commented Jul 5, 2013 at 11:19
  • If you have future methods, they will be called while the batch is still executing. This can cause problems if the records involved in the future method appears in the batch at the same time.
    – sfdcfox
    Commented Jul 5, 2013 at 19:15
  • Hi Andrew, thanks for your answer! I guess i must be having a different problem then :( Commented Jul 10, 2013 at 8:05
  • @PatrickOscity Did you find your problem (and solution) in the end? Commented Nov 14, 2019 at 10:29
  • @WillemMulder sorry I can't remember what caused this issue any more. I've also moved away from Salesforce a few years ago. Good luck though! Commented Nov 14, 2019 at 14:24
1

This is only true for the jobs of a Single batch. But as Salesforce allows you to put up to 5 such job packages into it's queue you have parallel processing.

If you really rely on sequential ordering for all enqueued jobs you need to somewhat build your own Apex queue.

I am currently trying to create an improved custom Apex queue as part of a GitHub project I would love to get your feedback and invite you to fork and collaborate on it.

You must log in to answer this question.

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