title: Firebase: Create/Update multiple child nodes atomically
body: <p>Say I have a project with a "users" node and a "pets" node. When a user gets a pet, I want to both add the pet's key to the user's "pets" node and the user id to the pet's "owner" node.</p>
  
  <p><strong>Example:</strong></p>
  
  <pre><code>{
      "users":{
          "user1":{
              "pets":{
                  "pet1":true,
                  "pet3":true
              }
              ...
          }
      },
      "pets":{
          "pet1":{
              "owner":"user1",
              ...
          },
          ...
      }
  }
  </code></pre>
  
  <p>I want to make sure both updates succeed to prevent an inconsistent state.</p>
  
  <p>I read about how to work with Firebase transactions <a href="https://firebase.google.com/docs/database/android/read-and-write#save_data_as_transactions" rel="nofollow noreferrer">here</a>, however when I try to create a transaction that modifies the relevant values (using dbRef.runTransaction(...)), I get this DatabaseError error:</p>
  
  <blockquote>
    <p>The transaction had too many retries</p>
  </blockquote>
  
  <p>The transaction only seems to work when I call runTransaction() on the specific node I modify (for example, calling </p>
  
  <pre><code>dbRef.child("users").child("user1").runTransaction(...));
  </code></pre>
  
  <p>However, this does not help me because I need to reach both the relevant user and the pet.
  How can I make this transaction work?</p>