2

I'm getting UNABLE_TO_LOCK_ROW while running the apex test on CircleCI

How can I avoid the deadlock while running tests?This is due to parallel execution of test(Not sure about this). Do We have alternate command to execute the test.

Command

run force:apex:test:run -u circle_build_$CIRCLE_BUILD_NUM -c -r human -d testresults -w 9999

Error

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

1 Answer 1

0

The documentation says :

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.

So this is indeed due to parallel test execution which is the default behaviour, although you can disable it from the setup :

  1. From Setup, enter Apex Test Execution in the Quick Find box, select Apex Test Execution, then click Options
  2. In the Apex Test Execution Options dialog, select Disable Parallel Apex Testing and then click OK.

But you are getting this error when you run tests in CircleCI, so I suppose you do not have access to the scratch org to disable this setting from the UI. You then have 3 options :

  1. Update the scratch org definition file to include the following apexSettings :

    "settings": {
    ...,
    "apexSettings": {
      "enableDisableParallelApexTesting": true
    },
    ...
    

    }

This allows you to create scratch orgs that have the checkbox "Disable Parallel Apex Testing" checked by default, so test will not be executed in parallel.

  1. You can include in your package the following settings file, which needs to be deployed with the rest of your metadata to disable parallel test execution in your target org :
<?xml version="1.0" encoding="UTF-8"?>
<ApexSettings xmlns="http://soap.sforce.com/2006/04/metadata">
   <enableDisableParallelApexTesting>true</enableDisableParallelApexTesting>
</ApexSettings>
  1. Find a way to store the login credentials of your created scratch org in your CirclCI build as artifacts. Check the answers here.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .