title: Hibernate JPA+mysql: can not disable caching in createNativeQuery
body: <p>I am using Hibernate/JPA with mySQL, and because of legacy reasons createNativeQuery at one point. The application works with different servers using the same database, so it should do no caching at all but always show the most recent result. I simulate other servers by changing a value manually in a database-editor, but after a change it always gives old results.</p>
  
  <p>As far as I know I should disable any 2nd level caching (not very important because I do not use any ORM-objects), clear() any 1st level caching, and disable mysql query caching (is already done on database level). Where do I fail, or what do I forget? It drives me crazy.</p>
  
  <p>init(): start of the servlet</p>
  
  <pre><code>    entityFactory = Persistence.createEntityManagerFactory("persistence-id");
  </code></pre>
  
  <p>getEntityManager(): start of each request</p>
  
  <pre><code>    destroyEntityManager(); // just in case
      entityFactory.getCache().evictAll();
      entityManager = entityFactory.createEntityManager();
      entityManager.setProperty("javax.persistence.cache.storeMode",
              CacheStoreMode.BYPASS);
      entityManager.clear(); // just in case
  </code></pre>
  
  <p>destroyEntityManager(): end of each request</p>
  
  <pre><code>    if (entityManager != null) {
          if (entityManager.getTransaction().isActive()) {
              entityManager.flush();
              entityManager.getTransaction().commit();
          }
          entityManager.clear();
          if (entityManager.isOpen()) {
              entityManager.close();
          }
          entityManager = null;
      }
  </code></pre>
  
  <p>destroy(): end of servlet</p>
  
  <pre><code>    destroyEntityManager();
      if (entityFactory != null) {
          entityFactory.close();
      }
  </code></pre>
  
  <p>persistence.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence xmlns="http://java.sun.com/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
  http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"&gt;
  &lt;persistence-unit name="WallMountBackOffice-PU"&gt;
      &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
      &lt;class&gt;...&lt;/class&gt;
      &lt;class&gt;...&lt;/class&gt;
      &lt;properties&gt;
          &lt;property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" /&gt;
          &lt;property name="hibernate.connection.url" value="jdbc:mysql://localhost/ourschema" /&gt;
          &lt;property name="hibernate.connection.username" value="root" /&gt;
          &lt;property name="hibernate.connection.password" value="" /&gt;
          &lt;property name="hibernate.connection.pool_size" value="10" /&gt;
          &lt;property name="hibernate.connection.autocommit" value="false" /&gt;
          &lt;property name="hibernate.connection.release_mode" value="on_close" /&gt;
          &lt;property name="dialect" value="org.hibernate.dialect.MySQLDialect" /&gt;
          &lt;property name="hibernate.cache.use_second_level_cache"
              value="false" /&gt;
          &lt;property name="hibernate.cache.use_query_cache" value="false" /&gt;
          &lt;property name="javax.persistence.sharedCache.mode" value="NONE" /&gt;
          &lt;property name="org.hibernate.cacheable" value="false" /&gt;
      &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <p></p>
  
  <p>The code which does the 'select ...':</p>
  
  <pre><code>    ...
      Query jpaQuery = entityManager.createQuery(query);
      entityManager.getTransaction().begin();
      jpaQuery.executeUpdate();
      entityManager.getTransaction().commit();
  </code></pre>