4

我将 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();
    }

持久性.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();
4

2 回答 2

3

第一行有一个错误,一定是“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); 
于 2014-11-09T09:59:56.527 回答
1

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

对于休眠

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
}   
于 2012-10-29T17:52:57.080 回答