function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shiva RajendranShiva Rajendran 

Does Batch executes sequentially or parallelly?

I want to understand if a batch class with 1million records are run , will it executes 200 records per batch after batch? I mean by , at any point of time only 1 batch of records are executed? or is it like n number of batches of records can run parallelly too?
Want to understand if only 1 batch of records will be locked at any given records or it can be more.
Thanks,
Shiva RV
Best Answer chosen by Shiva Rajendran
Raj VakatiRaj Vakati
currently 'execute' methods run sequentially, though the documentation does not make it perfectly clear on this.  

I want to understand if a batch class with 1million records are run , will it executes 200 records per batch after batch?
Yes -- First 200 then next 200 like this 

I  mean by , at any point of time only 1 batch of records are executed? or is it like n number of batches of records can run parallelly too?

currently 'execute' methods run sequentially, though the documentation does not make it perfectly clear on this.  

Want to understand if only 1 batch of records will be locked at any given records or it can be more.

You can lock by one batch at a time to avoide the issue .. as soon as batch is execute  method ix ecuted you will be able to relase the record locking and can able to use by other batch 
 

All Answers

Raj VakatiRaj Vakati
currently 'execute' methods run sequentially, though the documentation does not make it perfectly clear on this.  

I want to understand if a batch class with 1million records are run , will it executes 200 records per batch after batch?
Yes -- First 200 then next 200 like this 

I  mean by , at any point of time only 1 batch of records are executed? or is it like n number of batches of records can run parallelly too?

currently 'execute' methods run sequentially, though the documentation does not make it perfectly clear on this.  

Want to understand if only 1 batch of records will be locked at any given records or it can be more.

You can lock by one batch at a time to avoide the issue .. as soon as batch is execute  method ix ecuted you will be able to relase the record locking and can able to use by other batch 
 
This was selected as the best answer
Shiva RajendranShiva Rajendran
Hi Raj,
Thanks for the clarity .Just a follow up , i assume if the batch hits the execute method those 200 records are locked till the end of the execute method.
Or is it like those all those 1 million records are locked till the batch completes the finish method?
Because i assumed as the first way but your explaination sounds more like the second way.

Thanks,
Shiva RV
Raj VakatiRaj Vakati
Sorry for confision ...  The batch process won't lock all records in the query locator, as that could be up to 50 milinons records 

And My Bad  ..Batch wnt lock even 200 records in exeucte method if you process 200 by each batch 

SO  you will get UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record or 200 records error is same record is access by another batch or process same time 
Shiva RajendranShiva Rajendran
Thanks Raj. still slightly misleading , you saying it wont lock the 200 reocrds but if another batch tries to access those records at the same time, will get unable to lock row error?
Thanks,
Shiva RV