【问题标题】:Hibernate JPA+mysql: can not disable caching in createNativeQueryHibernate JPA+mysql:无法在 createNativeQuery 中禁用缓存
【发布时间】:2012-10-18 20:09:59
【问题描述】:

我正在将 Hibernate/JPA 与 mySQL 一起使用,并且由于遗留原因,我曾一度使用 createNativeQuery。该应用程序与使用相同数据库的不同服务器一起工作,因此它根本不应该进行缓存,但始终显示最新结果。我通过在数据库编辑器中手动更改值来模拟其他服务器,但更改后它总是给出旧结果。

据我所知,我应该禁用任何二级缓存(不是很重要,因为我不使用任何 ORM 对象),clear() 任何一级缓存,并禁用 mysql 查询缓存(已经在数据库级别完成)。我在哪里失败,或者我忘记了什么?它让我发疯。

init(): servlet 的开始

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

getEntityManager():每个请求的开始

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

destroyEntityManager():每次请求结束

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

destroy(): 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>

执行“选择...”的代码:

    ...
    Query jpaQuery = entityManager.createQuery(query);
    entityManager.getTransaction().begin();
    jpaQuery.executeUpdate();
    entityManager.getTransaction().commit();

【问题讨论】:

    标签: mysql hibernate caching jpa


    【解决方案1】:

    第一行有错误,必须是“BYPASS”而不是“REFRESH”,如下:

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

    并且建议使用 JPA 枚举而不是字符串文字,所以它是:

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

    【讨论】:

      【解决方案2】:

      您可以使用setHint() storeModeretrieveMode 方法。如果您尝试检索记录,请使用 retrieveModeBYPASS

      休眠

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

      对于 EclipseLink。

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

      JPA 2.0 规范

      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
      }   
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-29
        • 2013-07-28
        • 2012-11-17
        • 1970-01-01
        • 2017-12-25
        • 2021-05-22
        • 1970-01-01
        • 2012-07-29
        相关资源
        最近更新 更多