Unable to lock row issue in salesforce

Unable to lock row is an issue that scares almost every developer thanks to the limited or almost no options left to resolve it or even predict when and where it would pop up. Salesforce momentarily locks the record when it is getting created or updated. This is done so as to maintain data integrity and avoid data inconsistencies. The lock typically lasts for very few seconds or less but could be good enough to cause the row lock issue on other operations happening on the same record.

While the main purpose of this article is to provide solution on row lock issue; it is very important to understand in which scenarios such an error would occur. This is important because most of the times it becomes difficult to identify how and from where the error is coming.

Parent Record is locked when the child record is getting inserted or updated.
- Consider a child record is getting inserted then its parent is temporarily locked for updates and if the same record is then updated manually by a user or through different operation or batch class the record update fails with the row lock error.

Two child records of same parent are being attempted with an update through bulk update in two different parallel batches at the same time then one of the record fails with the row lock issue as it is already locked because of other update.

If there are too many detail records for a master record then there is a very high chance that the master record would remain locked and updates on detail records would fail as the master record would remain locked.

Assynchronous operations like batch apex or future methods can cause the issue while the same records are being update through different operations.

All in all predicting and preventing this error is a very tough job as the error remains intermittent without a particular pattern and repeatability. Now coming to the main point, lets see what we can do to make sure we reduce the these errors to the minimum.  

1. If the error is thrown at a particular update or insert dml then we can have a while loop on it and retry it untill the lock is released and successfull update/insert happens.
  We can do this for no of few attempts as otherwise it may becoem infinite loop.

integer i = 0
  update accList
catch(exception ex){
   if(ex.geterrormessage() == 'Unable to row lock'){ 
     i = 10;

2. If the error is thrown during bulk data updates through data loader then try to have serial updates rather than parallel. Or if parallel is used then try to have smaller batch sizes this would reduce the risk of errors.Try to sort out records so that child parent records remain in same batches and are split across multiple batches. These techniques although complicated but if followed would definitely reduce the errors and hence the overall update time.

3. Try to avoid writing too many future methods that are called from trigger and would perform dml operations on same or related objects.

No comments:

Post a comment