title: Persistence strategies for High Replication environment (Google App Engine)
body: <p>I have this code thtat works just fine w/o HR:</p>
  
  <pre><code>protected Entity createEntity(Key key, Map&lt;String, Object&gt; props){
      Entity result = null;
      try {
          Entity e = new Entity(key);
          Iterator it = props.entrySet().iterator();
          while (it.hasNext()) {
              Map.Entry&lt;String, Object&gt; entry = (Map.Entry&lt;String, Object&gt;) it.next();
              String propName = entry.getKey();
              Object propValue = entry.getValue(); 
              setProperty(e, propName, propValue);
          }
          key = _ds.put(e);
          if (key != null)
              result = _ds.get(key);
      } catch (EntityNotFoundException e1) {
      }
      return result;
  }
  </code></pre>
  
  <p>This is just a simple method where its function is to create a new <code>Entity</code> out a a given <code>key</code>, just return NULL otherwise. This works fine without the HR configuration in JUnit however when I configured it, I am always getting an error, where <code>_ds.get(key)</code> can't find the key throwing:</p>
  
  <pre><code>EntityNotFoundException: No entity was found matching the key:
  </code></pre>
  
  <p>Specifically when doing:</p>
  
  <pre><code>while(it.hasNext()){
     // stuff
     createEntity(key, map); 
     // stuff
  }
  </code></pre>
  
  <p>I assume that the problem in my code is that it tries to fetch the entity too soon. If thats is the case, how can I deal with this wihout resorting to <code>Memcache</code> or anything like that.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>When the <code>createEntity</code> is executed within a transaction, it fails. However if I remove it outside of the transaction if fails miserably.  I need to be able to run within a transaction, since my higher level API put lots of objects that needs to be there as a group. </p>
  
  <p><strong>Update:</strong></p>
  
  <p>I followed Strom's advise however I found a weird side effect, not doing a <code>_ds.get(key)</code> on the method, makes my <code>PreparedQuery</code> <code>countEntities</code> to fail. Where if add a <code>_ds.get(key)</code> even I don't do anything or save the <code>Entity</code> return from that get <code>countEntities</code> return the expected count. Why is that?</p>