title: Hibernate query returns stale data
body: <p>I have a hibernate query (hibernate 3) that only reads data from the database. The database is updated by a separate application and the query result does not reflect the changes in the database. </p>
  
  <p>With a bit of research, I think it may have something to do with the Hibernate L2 cache (I don't think it's the L1 cache since I always open a new session and close it after it's done).</p>
  
  <pre><code>Session session = sessionFactoryWrapper.getSession();
  List&lt;FlowCount&gt; result = session.createSQLQuery(flowCountSQL).list();
  session.close();
  </code></pre>
  
  <p>I tried disabling the second-layer cache in the hibernate config file but it's not working:</p>
  
  <pre><code>&lt;property name="hibernate.cache.use_second_level_cache"&gt;false&lt;/property&gt;
  &lt;property name="hibernate.cache.use_query_cache"&gt;false&lt;/property&gt;
  &lt;propertyname="cache.provider_class"&gt;org.hibernate.cache.NoCacheProvider&lt;/property&gt;
  </code></pre>
  
  <p>I also added <code>session.setCacheMode(CacheMode.Refresh);</code> after <code>Session session = sessionFactoryWrapper.getSession();</code> to force a refresh on the L1 cache but still not working...</p>
  
  <p>Is there another way to pick up the changes in the database? Am I doing something wrong on how to disable the cache? Thanks.</p>
  
  <p><strong>Update:</strong>
  I did another experiment by monitoring the database query log:</p>
  
  <ol>
  <li>Run the code the 1st time. Check the log. The query shows up.</li>
  <li>Wait a few minutes. The data has changed by another application. I verified it through MySql Workbench. To distinguish from the previous query I add a dummy condition.</li>
  <li>Run the code the 2nd time. Check the log and the query shows up. </li>
  </ol>
  
  <p>Both time I'm using the same query but since the data has changed, the result should be different but somehow it's not...</p>