JPA 2 + EclipseLink:缓存问题 [英] JPA 2 + EclipseLink : Caching Issue

查看:214
本文介绍了JPA 2 + EclipseLink:缓存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在缓存和JPA实体(EclipseLink 2.4.1)+ GUIPER PERSIST上有一个奇怪的行为

I have a strange behavior with caching and JPA Entities (EclipseLink 2.4.1 ) + GUICE PERSIST

我不会使用缓存,但是我会随机获取在MySQL数据库中已更改的旧实例

I will not use caching, nevertheless I get randomly an old instance that has already changed in MySQL database.

我尝试了以下操作:


  1. 向JPA实体添加@ Cacheable(假)。

  1. Add @ Cacheable (false) to the JPA Entity.

禁用persistence.xml文件中的Cache属性:

Disable Cache properties in the persistence.xml file :

     <class>MyEntity</class>
     <shared-cache-mode>NONE</shared-cache-mode>    
     <properties>      

            <property name="eclipselink.cache.shared.default" value="false"/>
         <property name="eclipselink.cache.size.default" value="0"/>
        <property name="eclipselink.cache.type.default" value="None"/>
        <property name="eclipselink.refresh" value="true"/>
        <property name="eclipselink.query-results-cache" value="false"/>
            <property name="eclipselink.weaving" value="false"/>          
     </properties>  

即使激活跟踪EclipseLink,i请参阅JPQL查询:

Even activating trace EclipseLink, i see the JPQL query:

ReadObjectQuery执行查询(名称= readObject referenceClass = XX sql = ...(仅进行调用),找到实体管理器

,但是,随机返回该类的旧值。

but, However randomly returns an old value of that class.

也许使用不同的EntityManager实例并且每个人都有其缓存吗?
我见过以下相关文章:禁用JPA EclipseLink 2.4缓存
如果是这样,可以使用以下方法清除所有EntityManager的缓存:????
em.getEntityManagerFactory()。getCache()。evictAll();
是可以使用evictALL清除所有高速缓存吗?

推荐答案

全部清除用于您拥有的共享高速缓存实体管理器实例默认情况下需要具有自己的一级缓存跟踪他们创建的所有托管实例。实体管理器用于表示逻辑事务,因此不应长期存在。您需要丢弃EntityManager并重新获得它们,或者只是在逻辑上清除它们,而不是让其缓存中的托管实体数量不断增长。这也将有助于限制陈旧的数据问题,尽管悲观锁定无法消除它。如果您尚未避免过时的数据覆盖,建议您使用乐观锁定。

Evict all is for the shared cache which you have disabled already anyway. EntityManager instances are required by default to have a first level cache of their own to keep track of all managed instances they have created. An entityManager is meant to represent logical transactions and so should not be long lived. You need to throw away your EntityManagers and re obtain them or just clear them at logical points rather than let the number of managed entitites in its cache grow endlessly. This will also help limit the stale data issue, though nothing other than pessimistic locking can eliminate it. I recommend using optimistic locking if you aren't already to avoid overwriting with stale data.

这篇关于JPA 2 + EclipseLink:缓存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆