5

For example, say I get a List of rows to delete using FOR UPDATE. These are all parent records in a master-detail relationship. Will the children be locked?

5

No. You must independently lock child records that you're interested in. Also see Avoiding Deadlocks, where they mention:

Apex has the possibility of deadlocks, as does any other procedural logic language involving updates to multiple database tables or rows. To avoid such deadlocks, the Apex runtime engine:

  1. First locks sObject parent records, then children.
  2. Locks sObject records in order of ID when multiple records of the same type are being edited.

As a developer, use care when locking rows to ensure that you are not introducing deadlocks. Verify that you are using standard deadlock avoidance techniques by accessing tables and rows in the same order from all locations in an application.

  • 1
    I'm confused because that would seem to say that the Apex locks the children as well as the parents – Brad Thomas Aug 4 '16 at 19:56
  • 1
    @BradThomas The opposite, actually; locking children also lock parents, to allow roll-up summaries, etc. It would be impractical for a parent to lock all of its children indiscriminately, although since the parent is locked first, the additional children may not be lockable until the parent is free. – sfdcfox Aug 4 '16 at 20:06

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.