We have external Systems using an rest-endpoint in our Org. In the called function a certain record is called (some kind of tracker) which gets updated everytime the function runs with an success.
Most of the time everything works fine, but when the load increases (in a certain time of the year, due to more customers) we often get the QueryException UNABLE_TO_LOCK_ROW, because the endpoint is called multiple times and the record is always queried and updated. The Query already uses the "FOR UPDATE" keyword.
I already have some approaches to solve this, but my main problem is how to reproduce the error to verify the solution works.
I guess it is not possible via unittests, but also I tried it via a anonymous script calling the endpoint and also via postman, but it didn't work. There is either no exception or a different one.
I know that there is this question Reproduce UNABLE_TO_LOCK_ROW error but it is 8 years old and the linked youtube video is not accessible anymore.
Can somebody provide some hints or example code to reproduce this?
Endpoint which is called
global static Response post() {
Data data = Request //some logic returns data to create an account
Account a = insertAccount(data);
}
When the Account is inserted the accountTriggerHandler runs, and the function where the records are locked:
NumberHelper__c numberHelper = [SELECT Id, currentNumber__c FROM NumberHelper__c in :accounts FOR UPDATE LIMIT 1] // there is only 1 record
for (Account a : accounts){
a.accNumber = numberHelper.currentNumber__c;
numberHelper.currentNumber__c ++;
}
update autoNumber; //UNABLE_TO_LOCK_ROW error
In my scripts I just tried make fast calls to the endpoint, but this probably can't work, because of the user context.