Hibernate JPA + mysql: cannot disable caching in createNativeQuery

I am using Hibernate / JPA with mySQL, and due to obsolete reasons to createNativeQuery at some point. The application works with different servers using the same database, so you don’t need to cache at all, but it always shows the latest result. I mimic other servers by manually changing the value in the database editor, but after the change, it always gives old results.

As far as I know, I have to disable second level caching (not very important because I do not use any ORM objects), clear () any level 1 caching and disable mysql query caching (already done at the database level). Where do I fail or what do I forget? It drives me crazy.

init (): start of servlet

entityFactory = Persistence.createEntityManagerFactory("persistence-id"); 

getEntityManager (): start of each request

  destroyEntityManager(); // just in case entityFactory.getCache().evictAll(); entityManager = entityFactory.createEntityManager(); entityManager.setProperty("javax.persistence.cache.storeMode", CacheStoreMode.BYPASS); entityManager.clear(); // just in case 

destroyEntityManager (): end of each request

  if (entityManager != null) { if (entityManager.getTransaction().isActive()) { entityManager.flush(); entityManager.getTransaction().commit(); } entityManager.clear(); if (entityManager.isOpen()) { entityManager.close(); } entityManager = null; } 

destroy (): end of servlet

  destroyEntityManager(); if (entityFactory != null) { entityFactory.close(); } 

persistence.xml:

 <?xml version="1.0" encoding="UTF-8"?> <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"> <persistence-unit name="WallMountBackOffice-PU"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <class>...</class> <class>...</class> <properties> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" /> <property name="hibernate.connection.url" value="jdbc:mysql://localhost/ourschema" /> <property name="hibernate.connection.username" value="root" /> <property name="hibernate.connection.password" value="" /> <property name="hibernate.connection.pool_size" value="10" /> <property name="hibernate.connection.autocommit" value="false" /> <property name="hibernate.connection.release_mode" value="on_close" /> <property name="dialect" value="org.hibernate.dialect.MySQLDialect" /> <property name="hibernate.cache.use_second_level_cache" value="false" /> <property name="hibernate.cache.use_query_cache" value="false" /> <property name="javax.persistence.sharedCache.mode" value="NONE" /> <property name="org.hibernate.cacheable" value="false" /> </properties> </persistence-unit> 

Code that executes "select ...":

  ... Query jpaQuery = entityManager.createQuery(query); entityManager.getTransaction().begin(); jpaQuery.executeUpdate(); entityManager.getTransaction().commit(); 
+4
source share
2 answers

You can use the setHint() storeMode or retrieveMode . If you are trying to retrieve a record, use retrieveMode with BYPASS .

For sleep mode

 query.setHint("javax.persistence.cache.storeMode", "REFRESH"); query.setHint("javax.persistence.cache.retrieveMode", "REFRESH"); 

For EclipseLink.

 query.setHint("javax.persistence.cache.storeMode", "REFRESH"); query.setHint("javax.persistence.cache.retrieveMode", "REFRESH"); 

JPA 2.0 Specification

 public enum CacheRetrieveMode { /** * Read entity data from the cache: this is * the default behavior. */ USE, /** * Bypass the cache: get data directly from * the database. */ BYPASS } public enum CacheStoreMode { /** * Insert/update entity data into cache when read * from database and when committed into database: * this is the default behavior. Does not force refresh * of already cached items when reading from database. */ USE, /** * Don't insert into cache. */ BYPASS, /** * Insert/update entity data into cache when read * from database and when committed into database: * Forces refresh of cache for items read from database. */ REFRESH } 
+1
source

There is an error in the first line, it should be "BYPASS" and not "REFRESH", as shown below:

 query.setHint("javax.persistence.cache.retrieveMode", "BYPASS"); 

And it is recommended that you use JPA enums instead of string literals, so this will be:

 query.setHint(QueryHints.CACHE_RETRIEVE_MODE, CacheRetrieveMode.BYPASS); query.setHint(QueryHints.CACHE_STORE_MODE, CacheStoreMode.REFRESH); 
+1
source

Source: https://habr.com/ru/post/1442751/


All Articles