We're receiving the:

caused by: System.DmlException: Insert failed. First exception on row 0; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record: []

exception in some of our customer orgs. Does anyone know of a fairly reliable way to trigger this error so I can test fixes for it?

  • 2
    Dan Appleman gave a presentation on Dreamforce last year on this topic, in the presentation if I remember correctly he also provided some code to reproduce it. This is also covered in his book "Advanced Apex Programming – 2nd edition" advancedapex.com youtube.com/watch?v=AK9m8E5wwts – PepeFloyd Jul 11 '14 at 12:15
  • Here was his slide, though it doesn't have the logic to reproduce it... slideshare.net/developerforce/concurrency-designpatterns-mt – Double A Jul 11 '14 at 12:57
  • this could either be caused by conflicting DML operations in the various trigger execution or some recursive trigger execution. i would assume that the async executions cause multiple subsequent updates on the same records. I would recommend to try to simplify the process to avoid too many related trigger executions. Plz go through the below link may be it will help to resolve your problem click here – tandonprateek Jul 11 '14 at 14:56
  • In a test method, it's impossible, because you need more than one simultaneous transaction (even batches, etc are considered in the same transaction in a test method), but you certainly can trigger that exception with 100% accuracy. I did so in this question to determine how much leeway you have before a transaction fails due to row locks. I don't know if this will help you, though... (Summary) make VF page with two remote actions. Call both at once. – sfdcfox Jul 12 '14 at 17:13
  • Thanks to @PepeFloyd. I've managed to reproduce the error now, using the code given in the video. It's a long video so for anyone else, the code is about 18 mins in. – Jess Jul 14 '14 at 16:15

You will need a technique for creating multiple concurrent requests for processing against the same record that is locked using FOR UPDATE.

One option is to use the @future annotation to queue up multiple concurrent requests that will retrieve a single record using FOR UPDATE in the SOQL call. If you can get these methods executing for long enough you will start getting UNABLE_TO_LOCK_ROW errors as transactions timeout waiting for the exclusive lock.

From anonymous apex you can make several calls to the future method to get them running at the same time.

You will need to create some form of busy operation to keep the record locked for more than 5 seconds (but less than 10 seconds). This could be a simple a for loop that counts from 0 to some very large number. You will need to experiment with the large number to get the required timing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.