You need to sign in to do that
Don't have an account?
UNABLE_TO_LOCK_ROW error in parallel apex tests
Hi,
i get an UNABLE_TO_LOCK_ROW error when running my tests in parallel. The error occurs when creating a pricebookentry in the standard pricebook. As far as i know Salesforce locks also the parent Object when inserting a object. Which would mean that in this case while inserting the pricebookentry the pricebook is locked. When now a parallel test wants to add another entry to the locked pricebook the test throws the UNABLE_TO_LOCK_ROW error.
Does anyone know how to get around this?
i get an UNABLE_TO_LOCK_ROW error when running my tests in parallel. The error occurs when creating a pricebookentry in the standard pricebook. As far as i know Salesforce locks also the parent Object when inserting a object. Which would mean that in this case while inserting the pricebookentry the pricebook is locked. When now a parallel test wants to add another entry to the locked pricebook the test throws the UNABLE_TO_LOCK_ROW error.
Does anyone know how to get around this?
Check this out
https://developer.salesforce.com/blogs/engineering/2013/01/reducing-lock-contention-by-avoiding-account-data-skews.html
https://help.salesforce.com/apex/HTViewSolution?urlname=How-can-I-avoid-getting-lock-errors-in-my-organization-1327109108393&language=en_US
Thx for the links to the resources about locking.
do you have another idea?
I had the same issue trying to insert Pricebookentries. It worked fine when you only ran that one test class but runing multiple test classes you got the Error.
This is how I made it work by using a Custom Pricebook instead of the Standard.
Then using the customPricebookEntries when adding Opportunity Products or Order Lines.
I hope that helped you or someone else :)
According to Salesforce Testing Best Practices (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm) :
Best Practices for Parallel Test Execution
Tests that are started from the Salesforce user interface (including the Developer Console) run in parallel. Parallel test execution can speed up test run time. Sometimes, parallel test execution results in data contention issues, and you can turn off parallel execution in those cases. In particular, data contention issues and UNABLE_TO_LOCK_ROW errors might occur in the following cases.
- When tests update the same records at the same time. Updating the same records typically occurs when tests don’t create their own data and turn off data isolation to access the organization’s data.
- When a deadlock occurs in tests that are running in parallel and that try to create records with duplicate index field values. A deadlock occurs when two running tests are waiting for each other to roll back data, which happens if two tests insert records with the same unique index field values in different orders.
You can prevent receiving those errors by turning off parallel test execution in the Salesforce user interface:But enabeling that the test runs super slow.
Indicate the test classes that can run in parallel and aren’t restricted by the default limits on the number of concurrent tests. This makes the execution of test classes more efficient, because more tests can be run in parallel. https://releasenotes.docs.salesforce.com/en-us/winter18/release-notes/rn_apex_annotation_istest_isparallel.htm