title: Servlet sends old version of entity (JPA)
body: <p>When I make a request to my <code>Servlet</code>, it checks the database for any updated <code>Entities</code> (I am using JPA for that). The updated <code>Entity</code> is then fetched and the <code>Servlet</code> is supposed to return the entity as a JSON String. <strong>But</strong> I am getting an <em>old</em> version of the entity, one that has not been updated yet – although I can see the underlying MySQL database already has the updates. <strong>How is this possible? Am I missing any buffers or caches?</strong></p>
  
  <p>This is my architecture / the data flow:</p>
  
  <pre><code> ––––––--    timestamp of previous request   ----------–––
  | client |–––––––––––––––––––––––––––––––––&gt;| PullServlet |
   --------        (e.g. 1475056871050)        -------------
                                                     |(injected)
                                                     |
                   --------------------       ---------------
                  |   Entity Facade    |&lt;----|    remote     |
                  |  implementation    |     | Entity Facade |
                   --------------------       ---------------
                            |
                   @NamedQuery
                   | SELECT m
                   | FROM MyEntity m
                   | WHERE 
                   | m.updated &gt;=:previousRequest
  </code></pre>
  
  <p>Let's say the row in the MySQL database looks like this:</p>
  
  <pre><code> id | updated       | value
   ---+---------------+--------
   12 | 0             | "hi"
  </code></pre>
  
  <p>Then an update is made and the <code>updated</code> column logs the time in milliseconds when the update was made:</p>
  
  <pre><code> id | updated       | value
   ---+---------------+--------
   12 | 1475056872002 | "hello"
  </code></pre>
  
  <p>so the query <code>SELECT ... WHERE 1475056872002 &gt;= 1475056871050</code> returns the entity because the <code>updated</code> column¹ has a value that is greater than the <code>previousRequest</code> parameter.</p>
  
  <p><strong>However</strong> when I log the result of the query, I get something like this</p>
  
  <pre><code>id=12, updated=0, value="hi"
  </code></pre>
  
  <p>instead of</p>
  
  <pre><code>id=12, updated=1475056872002, value="hello"
  </code></pre>
  
  <p>And the <em>old</em> row gets send to my Servlet, which then sends the <em>old</em> version as JSON to my client:</p>
  
  <pre><code>{
    "id" : 12,
    "updated" : 0,
    "value" : "hi"
  }
  </code></pre>
  
  <p><strong>Question</strong>: is there any cache that I could <em>"flush"</em> or anything, to make sure that the selected rows are always up to date? </p>
  
  <h2>Update</h2>
  
  <p>So I added some logging and this is the result:</p>
  
  <p><strong>Facade makes update</strong></p>
  
  <pre><code>UPDATED entity: id=45111, updated=1475067494578, value=100 
  </code></pre>
  
  <p><strong>Client makes request to servlet a few seconds later</strong></p>
  
  <pre><code>Client requested update – fetching entities  // servlet
  Facade fetched update: id=45111, updated=1475066854564, value=96  // Facade
  servlet received update: id=45111, updated=1475066854564, value=96 // servlet again
  </code></pre>
  
  <p><strong>Client receives JSON</strong></p>
  
  <pre><code>{
    "id": 45111,     
    "updated": 1475066854564,     
    "value": 96
  }
  </code></pre>
  
  <h2>Solution</h2>
  
  <p>Thanks to Navneet Arora I found out that the entites where indeed cached and the query used those cached instances instead of retrieving the entites from the database. <a href="https://stackoverflow.com/a/13709765/896249">This Answer</a> from another question shows how ignore the cache.<br></p>
  
  <p>Now my query looks like this:</p>
  
  <pre><code> TypedQuery&lt;Post&gt; query = getEntityManager().createNamedQuery("MyEntity.getUpdatedSince", MyEntity.class);
   query.setParameter("previousRequest", previousRequest);
   query.setHint(QueryHints.REFRESH, HintValues.TRUE);  //this does the trick
  </code></pre>
  
  <p>And now everything is fine and I get all the updates :)</p>
  
  <p>__________________<br>
  ¹ the <code>updated</code> column and the <code>previousRequest</code> parameter are unix timestamps in milliseconds</p>