Disable caching in JPA (eclipselink)

前端 未结 8 1262
失恋的感觉
失恋的感觉 2020-11-29 02:09

I want to use JPA (eclipselink) to get data from my database. The database is changed by a number of other sources and I therefore want to go back to the database for every

相关标签:
8条回答
  • 2020-11-29 02:14

    See,

    http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching

    For the same EntityManager JPA always requires that one==two, so this is correct, not matter your caching options (this is the L1 cache, or transactional cache, which enforces your transaction isolation and maintains object identity).

    To force the query to refresh (and revert any changes you have made) you can use the query hint "eclipselink.refresh"="true". Or probably better, use a new EntityManager for each query/request, or call clear() on your EntityManager.

    <property name="eclipselink.cache.shared.default" value="false"/>
    

    Is the correct way to disable the shared cache (L2 cache). Please remove all your other settings as they are not correct, and can cause issues.

    EclipseLink does not maintain a query cache by default, so those settings will have no affect. CacheUsage is also not correct, do not use this (it is for in-memory querying).

    0 讨论(0)
  • final Query readQuery = this.entityManager.createQuery(selectQuery);
    readQuery.setParameter(paramA, valueA);
    
    // Update the JPA session cache with objects that the query returns.
    // Hence the entity objects in the returned collection always updated.
    readQuery.setHint(QueryHints.REFRESH, HintValues.TRUE);
    
    entityList = readQuery.getResultList();
    

    This works for me.

    0 讨论(0)
  • 2020-11-29 02:23

    This behavior is correct, otherwise if you change object one and object two with different values you will have problems when persisting them. What is happening is the call to load object two updates the entity loaded in the first call. They must point to the same object since they ARE the same object. This ensures that dirty data cannot be written.

    If you call em.clear() between the two calls, entity one should become detached your check will return false. There is however no need to do that, eclipse link is infact updating your data to the latest which I would guess is what you want since it frequently changes.

    On a side note if you wish to update this data using JPA you will need to be obtaining pessimistic locks on the Entity so that the underlying data cannot change in the DB.

    You will need to disable the query cache as well your cache options were just removing the object cache from play not the query cache, that is why you are not getting the new results:

    In your code:

    em.createNamedQuery("MyLocation.findMyLoc").setHint(QueryHints.CACHE_USAGE, CacheUsage.DoNotCheckCache).getResultList().get(0);
    

    Or in persistence.xml:

    <property name="eclipselink.query-results-cache" value="false"/>
    
    0 讨论(0)
  • 2020-11-29 02:24

    I know this post might be old, but I am writing for others who need help. I had this problem and finally I solved it by this code:

    em.createNamedQuery("findAll").setHint(QueryHints.CACHE_RETRIEVE_MODE, CacheRetrieveMode.BYPASS).getResultList();
    

    It works really well. And we can see in javadoc of the BYPASS enum, it is written that:

    Bypass the cache: get data directly from the database.

    I should notice that I use Weblogic 12c and TopLink as a JPA implementation.

    0 讨论(0)
  • 2020-11-29 02:28

    When you create your query via the entity manager the first-level cache is called.The entity will be old. Lets say I have a key ,which I updated manually in the database and then tried to retrieve:

    Key result = entityManager
                    .createQuery("SELECT k FROM Key k WHERE k.linkKey = :key", Key.class)
                    .setParameter("key", key)
                    .getSingleResult();
    

    The following key will be as it was ,because it is still cached ,my update will not appear.To refresh the entity you will need to call this:

     entityManager.refresh(result);
    
    0 讨论(0)
  • 2020-11-29 02:34

    First level cache is enabled by default and you can not disable it. i.e. no settings in your persistence.xml file will disable first level cache.

    You can only clear out all the entity manager objects by calling

    entityManager.clear()
    

    this will make subsequent queries go to the database (the first time) and then objects are again stored in the cache

    You can force each query to go to the database directly by calling

    query.setHint("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH);
    
    0 讨论(0)
提交回复
热议问题