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
Archit Budhraja 7Archit Budhraja 7 

Encountering "TooManyLockFailure" error repeatedly in Bulk Data Load jobs

We have several jobs scheduled to run every 30 minutes that either update or upsert information from our Data Warehouse into Salesforce. These jobs run using the Bulk API in parallel mode. A lot of these jobs have had batches that have been failing repeatedly with the following error - 

Error: Batch 751a0000007HacXAAS of job 750a0000003Y1X6AAK failed: InternalServerError : Retried more than 10 times;
TooManyLockFailure : Too many lock failure 199 Trying again later.;
TooManyLockFailure : Too many lock failure 199 Trying again later.;
TooManyLockFailure : Too many lock failure 199 Trying again later.;
TooManyLockFailure : Too many lock failure 197 Trying again later.;
TooManyLockFailure : Too many lock failure 198 Trying again later.;
TooManyLockFailure : Too many lock failure 194 Trying again later.;
TooManyLockFailure : Too many lock failure 196 Trying again later.;
TooManyLockFailure : Too many lock failure 199 Trying again later.;
TooManyLockFailure : Too many lock failure 199 Trying again later.;
TooManyLockFailure : Too many lock failure 200 Trying again later.

As a result, we end up having bad/missing data in Salesforce. I'm struggling to diagnose what exactly is causing this issue, any help here would be appreciated. Thanks!
Mahesh DMahesh D
Hi Archit,

Please follow below links:

https://developer.salesforce.com/docs/atlas.en-us.api_asynch.meta/api_asynch/asynch_api_reference_errors.htm
https://developer.salesforce.com/docs/atlas.en-us.api_asynch.meta/api_asynch/asynch_api_planning_guidelines.htm

Regards,
Mahesh
Amit Chaudhary 8Amit Chaudhary 8
It look like to many code is executing in your org here and there. To lock a record you can use the "for Update".
http://amitsalesforce.blogspot.in/2015/03/locking-statements-for-update.html

In your case please change the Size in your data loader to 1 that will resolve your issue.
https://developer.salesforce.com/page/Loading_Large_Data_Sets_with_the_Force.com_Bulk_API

User-added image

Let us know if this will help you

Thanks
Amit Chaudhary
Archit Budhraja 7Archit Budhraja 7
Hey Amit,

I'm already use FOR UPDATE in my triggers to lock records for editing. Also, I am not using the Data Loader to load data, so I can't set the batch size. I am doing it through a python script that uses a library called salesforce_bulk to load data from our data warehouse into Salesforce.

@Mahesh - I have already gone through those documents, they didn't really help me figure out what exactly is causing the issue.
Sumit Jain 17Sumit Jain 17
These lock errors are very common when you run the bulk API in parallel mode.

Whenever a reference field (lookup field) is accessed by 1 batch, it locks the record and if the same lookup value is getting accessed in another batch (which is highly likely to happen), it starts gives locking errors.

One way to prevent this error can be to sort the records by the reference id field so that the same values can be in a single batch and this will limit the scenario of getting the same reference id value in another batch.

For example, if you are loading contacts, then sort the data on account id.

I hope this helps.

Regards
Sumit
sumit@celebal.com
 
Janina MartinJanina Martin
Sumit Jain 17: Your post saved my freakin' life. I sorted on the record id and the errors stopped. Thank you!!!!!
Yann Jourdain 8Yann Jourdain 8
4 years laters and the advice is still accurate :)
Scott ChesworthScott Chesworth
Sumit Jain.. this was an extreamly useful tip. Thank you for your advice.
Robyn JungersRobyn Jungers
Sumit Jain 17: THANK YOU SOOOOOOOOOOOO MUCH!! Like Janina said...saved my life. Best life hack ever. haha. Thanks, again!!!!!
한성진한성진
Sumit Jain 17 : You saved me!
Harish Mada 9Harish Mada 9
Hi @sumit jain 17, great! it actually worked for me when I am doing updates on child objects like opprotunity, but I am getting a lot of locking exceptions when I am doing bulk updates on Account object. Any suggestions on how to avoid these errors in this case? 
Michal Macak 2Michal Macak 2
@Sumit Jain 17: This happens to me when importing 20k campaign members to 2 campaigns by import wizard (standard user, not admin). How to deal with it? Standard users cannot use dataloader. Thank you!